943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1216
  • C++ RSS
Jul 27th, 2008
0

Help on socket programming

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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 :
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Hannahlv is offline Offline
31 posts
since Jul 2008
Jul 27th, 2008
0

Re: Help on socket programming

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 27th, 2008
0

Re: Help on socket programming

Ok...I show you the full code of client.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Hannahlv is offline Offline
31 posts
since Jul 2008
Jul 27th, 2008
0

Re: Help on socket programming

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Hannahlv is offline Offline
31 posts
since Jul 2008
Jul 27th, 2008
0

Re: Help on socket programming

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
C++ Syntax (Toggle Plain Text)
  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).
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 27th, 2008
0

Re: Help on socket programming

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 :

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Hannahlv is offline Offline
31 posts
since Jul 2008
Jul 27th, 2008
0

Re: Help on socket programming

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Simple Bubble sort C++ function
Next Thread in C++ Forum Timeline: switch problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC