# WSAEWOULDBLOCK

This topic is 4902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, I've just begun writing a client and server for a game I'm working on. I've set up a socket for the server, and one for the client(two seperate programs of course), and the client can connect to the server and get accepted successfully. Anyway, the problem is AFTER the socket is set up and the client has already connected, if I try to send data to the server, the recv() function sends an error to WSAGetLastError(), which is WSAEWOULDBLOCK. I searched around and realized that WSAEWOULDBLOCK is the error when the data is not yet ready to be received. My server is non-blocking, and I let it sit there for a few minutes and it never received the data. Soo, I decided to check the client, yet the send() function returns the bytes it sent, indicating that it was a success. So my question is, what the heck could the problem be? :P Here's the code where I set up the socket for the client:
int make_socket(char *hostname, int port)
{
int rVal=0;

WORD version;
version = MAKEWORD(1,1);

LPHOSTENT hostEntry;

hostEntry = gethostbyname(hostname);

if(!hostEntry)
{
sError("Failed gethostbyname()");
//WSACleanup();
return CS_ERROR;
}

//create the socket
SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

if(theSocket == SOCKET_ERROR)
{
sError("Failed socket()");
return CS_ERROR;
}

serverInfo.sin_family = PF_INET;

serverInfo.sin_port = htons(port);

if(rVal != 0)
{
sError("Failed connect()");
return CS_ERROR;
}

nonblock(theSocket);

main_socket = theSocket;
}


And here is the code where I tested the send() function and realized the server was getting the WSAEWOULDBLOCK error:
	if(keys[VK_DOWN])
{
char *data = "work";
ret_val = send(main_socket, data, strlen(data), 0);

if(ret_val == SOCKET_ERROR)
print("Error with socket sending.");

else
sprintf(buf, "Sent %d bytes.", ret_val);

}


If I wasn't clear enough on my problem, please let me know. Thanks in advance :)

##### Share on other sites
You should use the select() function to check if theres data pending on a socket. If there is, you can go ahead and recv(). If not, you can either wait, or return some status code saying that theres no data yet.

##### Share on other sites
Quote:
 Original post by Evil SteveYou should use the select() function to check if theres data pending on a socket. If there is, you can go ahead and recv(). If not, you can either wait, or return some status code saying that theres no data yet.

I assume you mean use select() on the server, yet the server already does this:

[source="cpp"]   /* Poll (without blocking) for new input, output, and exceptions */		if (select(maxdesc + 1, &input_set, &output_set, &exc_set, &null_time) < 0)		{			perror("Select poll");			return;		}

The server doesn't hit the recv() function until after it knows there was data sent it's way. In fact, I can connect to the server using a different client(one I don't have access to the source code of course), and it will work perfectly fine. When I send data using my client, it will hit the recv() and get the blocking error.

But like I said before, no error is found when I use the send() function on my client, but there is that blocking error when the server tries to get the input.

Maybe I'm not getting what you said though. :P

##### Share on other sites
So it works with another client, but not your client? So its a client problem, and send() isn't working right?

EDIT: What port are you using? I had a problem just like this when I was making my SMTP server (port 25). It turns out Norton Antivirus does strange things to my sockets...

##### Share on other sites
I was using port 4000. For some reason, I switched the port to 4444, and used another server to test the input, and it reads it perfectly.

Looks like it just doesn't like the client I was testing it with for some reason.

Thanks anyways :)

##### Share on other sites
According to my random port list site, port 4000 is used by ICQ and Command and Conquer. You didn't happen to be running either of those did you?
Apart from that, I've no idea why it works on a different port...