Jump to content
  • Advertisement
Sign in to follow this  
SCarvenger

[C++] Socket Server issue (Solved)

This topic is 3891 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

I have started my study in sockets and decided to implement a server using select(), and it started to give me some problems: 1) When a client enters the server and don't send any message the server is blocked until it send's one; 2) When a client disconnects my server hangs and has to be restarted (nice server uh? hehe). Ah, by the way the object clients is a vector<> and the class Iterator is a typedef vector<>::iterator.
select(maximumSocket + 1, &readDescriptor, NULL, NULL, NULL);

if (FD_ISSET(sock->sock, &readDescriptor))
{
	SOCKET s_client = accept(sock->sock, NULL, 0);
	Socket* socket  = new Socket(s_client);


	clients.push_back(socket);
	FD_SET(s_client, &readDescriptor);
			
	if (s_client > maximumSocket)
	{
		maximumSocket = s_client;
	}

}

Iterator end = clients.end();
Iterator i   = clients.begin();
for (;i != end;i++)
{
	if (FD_ISSET((*i)->sock, &readDescriptor))
	{
		string message;

		int connected = (*i)->getMessage(message);

		if (!connected)
		{
			FD_CLR((*i)->sock, &readDescriptor);
			clients.erase(i);
			cout << "disconnected\n";
		}
		else
		{
			cout << "onmessage\n";
			listener->onMessage(message, (*i));
		}
	}
}
[Edited by - SCarvenger on September 30, 2007 7:12:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
Ok, i have discovered that select changes the fd_set structure provided, then i created a master file descriptor and "saves" the connections structure. =)

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!