我正在编写一个代码,其中有while(1)循环。在while(1)循环内部有一个消息框,然后是recvfrom()函数,根据我的理解,它必须阻止调用recvfrom()直到收到数据或recvfrom()函数失败。
但实际发生的是while(1)循环continuoulsy继续显示消息框,并且不会转到recvfrom()语句。
突出显示我认为问题所在的部分。
即。而(1)被困在第一个陈述。我正在添加整个代码以获得帮助。请帮我弄清楚我的错误。
DWORD WINAPI sendThreadProcedure(LPVOID param)
{
int PORT = 9999;
WSADATA wsa;
//Initialise winsock//
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
exit(EXIT_FAILURE);
}
//Create a socket//
SOCKET sendSocketIdentifier;
SOCKADDR_IN sendSocket;
if((sendSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
exit(EXIT_FAILURE);
}
//Socket Created//
//Prepare the sockaddr_in structure//
sendSocket.sin_family = AF_INET;
sendSocket.sin_addr.s_addr = INADDR_ANY;
sendSocket.sin_port = htons(PORT);
//Bind//
if( bind(sendSocketIdentifier ,(struct sockaddr *)&sendSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
// "BIND FAILED",
exit(EXIT_FAILURE);
}
int clientSocketLength = sizeof(SOCKADDR_IN);
char receiveBuffer[10000];
int recv_len=0;
//=======================================================================
char threadNumberBuffer[256] = "Thread Number : ";
char buff[32];
char file[32]="Master";
char fileName[128];
int sendCount=0;
FILE *fpSend;
itoa(threadNumber,buff,10);
strcat(threadNumberBuffer,buff);
strcpy(fileName,file);
strcat(fileName,buff);
strcat(fileName,".txt");
MSG msg;
// Start of problem area
while(1)
{
MessageBox( NULL,
"Going in while loop",
"",
MB_ICONINFORMATION);
recv_len = recvfrom(sendSocketIdentifier, receiveBuffer,
sizeof(receiveBuffer), 0,
(struct sockaddr *) &clientSocket,
&clientSocketLength)
{
// "Could not receive data at start of sending thread"
}
// End of problem area
//"Start of Receive thread"
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
if(msg.message == Send_File)
{
if((fpSend = fopen(TEXT(fileName), "r+b")) == NULL)
{
MessageBox( NULL,
"Unable to open the File",
"Error!",
MB_ICONEXCLAMATION |
MB_OK);
}
char file_buffer[10000];
int bytes_read=0;
char new_buffer[1000] = "FILE",send[1000];
if(sendto(sendSocketIdentifier, new_buffer, sizeof(new_buffer), 0,
(struct sockaddr *) &clientSocket,
sizeof(clientSocket)) < 0)
{
MessageBox( NULL,
"NOT SENNT!",
"ERROR!",
MB_ICONEXCLAMATION |
MB_OK);
break;
}
while(fpSend!=NULL)
{
if((bytes_read=fread(file_buffer, sizeof(char), 1, fpSend))<=0)
{
if(feof(fpSend))
{
char new_buffer[1000] = "EXIT",send[1000];
if(sendto(sendSocketIdentifier, new_buffer,
sizeof(new_buffer), 0,
(struct sockaddr *) &clientSocket,
sizeof(clientSocket)) < 0)
{
MessageBox( NULL,
"NOT SENNT!",
"ERROR!",
MB_ICONEXCLAMATION |
MB_OK);
break;
}
char send_Count[128];
itoa(sendCount,send_Count,10);
fclose(fpSend);
break;
return 0;
}
else
{
char err[128], bread[128];
itoa(errno,err,10);
itoa(bytes_read,bread,10);
MessageBox( NULL,
"Unable to copy file into buffer",
bread,
MB_ICONEXCLAMATION |
MB_OK);
fclose(fpSend);
break;
}
}
else
{
if(sendto(sendSocketIdentifier, file_buffer,
bytes_read, 0,
(struct sockaddr *) &clientSocket,
sizeof(clientSocket)) < 0)
{
MessageBox( NULL,
"NOT SENNT!",
"ERROR!",
MB_ICONEXCLAMATION |
MB_OK);
break;
}
else
{
sendCount = sendCount+1;
}
}
}
//======================Sent a File at Socket===========================
}
else
DispatchMessage(&msg);
}
}
return 0;
}
修改 即使我将代码截断为以下内容,即使这样,消息框也会显示近40次,然后可能会调用recvfrom()。
while(1)
{
MessageBox( NULL,
"Going in while loop",
"",
MB_ICONINFORMATION);
if((recv_len = recvfrom(sendSocketIdentifier, receiveBuffer,
sizeof(receiveBuffer), 0,
(struct sockaddr *) &clientSocket,
&clientSocketLength)) == SOCKET_ERROR)
{
char err[128];
itoa(WSAGetLastError(),err,10);
MessageBox( NULL,
"Could not receive data at start of sending thread",
err,
MB_ICONINFORMATION);
}
MessageBox( NULL,
receiveBuffer,
"Start of Receive thread",
MB_ICONINFORMATION);
}
答案 0 :(得分:2)
如果消息框不断显示,则表明已满足条件导致接收返回。
所以你必须做的是,检查接收的返回值并查看它的内容。如果它是-1,那么另外看看errno以找出它试图告诉你的内容。