Multiple clients connect to server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrmodest
    New Member
    • Dec 2011
    • 1

    Multiple clients connect to server

    So... I am trying to write a C++ Server that can accept multiple clients. I have already written some very simple code that will accept one client but I am having trouble getting it to accept more than one. Below is the server code I have right now, it works but only for one client.
    Code:
    #include "info.h"
    
    using namespace std;
    
    int main(){
    
    long answer;
    char message[256];
    
    WSAData wsaData;
    WORD DLLVERSION;
    
    DLLVERSION = MAKEWORD(2,1);
    answer = WSAStartup(DLLVERSION, &wsaData);
    
    SOCKADDR_IN addr;
    int addrlen = sizeof(addr);
    SOCKET sListen;
    SOCKET sConnect;
    bool x;
    sConnect = socket(AF_INET,SOCK_STREAM,NULL);
    
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    addr.sin_family = AF_INET;
    addr.sin_port = htons(1001); // port
    sListen = socket(AF_INET,SOCK_STREAM,NULL);
    bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
    listen(sListen, SOMAXCONN);
    
    	cout << "Chat Server" << endl;
    	cout << endl;
    
    
    	cout << "Waiting for a connection..." << endl;
    	if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
    	{
    		cout << "Connection found!" << endl;
    		x = true;
    }
    	
    while(x == true)
    {
    		
    		answer = recv(sConnect, message, sizeof(message)+1, NULL);
    		cout << message << endl;
    		
    }
    	
    return 0;
    }



    And, here is the client code if you wish to view it...

    Code:
    #include "header.h"
    
    using namespace std;
    
    int main(){
    
    string confirm;
    char message[200];
    string strmessage;
    long answer;
    char address[256];
    short port;
    bool run;
    
    WSAData wsaData;
    WORD DLLVersion;
    DLLVersion = MAKEWORD(2,1);;
    answer = WSAStartup(DLLVersion,&wsaData);
    
    SOCKADDR_IN addr;
    int addrlen = sizeof(addr);
    
    SOCKET sConnect;
    sConnect = socket(AF_INET, SOCK_STREAM, NULL);
    
    //("127.0.0.1");// loopback
    
    addr.sin_family = AF_INET;
    
    
    	cout << "Chat Client" << endl;
    	cout << "Address?" << endl;
    	cin.getline(address,256);
    	addr.sin_addr.s_addr = inet_addr(address);
    	cout << "Port?" << endl;
    	cin >> port;
    	addr.sin_port = htons(port); // server port
    	cout << endl;
    
    run = true;
    
    	while(run == true){
    
    		if(connect(sConnect, (SOCKADDR*)&addr, sizeof(addr))){
    
    			cin.getline(message,256);
    			answer = send(sConnect, message, sizeof(message), NULL);
    
    		}
    	}
    
    system("pause");
    return 0;
    
    }

    I did not include the header files because they are probably obvious to you guys, I am new here though and if you wish to view them I will be happy to present them for your viewing pleasure. I am doing this for a learning experience and whether you want to write code or pseudo-code or no variation of code at all I will be worthy to accept any form of advice. I figure that my client does not need to be changed, but if it does, please... let me know.

    Thank you.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The only way to handle multiple connections well is to use multiple threads of execution

    Code:
    LISTENING-THREAD
    
        CREATE SOCKET
        BIND SOCKET
        LISTEN
    
        DO
            ACCEPT CONNECTION
            START HANDLER-THREAD WITH NEW SOCKET
        UNTIL SOME HALT CONDITION
    
    END THREAD
    
    HANDLER-THREAD ACCEPTS SOCKET
        READ FROM SOCKET
        HANDLE DATA
    END THREAD
    In theory you actually could use one handler thread but on the whole it is just as easy to have multiple threads unless you plan on have a very large number of connections.

    Comment

    Working...