Jump to content
  • Advertisement
Sign in to follow this  
ThomasMathers

Problem with checking for client connections

This topic is 4817 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 All, I seem to be having a problem with my server side code. I keep on getting disconnected from my server application every time I try to connect to it through telnet. I think the problem has to do with me accepting a temporary client connection and assigning that later to my client connection array. I am not sure if you can use the equality operator on socket variables. I need to use this temporary client connection because I want to give a client a message saying "This server is currently full" if the server is currently full. By storing the connection in a temporary structure I can send a message to him without storing it in my client connection array. Perhaps there is another error in there which is making my clients disconnect!... [cpp] void CPlayerHost::Check_Connections() { int s; int nfds; int clientLength = (int)sizeof(m_pClients->m_clientAddress); fd_set conn; timeval serverTimeout; serverTimeout.tv_sec = 0; serverTimeout.tv_usec = 0; FD_ZERO(&conn); FD_SET(m_serverSocket, &conn); nfds = m_serverSocket + 1; // Check to see if there are any clients waiting to connect to the server s = select(nfds, &conn, NULL, NULL, &serverTimeout); if (s > 0) { // There is at least one person waiting to connect to the server. CPlayerClient tempClient; tempClient.m_clientSocket = accept(m_serverSocket, (SOCKADDR*)&tempClient.m_clientAddress, &clientLength); // Check to see if there is room on the server for one more client to connect for (int i = 0; i < m_totalClients; i++) { if (m_pClients.m_isConnected == false) { // We have found an open space to store this client's connection. m_pClients.m_clientSocket = tempClient.m_clientSocket; m_pClients.m_clientAddress = tempClient.m_clientAddress; m_pClients.m_isConnected = true; m_connectedClients++; return; } } // The server is currently full right now and cannot accept any more connections. Local_Message(tempClient.m_clientSocket, CreatePacket("This server is currently full. Please try again later", 0)); } } [/cpp]

Share this post


Link to post
Share on other sites
Advertisement
I suspect that your CPlayerClient destructor calls closesocket(). When the variable tempClient goes out of scope, the destructor is called, and the socket is closed, leaving you with an invalid socket in the m_pClients array.

You could use a pointer to a CPlayerClient, you could remove the closesocket() call from the destructor, and clean it up manually, or you could use some kind of reference counting (and implement operator= and thecopy constructor if you haven't already) to make sure that you only close sockets that aren't referenced from anywhere else.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!