Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C udp Sockets
am trying to make an asynchronous udp chat application currently havung only one client and server.the problem is that when I run my server it displays a lot of redundant data and afterward whwn some text is typed displays the error"Error sending the file! \n"... Could someone please look at the code and let me know where am I going wrong???
 
  u_long iMode=1;
         ioctlsocket(sd,FIONBIO,&iMode);
         int n=sd+1;
 

         fd_set readfds,writefds;
         while(1)
                  {
          FD_ZERO(&readfds);
          FD_ZERO(&writefds);
          FD_SET(sd,&readfds);
          FD_SET(sd,&writefds);
 
         int rv = select(n, &readfds, &writefds, NULL, NULL);
         if(rv==-1)
         {
             printf("Error in Select!!!\n");
             exit(0);
         }
         if(rv==0)
         {
           printf("Timeout occurred\n");
         }
         if (FD_ISSET(sd, &readfds))
         {
             FD_CLR(sd,&readfds);
             int client_length = (int)sizeof(struct sockaddr_in);
             memset(&buffer,0,sizeof(buffer));
            int bytes_received = recvfrom(sd, buffer,SIZE, 0, (struct sockaddr *)&client, &client_length);
        if (bytes_received < 0)
         {
        fprintf(stderr, "Could not receive datagram.\n");
        closesocket(sd);
        WSACleanup();
        exit(0);
         }
         }
 
    printf("\nClient says: %s",buffer);
 

 
    printf("\nWrite :");
 

    fgets(buffer,SIZE,stdin);
 
    if(FD_ISSET(sd,&writefds))
        {
            FD_CLR(sd,&writefds);
            int client_length = (int)sizeof(struct sockaddr_in);
            if(sendto(sd, buffer,strlen(buffer), 0, (struct sockaddr *) &client,client_length)<0)
                    {
                printf("Error sending the file! \n");
                exit(1);
                    }
        }
 

                }
              closesocket(sd);
              WSACleanup();
 
             return 0;
 
               }
Posted 16-Apr-13 1:04am
Edited 16-Apr-13 1:15am
v2
Comments
Richard MacCutchan at 16-Apr-13 7:34am
   
There is no point in printing an error message unless it contains some useful information. Use the WSAGetLastError function to find out why the send failed.
Aayman Khalid at 16-Apr-13 7:49am
   
WSAGetLastError returns "10047"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 285
1 DamithSL 265
2 CPallini 195
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100