Advertisement

Winsock Multi-user server problem

Started by April 07, 2004 08:38 AM
0 comments, last by chicknstu 20 years, 10 months ago
I have a server, which multiple clients may connect to. This function should check for any messages from any clients, and works fine until one of the clients disconnects. at thispoint, the server stops recieving messages from any of them at all. This is the check message function void c_muServer::Check_Message(void) { fd_set input_set, exc_set; // Create Input and Error sets for the select function int s, // This is Used for the Select command to store the value it gets back nfds; // Used for Compatability timeout.tv_sec=0; // Used for Timeout timeout.tv_usec=0; //Set up the input,and exception sets for select(). FD_ZERO(&input_set); FD_ZERO(&exc_set); nfds = 0; // Loop makes it so that the Input and Error sets will look at all the Sockets connected for (int i = 0; i < ClientsConnected { FD_SET(Client.ClientSocket,&input_set); FD_SET(Client.ClientSocket,&exc_set); nfds++; i++; } // The Actual Select Function sees if data coming in and stores it in s s = select(nfds,&input_set,NULL,&exc_set,&timeout); if (s > 0) // Is there data coming in? { // Look Through all the clients to see which one is giving error or data for (int i=0; i < ClientsConnected<img src="wink.gif" width=15 height=15 align=middle> { if (FD_ISSET(Client.ClientSocket,&exc_set)) // There was an Error? { // For this Server we assume it was a disconnection or something so close the socket. // Store the IP of the Client that Just Disconnected char *ClientIp = inet_ntoa(Client.clnt_addr.sin_addr); cout << "This Client Has Disconnected: " << ClientIp << endl; Disconnect(i); } if ((FD_ISSET(Client.ClientSocket, &input_set))) // Actual Data coming through? { // Get whatever message the client sending, and carry out appropriate response Get_Message(i); } i++; // Next Client } } } This is the disconnect function int c_muServer::Disconnect(int s) { closesocket(Client.ClientSocket); Client.InUse = NO; ClientsConnected–; Game.Player.active=false; Game.Player.frameno=1; printf ("Client %d Disconnected\n\n",s); return(1); } can anyone see what''s wrong off the top of their heads? any help would be greatly appreciated stu </i>
Wherever you call recv(), which is probably in your get message function, do something like this:


int ret = recv(...);
if (ret == -1) Disconnect();



See if that takes care of your problem. Also, when you disconnect, I believe the correct procedure is:
-shutdown socket for read and write
-recv() any remaining data from it
-close socket
-decrement your variables

but this shouldn''t be your problem... just another tip.

This topic is closed to new replies.

Advertisement