我正在尝试编写一个非常简单的 C 程序。出于某种原因,我不能在同一个程序中使用 writeFileEx 和 recvfrom,因为每次调用 writeFileEx 都会导致 recvfrom 崩溃。
这是相关的代码片段
struct sockaddr_in server, client_address;
int client_length, recv_len;
char buf[BUFLEN];
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0 )
{
//Error
}
SOCKET s = socket (AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
//invalid socket error
}
server.sin_family = AF_INET;
server.sin_addr.s_addr= INADDR_ANY;
server.sin_port = htons (PORT);
if (bind(s, (stuct sockaddr* ) &server, sizeof(server)) == SOCKET_ERROR)
{
//bind error
}
OVERLAPPED ovWrite;
memset(&ovWrite,0,sizeof(ovWrite));
ovWrite.offset=0;
ovWrite.OffsetHigh=0;
ovWrite.hEvent = CreateEvent (0,TRUE,0,0);
memset(buf,'\0',BUFLEN);
while (1)
{
if (!recvfrom(s, buf, BUFLEN,0,(struct sockaddr *) &client_address, &client_length)
{
fprintf(stderr, "Recvfrom Failed %d\n", WSAGetlastError());
}
if (!WriteFileEx(serialHandle, buf, strlen(buf),&ovWrite,NULL))
{
fprintf(stderr, "Error writing to COM port %d\n", GetlastError());
}
memset(buf,'\0', BUFLEN);
}
我正在使用 lcc 编译器,结果程序在循环的第二次迭代中崩溃,访问冲突 0xc0000005。当我用另一个输入源(如标准输入)替换 recvfrom 时,程序运行良好。这是一个已知问题还是 2 个函数调用根本不能同时存在?