Winsock send() always returns error 10057 in server

Soran picture Soran · Jul 18, 2012 · Viewed 7.8k times · Source

I'm trying to automate a server request to the client upon connection but I keep getting the WSAGetLastError of 10057. I've set up requests from the client to the server without issues so I don't understand why I cant do the reverse ? Maybe the server must wait for the 1st 'send' from the client but I don't understand why that could be so ?

I'm using:
-asynchronous socket
-TCP
-s is a valid socket
-i'v looped RequestInfo for several passes but nothing changes
-select() returns 0
-datasize returns -1 with errcode 10057

THANK YOU !!!

SERVER:

//first - following the debugger  
FD_ACCEPT  
int acc = accept(s, (struct sockaddr*)&fromm, &fromlenn);  //success
if(acc <= 0)  
{  
    eLOGG << "\nFAIL FD_ACCEPT: " << WSAGetLastError();  
}  
RequestInfo();   

//then
RequestInfo()
{
    stringstream ssConverter;
    ssConverter.clear();    ssConverter.str(string());
    ssConverter << "00aa";      //request signal

    bool blogin = false;

    eLOG << "signal is: *" << ssConverter.str() << "*";

    int bufSize = ssConverter.str().length();

    fd_set writefds;

    struct timeval timeout;
    timeout.tv_sec = 3;
    timeout.tv_usec = 0;

    FD_ZERO(&writefds);
    FD_SET(s, &writefds);

    int sel = select(s, NULL, &writefds, NULL, &timeout);
    if(sel == SOCKET_ERROR) 
    {
        eLOG << "\nselect - read fail: " << WSAGetLastError();
    }
    if(sel == 0)
    {
        eLOG << "\nselect - not connected: " << WSAGetLastError();
    }

    if (FD_ISSET(s, &writefds))
    {
        eLOG << "\n FD_ISSET";
    }
    eLOG << "\nauth socket is: " << s;
    int dataSize = send(s, ssConverter.str().c_str(), bufSize, 0);
    if(dataSize < bufSize)  
    {
        eLOG << "\n FD_ISSET";
    }
    //...etc
}

Answer

Nikolai Fetissov picture Nikolai Fetissov · Jul 18, 2012

You are confusing the listening server socket s and the connected socket, which you get from accept() in the acc variable. You cannot send or receive data over the server socket since it's not connected to anything. Its only purpose is to accept new connections from clients. After all, servers usually handle multiple clients.

By the way, that connected socket does not inherit non-blocking status from the server socket, so you need to mark it so after every accept().