Help on socket programming

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

Help on socket programming

 
0
  #1
Jul 27th, 2008
Hi everyone!
I'm doing my project related to socket programming. The send and receive part I got problem.
The code is below :
Server side:
  1. printf("Handling client %s\n",inet_ntoa(their_addr.sin_addr));
  2. if (send(new_fd, "Menu\n", 5, 0) == -1) //(1)
  3. perror("send");
  4. if(send(new_fd, "1. A.mp3, 2. B.mp3, C.mp3\n", 26,0) == -1) //(2)
  5. perror("send");
  6. if(send(new_fd, "Which file to play?\n", 20, 0) == -1) //(3)
  7. perror("send");

Client side :
  1. buf[numbytes] = '\0';
  2. printf("%s",buf);
  3. printf("Enter your song number : "); //(4)
  4. fflush(stdin);
  5. gets(choice);

I mark the 4 lines for shorter writing.
Sometimes when server sends messages, the client can receive full of them(mean in the order (1) to (4) ). But sometimes it just receive the (1) line, then (4) and after that is (2) and (3). When this happens, I must close the server and client, run again, then it works.
My teacher said that this is caused by buffer. But exactly what I should do to make sure everytime the client will receive (1) to (4) respectively?
Thank for your help.
Last edited by Hannahlv; Jul 27th, 2008 at 1:06 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help on socket programming

 
0
  #2
Jul 27th, 2008
> fflush(stdin);
> gets(choice);
Nevermind the network stuff, how about getting the basics right?
fflush(stdin);
gets()

> send(new_fd, "Menu\n", 5, 0)
0, 1, 2, 3, 4 are also valid return results. You can't just assume that if it wasn't an error (-1), that the only other possible result must have been 5. If it wasn't 5, then it's YOUR job to call send() again with the remaining data.

> buf[numbytes] = '\0';
Show us how you declared buff, and how you called recv() to fill that buffer.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

Re: Help on socket programming

 
0
  #3
Jul 27th, 2008
Ok...I show you the full code of client.
  1. #define PORT 3490 // the port client will be connecting to
  2.  
  3. #define MAXDATASIZE 100 // max number of bytes we can get at once
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. int sockfd, numbytes, rcvSize;
  8. char buf[MAXDATASIZE];
  9. char choice[10];
  10. struct hostent *he;
  11. struct sockaddr_in their_addr; // connector's address information
  12.  
  13. if (argc != 2) {
  14. fprintf(stderr,"usage: client hostname\n");
  15. exit(1);
  16. }
  17.  
  18. if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
  19. perror("gethostbyname");
  20. exit(1);
  21. }
  22.  
  23. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  24. perror("socket");
  25. exit(1);
  26. }
  27.  
  28. their_addr.sin_family = AF_INET; // host byte order
  29. their_addr.sin_port = htons(PORT); // short, network byte order
  30. their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  31. memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
  32.  
  33. if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof their_addr) == -1) {
  34. perror("connect");
  35. exit(1);
  36. }
  37.  
  38. if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
  39. perror("recv");
  40. exit(1);
  41. }
  42.  
  43. buf[numbytes] = '\0';
  44.  
  45. printf("%s",buf);
  46.  
  47. printf("Enter your song number : ");
  48. fflush(stdin);
  49. gets(choice);
  50. if(send(sockfd, choice, 7, 0) == -1)
  51. perror("send");
  52.  
  53. return 0;
  54. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

Re: Help on socket programming

 
0
  #4
Jul 27th, 2008
Also, I can't get you at this point :

send(new_fd, "Menu\n", 5, 0)
0, 1, 2, 3, 4 are also valid return results. You can't just assume that if it wasn't an error (-1), that the only other possible result must have been 5. If it wasn't 5, then it's YOUR job to call send() again with the remaining data.

Function send() is send(new_socket,buffer,bufsize,0);, so the third parameter is the length of the buffer, is it? Since the buffer is a string, so count the length and put it into the function.
It's not correct?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help on socket programming

 
0
  #5
Jul 27th, 2008
If you have
char buf[] = "hello";
int len = strlen( buff );

And you do
n = send ( fd, buff, len, 0 );
You also need to do
if ( n < len ) {
  n = send( fd, buff[n], len-n, 0 );
}

Actually, use a while loop, as in
  1. char buf[] = "hello";
  2. int len = strlen( buf );
  3. char *bp = buf;
  4. while ( len > 0 ) {
  5. n = send( fd, bp, len, 0 );
  6. if ( n > 0 ) {
  7. // at least partly successful, advance through the buffer
  8. len -= n;
  9. bp += n;
  10. } else if ( n == 0 ) {
  11. // connection closed
  12. } else {
  13. // an error
  14. }
  15. }
Over the loopback, or just over your LAN, you're unlikely to see this happen with small packets. But generally speaking, it's something you need to take care of.

The same is true of the recv(). The message may become fragmented, and it's up to you to reassemble it (say by looking for a \n).
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

Re: Help on socket programming

 
0
  #6
Jul 27th, 2008
Ok..Thank for your instruction. However, still one more thing, can you let me know the reason why, the client do not always receive full of messages from the server? (I need the messages come out in the below order :

  1. Menu :
  2. 1. A.mp3 2.B.mp3 3.C.mp3
  3. Which file to play?
and the last will be the client's line : "Enter your song number..."

But sometimes the 1st line come, then last line, and the middle lines come.

Do I need to do anything here?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help on socket programming

 
0
  #7
Jul 27th, 2008
I've no idea.
Being a stream, you either get stuff in the order you sent it, or you don't get it at all.

Like I said, make sure you're actually sending all the data with each send() call.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1036 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC