Ayse
Ayse

Reputation: 2754

while(1) loops infinitely executes its first statement and does not go to the next statement

I am writing a code in which there is while(1) loop. Inside while(1) loop there is a message box and then recvfrom() function which according to my understanding must block in call to recvfrom() until data is receieved or recvfrom() function fails.

But what actually happens is the while(1) loop continuoulsy keeps on showing message box and doesnot go to recvfrom() statement ever.

Highlighted the part where I think problem lies.

i.e. while(1) is stuck at its first statement. I am adding the whole code for assistance. Please help me figuring out my mistake.

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

Edit: Even if I truncate the code to the following, even then, message box is displayed almost 40 times and then recvfrom() is called probably.

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

Upvotes: 1

Views: 571

Answers (1)

Devolus
Devolus

Reputation: 22094

If the messagebox is continously showing, then there is a condition fullfilled that causes receive to return.

  1. There is a problem with your socket and receive returns -1
  2. There is already data available and you must read it. receive returns > 0
  3. Your socket is nonblocking == -1
  4. Your socket timed out, == 0 (not in this case)

So what you must do is, check the return value from receive and see what it says. If it is -1 then additionaly look at errno to find out what it tries to tell you.

Upvotes: 2

Related Questions