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


i writing code in there while(1) loop. inside while(1) loop there message box , recvfrom() function according understanding must block in call recvfrom() until data receieved or recvfrom() function fails.

but happens while(1) loop continuoulsy keeps on showing message box , doesnot go recvfrom() statement ever.

highlighted part think problem lies.

i.e. while(1) stuck @ first statement. adding whole code assistance. please me figuring out mistake.

dword winapi sendthreadprocedure(lpvoid param) {     int port = 9999;     wsadata wsa;       //initialise winsock//     if (wsastartup(makeword(2,2),&wsa) != 0)     {         exit(exit_failure);     }       //create socket//        socket sendsocketidentifier;     sockaddr_in sendsocket;       if((sendsocketidentifier = socket(af_inet , sock_dgram , 0 )) == invalid_socket)     {         exit(exit_failure);     }     //socket created//      //prepare 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 @ 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 open 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 copy file 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 file @ socket===========================             }             else                 dispatchmessage(&msg);         }     }     return 0; } 

edit: even if truncate code following, then, message box displayed 40 times , recvfrom() 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 @ start of sending thread",                     err,                      mb_iconinformation);     }      messagebox( null,                 receivebuffer,                 "start of receive thread",                  mb_iconinformation); } 

if messagebox continously showing, there condition fullfilled causes receive return.

  1. there problem socket , receive returns -1
  2. there data available , must read it. receive returns > 0
  3. your socket nonblocking == -1
  4. your socket timed out, == 0 (not in case)

so must is, check return value receive , see says. if -1 additionaly @ errno find out tries tell you.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -