简体   繁体   中英

C++ Winsock Socket Error: 10038 (WSAENOTSOCK)

I have an master slave application Master running on WinXp(i7, 2.1 Ghz) and slave being a controller board. The master sends request to the slave and the slave in response send to the master the data cyclically. This data send cyclically by the slave is 1000 bytes per 0.5 msec. When the masters makes the request to send the data, on the console an error is reported.

" Select() function error code:: 10038 ".

This is the code for WSAENOTSOCK .

This application is a single threaded application receiving the data from the slave. As from the error it seems that the socket is closed before it is checked by the select function.

Can any one please point me in the direction ?

:::Source Code::::

int Receive()
{
    int rc;
    socklen_t cli_alen;
    struct timeval to;
    fd_set read_fd, write_fd, excep_fd;

    FD_ZERO(&write_fd);
    FD_ZERO(&excep_fd);

sock_again:
    if (!_isSocketOpen) 
    {
        return 0;
    }
    FD_ZERO(&read_fd);
    FD_SET(_sock_fd, &read_fd);
    to.tv_sec =  0;
    to.tv_usec = 0;
    cli_alen = sizeof(SOCKADDR_IN);

    rc = select(_sock_fd+1, &read_fd, &write_fd, &excep_fd, &to);

    if (rc == 0 )  
    {   // Timeout
        // printf("XCP Port %d : select() timded out \n", _port);
        acess = 1;
        goto sock_again;
    } 
    else if (rc == SOCKET_ERROR) 
    {
        // Error
        LogError("XCP: select() error %d", WSAGetLastError());
        closesocket(_sock_fd);
        return -1;
    }
    else 
    {
        // Data
        if (!FD_ISSET(_sock_fd, &read_fd)) 
        {
            LogError("XCP: select() wrong socket descr");
            return -1;
        }
        else
        {
            //read data
            rc = recvfrom(_sock_fd, (char *)_recvBuf, UDP_RECVBUFLEN, 0, (LPSOCKADDR)&_saddr, &cli_alen);
}
}
}

:::: Edited ::::

int CloseUdpConnection()
{
    if (closesocket(_sock_fd) == SOCKET_ERROR) {
        LogError("closesocket() error: %d", WSAGetLastError());
        return -1;
    }
    _isSocketOpen = 0;
    LogError("successfully closed socket %s:%d", _address, _port);
    return 0;
}

::::: Debug Trace :::::

xcpApplication.exe -i 192.168.1.100 -p 5555
c
--> Connecting...
<-- Connection established
t
--> Setting daq signal list...
Sorting daq signal list...
Sorting done
<-- Daq signal list set
d
--> Configuring daq lists...
<-- Configuration done
r
--> Starting measurement...
<-- Measurement started

**_sock_fd: -448997078**
XCP: select() error 10038

::::: Trace ::::

--> Starting measurement...
<-- Measurement started
_sock_fd: -1963260928 
_sock_fd: 0x8afb0400
XCP: select() error 10038

:::::: Trace ::::

xcpApplication.exe -i 192.168.1.100 -p 5555

_sock_fd: 1936 _sock_fd: 0x790
successfully opened socket 192.168.1.100:5555

--> Connecting...
<-- Connection established
t
--> Setting daq signal list...
Sorting daq signal list...
Sorting done
<-- Daq signal list set
d
--> Configuring daq lists...
<-- Configuration done
c
--> Connecting...
<-- Connection established
r
--> Starting measurement...
<-- Measurement started
_sock_fd: 901186560 _sock_fd: 0x35b70400
XCP: select() error 10038
>net helpmsg 10038

An operation was attempted on something that is not a socket.

You can safely close a socket in the TCP/IP sense, using the shutdown() function. But don't call closesocket() on a SOCKET handle which is in use by select() or poll() .

Actually, in Win32 you will usually prefer to use WSAAsyncSelect or WSAEventSelect instead.

If your application is single-threaded and doesn't use asynchronous callbacks, then you can't call closesocket during the select call. And I see that you return immediately after calling closesocket . So you must be entering the function with an invalid socket, but _isSocketOpen is true. Perhaps you should set that flag to false when calling closesocket() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM