Sign in to follow this  
Kison

WSAEWOULDBLOCK

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)
{
	WSADATA wsaData;
    int rVal=0;

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

    WSAStartup(version,(LPWSADATA)&wsaData);

    LPHOSTENT hostEntry;

    //store information about the server
    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;
    }

    //Fill in the sockaddr_in struct
    SOCKADDR_IN serverInfo;

    serverInfo.sin_family = PF_INET;
    serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

    serverInfo.sin_port = htons(port);

    rVal= connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
    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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
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.


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites

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