• Advertisement
Sign in to follow this  

Multi-client server

This topic is 4255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again, it seems such a short time ago that I posted another question... But this question is just keeping me from progressing, so I thought I'd just be rude, and ask here again... Don't go holding it against me! Anyway - the problem. I want to write a server (I have some vague ideas of writing a MUD) and I need to get multiple clients to connect to it. I have used WSAAsyncSelect to make my message function process network messages as well and I made an InitServer function to initialize the server up to listening on a port. However, the following code is the response to te FD_ACCEPT event... Edit: the first connection attempt everything is OK. When I try to connect a second time, the FD_ACCEPT doesn't seem to get fired. It doesn't output "A client is connecting."...
case SM_SOCKETEVENT:
{

    int event = WSAGETSELECTEVENT(lParam);
    HWND hOutput = GetDlgItem ( hWnd, TXT_OUTPUT );	
		
    if ( event == FD_ACCEPT ) {

        // this function will add messages to a multiline edit control
	UpdateOutput ( hOutput, (char *) " - A client is connecting." );

	SOCKADDR_IN * from;
	int fromlen = sizeof ( from );
	SOCKET clientsocket;
			
        // took this method from msdn. Basically it waits until clientsocket
        // becomes a valid socket handle and then continues...
	while ( clientsocket == INVALID_SOCKET) {
	    clientsocket = accept ( serversocket, NULL, NULL );
	}

	if ( clientsocket == INVALID_SOCKET ) {

	    char * tError = (char *) malloc ( 20 * sizeof(char) );
	    sprintf ( tError, "Error: %u", WSAGetLastError() );
	    UpdateOutput ( hOutput, tError );

	} else {

	    UpdateOutput ( hOutput, "[server]: Connection established" );

	}				
			

    }
 
    return 0;

    break;
}


However, the above code works for only one client. I read about multithreaded servers, but apart from the fact that I'd rather avoid multithreading yet, I can't imagine that multithreading is the only way to get more clients to be able to connect... I also tried making the serversocket listen again after i accepted a connection, but that would just fail... What do I have to do to make it accept multiple clients? [Edited by - rogierpennink on May 29, 2006 9:21:55 AM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Have a look at the BSD Select function (which is also implemented in winsock), it allows you to listen to many sockets(as well as other things) in one thread.

Share this post


Link to post
Share on other sites
Can you be a bit more specific? I couldn't find anything on msdn by looking for 'BSD Select' or 'BSDSelect'.
Also, isn't it possible to have only one listening socket? I have made a CLIENT struct of which I make a copy each time a client connects, and I fill it with stuff like Socket handle, address etc.

Ideally, it'd look like this:

LISTEN FOR CONNECTIONS ON serversocket
IF AN FD_ACCEPT EVENT OCCURS KEEP USING accept() UNTIL A VALID sockethandle has been created
FILL A CLIENT STRUCT WITH CONNECTION-SPECIFIC INFO + A UNIQUE ID
LISTEN FOR CONNECTIONS ON serversocket
REPEAT

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://beej.us/guide/bgnet/output/html/advanced.html#select

Share this post


Link to post
Share on other sites
Quote:

clientsocket = accept ( serversocket, NULL, NULL );

Did you try

SOCKADDR_IN client_addr;
int addrlen = sizeof(client_addr);
clientsocket = accept ( serversocket, (SOCKADDR*)&client_addr, &addrlen );

I have seen this be a problem before, so I just thought you should try it.
Not sure if it will help though

Share this post


Link to post
Share on other sites
Yea, I did try that at first, but it didn't work. I think I should explain a little more about what I want because it may be confusing.

Basically, there's a problem with the listening. After one incoming connection the serversocket apperently stops listening because no more FD_ACCEPT events are fired at later connection attempts... I want to know how to keep the serversocket in that listening state to make sure it keeps firing FD_ACCEPT events on connection attempts..

Share this post


Link to post
Share on other sites

SOCKET clientsocket;

// took this method from msdn. Basically it waits until clientsocket
// becomes a valid socket handle and then continues...
while ( clientsocket == INVALID_SOCKET) {
clientsocket = accept ( serversocket, NULL, NULL );
}
[/quote]

Just a thought but you may want to initialize clientsocket to INVALID_SOCKET. I am not sure if it actually is initialized to INVALID_SOCKET by default which would make you lucky it even works the first time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement