Sign in to follow this  

[C++] Socket Server issue (Solved)

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

This topic is 3725 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this