1

我正在编写一个包含 while(1) 循环的代码。在 while(1) 循环内部有一个消息框,然后是 recvfrom() 函数,根据我的理解,它必须阻止对 recvfrom() 的调用,直到接收到数据或 recvfrom() 函数失败。

但实际发生的是 while(1) 循环不断地显示消息框并且永远不会进入 recvfrom() 语句。

突出显示我认为问题所在的部分。

即 while(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);
}
4

1 回答 1

2

如果消息框持续显示,则有一个条件已满,导致接收返回。

  1. 您的套接字有问题并收到返回 -1
  2. 已有数据可用,您必须阅读它。收到回报 > 0
  3. 您的套接字是非阻塞的 == -1
  4. 您的套接字超时,== 0(在这种情况下不是)

所以你必须做的是,检查接收的返回值,看看它说了什么。如果它是-1,那么另外查看 errno 以找出它试图告诉你的内容。

于 2013-05-20T07:35:00.917 回答