我有一个在 WinXp(i7, 2.1 Ghz) 上运行的主从应用程序 Master,从属应用程序是一个控制器板。主站向从站发送请求,从站作为响应循环向主站发送数据。从机循环发送的数据为每 0.5 毫秒 1000 字节。当 master 发出发送数据的请求时,控制台会报错。
“ 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;
}
::::: 调试跟踪 :::::
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
::::: 痕迹 ::::
--> Starting measurement...
<-- Measurement started
_sock_fd: -1963260928
_sock_fd: 0x8afb0400
XCP: select() error 10038
:::::: 痕迹 ::::
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