C ++ Winsock套接字错误:10038(WSAENOTSOCK)

时间:2013-03-19 15:44:35

标签: c++ select winsock

我有一个主从属应用程序Master在WinXp(i7,2.1 Ghz)上运行,slave是控制器板。主设备向从设备发送请求,并且从设备响应周期性地向主设备发送数据。从站周期性发送的数据是每0.5毫秒1000字节。 当主服务器发出数据发送请求时,会在控制台上报告错误。

“Select()函数错误代码:: 10038”。

这是WSAENOTSOCK的代码。

此应用程序是从单个线程应用程序接收来自从属的数据。 从错误看,套接字在被select函数检查之前似乎已关闭。

任何人都可以指点我的方向吗?

:::源代码::::

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);
}
}
}

::::编辑::::

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

1 个答案:

答案 0 :(得分:5)

>net helpmsg 10038

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

您可以使用shutdown()功能安全地关闭TCP / IP意义上的套接字。但请勿在{{1​​}}或closesocket()使用的SOCKET句柄上致电select()

实际上,在Win32中,您通常更愿意使用poll()WSAAsyncSelect

如果您的应用程序是单线程的并且不使用异步回调,那么您无法在WSAEventSelect调用期间致电closesocket。我看到你在致电select后立即返回。因此,您必须使用无效套接字输入函数,但closesocket为真。也许您应该在调用_isSocketOpen时将该标志设置为false。