Why did my winsocks2 send result in 10057 error sometimes?

北城以北 提交于 2019-12-25 17:12:52

问题


I did tcp socket connection with normal(?) steps as the code shows below. But send sometimes result in 10057 error while server-end printed some log which says connection is in, I have no idea about what's wrong with my code. Any help is appreciated. Any information about stable and robust windows socket programming is appreciated.

MSDN description about 10057 error:

Socket is not connected.A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied. Any other type of operation might also return this error—for example, setsockopt setting SO_KEEPALIVE if the connection has been reset.


PS: Real ip address in below code snippet is replaced by 127.0.0.1. Code isn't runnable.

int connect_to_server() {
SOCKET soc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
unsigned char buf[1024];

WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);

/* create a tcp socket; */  
if ((soc = socket(AF_INET, SOCK_STREAM, 0/*IPPROTO_TCP*/)) <= 0)
{
    LOGFMTF("errcode[-1], create socket fail!");
    return -1;
}

serveraddr.sin_family = AF_INET;

serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

unsigned long mode = 1;  // 1 to enable non-blocking socket
if (ioctlsocket(soc, FIONBIO, &mode) == SOCKET_ERROR) {
    LOGFMTF("errcode[-2], ioctlsocket fail.");
    return -2;
}

if (::connect(soc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
    /* FIONBIO socket enters here can go run; non-FIONBIO socket enters here stands for real error;*/
    if (WSAGetLastError() != WSAEWOULDBLOCK) {
        LOGFMTF("errcode[-3], connect fail.");
        //return -3;
    }

    //return -1;
}

g_client_fd = soc;

LOGFMTT("client_fd[%d]", g_client_fd);
LOGFMTT("connect to server last err[%d].", WSAGetLastError());

return 0;
}

/** send loop; */
DWORD WINAPI send_thread_loop(LPVOID pM) {
LOGFMTT("send thread[%d] start.", GetCurrentThreadId());

for (;!g_to_exit;) 
{
    QMutexLocker locker(&g_send_queue_lock);
    if (g_send_queue.count() == 0) {
        Sleep(Send_Thread_Gap);
    }else {
        LOGFMTT("send one packet.");
        int pakcet_size = g_send_queue.begin()->size;
        char *addr = g_send_queue.begin()->data;
        ::send(g_client_fd, addr, pakcet_size, 0);

        /* ------------!Attention begin------------ */
        LOGFMTT("send last err[%d].", GetLastError());--------> sometimes got 10057 error.
        /* ------------!Attention end------------ */

        delete[]addr;
        g_send_queue.erase(g_send_queue.begin());
    }
}

LOGFMTT("send thread[%d] exit.", GetCurrentThreadId());

return 0;
}

int start_net_thread() {
g_recv_thread = CreateThread(NULL, 0, recv_thread_loop, NULL, 0, NULL);

g_send_thread = CreateThread(NULL, 0, send_thread_loop, NULL, 0, NULL);

return 0;
}

int main(){
if(connect_to_server() != 0) 
{
    QMessageBox msgBox;
    msgBox.setText("failed.");
    msgBox.exec();
    return -1;
}

start_net_thread();

return 0;
}

回答1:


Why did my winsocks2 send result in 10057 error sometimes?

It didn't. At least, there is no evidence that it did.

::send(g_client_fd, addr, pakcet_size, 0);
/* ------------!Attention begin------------ */
LOGFMTT("send last err[%d].", GetLastError());--------> sometimes got 10057 error.

It isn't valid to call WSAGetLastError() unless there was an error, which in this case would have been signalled by send() returning -1. But as you aren't checking for that, there is no evidence here of a problem to be solved, except for your poor coding around send(). It should read:

if (::send(g_client_fd, addr, pakcet_size, 0) == -1)
{
    /* ------------!Attention begin------------ */
    LOGFMTT("send last err[%d].", GetLastError());--------> sometimes got 10057 error.
}

NB The word is spelt 'packet', not 'pakcet'.




回答2:


Hope my shallow answer will help someone.

It turns out to be that I ioctlsocket client socket to nonblocking mode before connect. Connection maynot completed when connect returns, but I immediately start send thread. There is a chance that send thread send data to an unconnected soeckt.

Like MSDN says, With a nonblocking socket, the connection attempt cannot be completed immediately.

PS: send may send bytes that less than expected bytes specified via the third argument of send.

int remain_size = pakcet_size;
int this_read_cnt = 0;
for (;;) {
    if ((this_read_cnt = ::send(g_client_fd, addr+ pakcet_size - remain_size, remain_size, 0)) != SOCKET_ERROR) {
        remain_size = pakcet_size - this_read_cnt;
    }
    else {
        LOGFMTT("send last err[%d].", GetLastError());
    }

    if (remain_size == 0) {
        delete[]addr;
        g_send_queue.erase(g_send_queue.begin());
        break;
    }
}   


来源:https://stackoverflow.com/questions/46450006/why-did-my-winsocks2-send-result-in-10057-error-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!