General sockets question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LwSiX
Posts: 22
Joined: Wed Jan 17, 2007 2:24 pm

General sockets question

Post by LwSiX »

Hi there,

In my game i have some basic network code with sockets, but i have come across a small issue. My understanding of how a program retreives data from a socket is that it will simply sit on a function such as this:

Code: Select all

//get a message sent to the socket
bytes = recv(sck_HL,buffer,1023,0);


until data is received, I have it check immediately after this function by:

Code: Select all

if (bytes == SOCKET_ERROR)
{
	printf("ERROR:%d\n",WSAGetLastError());
	printf("Connection lost, reconnecting in 5 seconds..\n");
	Sleep(5000);
	Connect();
}
but if the socket isn't explicitly closed from the other end, how do we detect when the socket is disconnected? It just sits...infinitely waiting for data that is never going to arrive?
Warchief
Posts: 204
Joined: Tue Nov 22, 2005 10:58 am

Post by Warchief »

The recv() function receives a message from a socket. The recv() call can be used on a connection mode socket or a bound, connectionless socket. If no messages are available at the socket, the recv() call waits for a message to arrive unless the socket is nonblocking. If a socket is nonblocking, -1 is returned and the external variable errno is set to EWOULDBLOCK. select() can be used to wait for incoming messages.
Check your socket. Have you set the flags for non blocking?
Warchief's Warboard 3D Now hotseat release
LwSiX
Posts: 22
Joined: Wed Jan 17, 2007 2:24 pm

Post by LwSiX »

Thanks! Guess i overlooked that part.
Post Reply