Click here to Skip to main content
15,036,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 16-Apr-13 0:15am
v2
Comments
Richard MacCutchan 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 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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900