By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Why read function does not read data when client sends more than 1000 bytes?

P: n/a
Hello,

I am using standard socket functions to send and receive data on a client/server model. I am using K-Develop under Ubuntu 10.04 OS. The application works fine locally but when I deploy it on my CentOS 5.5 server, data is not properly received by the server.

Whenever client sends more than 1000 bytes, the read() function on server side does not read all the data. Here is the server side code snippet.

Expand|Select|Wrap|Line Numbers
  1.  int sockfd, newsockfd, portno, clilen;
  2.     const int BUFFER_SIZE = 1024 * 3;
  3.     char buffer[BUFFER_SIZE];
  4.  
  5.     struct sockaddr_in serv_addr, cli_addr;
  6.     int yes = 1;
  7.     portno = 12345;
  8.  
  9.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  10.  
  11.     if (sockfd < 0) 
  12.            error("ERROR in opening socket...\n");  
  13.     else
  14.     printf("Socked opened successfully...\n");
  15.  
  16.  
  17.     if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) 
  18.     {
  19.     error("Setsockopt error...");
  20.     exit(1);
  21.     }
  22.  
  23.  
  24.     bzero((char *) &serv_addr, sizeof(serv_addr));
  25.  
  26.     serv_addr.sin_family = AF_INET;
  27.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  28.     serv_addr.sin_port = htons(portno);
  29.  
  30.     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
  31.     {
  32.     error("ERROR in binding...\n");
  33.     exit(1);
  34.     }
  35.     else
  36.       printf("Socked binded successfully...\n");
  37.  
  38.  
  39.      if( listen(sockfd,5) < 0 )
  40.       printf("Error in listening...\n");         
  41.      else
  42.           printf("Server has started listening...\n");         
  43.  
  44.  
  45.      printf("Waiting for connections on port %d...\n",portno);         
  46.  
  47.      while (true)
  48.      {        
  49.        int n = 0;
  50.        clilen = sizeof(cli_addr);
  51.  
  52.          newsockfd = accept(sockfd,(struct sockaddr *)&cli_addr,(socklen_t*)&clilen);
  53.  
  54.          if (newsockfd < 0)
  55.     {
  56.         error("ERROR in accepting connection...");    
  57.         continue;
  58.     }
  59.     else
  60.         printf("Connected accepted.\n");
  61.  
  62.      bzero(buffer,BUFFER_SIZE);
  63.      n = read(newsockfd,buffer,sizeof(buffer));
  64.      printf("Bytes read: %d\n",n);    
  65.  
  66.  
Any idea?
Regards.
Nov 3 '10 #1
Share this Question
Share on Google+
4 Replies


Oralloy
Expert 100+
P: 983
The problem is that your software is reading all of the data that are available on the socket at the time you called read.

The solution is to loop around "read", filling your incoming message buffer until you've read the complete message.

Something like this, assuming that you know that you are supposed to recieve DATA_LENGTH bytes:
Expand|Select|Wrap|Line Numbers
  1.      bzero(buffer,BUFFER_SIZE); 
  2.      int s=0;
  3.      while(DATA_LENGTH>s)
  4.      {
  5.           int n=read(newsockfd,buffer+s,DATA_LENGTH-s);
  6.           if(0==n)
  7.                ... error reading, complain and exit ...
  8.           s+=n;
  9.      }
  10.      printf("Bytes read:%d\n",s);
If you don't know how many bytes to expect, then you should have a sentinel character at the end of your message (e.g. '\n'). In which case, the loop looks like this:
Expand|Select|Wrap|Line Numbers
  1.      bzero(buffer,BUFFER_SIZE); 
  2.      char c=0;
  3.      for(int s=0;'\n'!=c;s++)
  4.      {
  5.           int n=read(newsockfd,&c,1);
  6.           if (1!=n)
  7.                ... error reading, complain and exit ...
  8.           buffer[s]=c;
  9.      }
  10.      printf("Bytes read:%d\n",strlen(buffer));
I've tried to keep your code style; hopefully it's understandable.

Network communications is pretty easy, if you take a few minutes to think about it. It's like waiting for someone typing at a keyboard.

Good luck.
Nov 3 '10 #2

P: n/a
Hi Oralloy,

Thank you for your response. I was hesitating to read data one byte at a time and thought that there might be performance issues using this technique.

Anyway, I am actually sending a formatted string from client to server. Therefore, can't use '\n' as a terminating character. I am now using '\0' to check if all the data has arrived on the server and it works fine. Thanks a lot.

Regards.
Nov 4 '10 #3

Oralloy
Expert 100+
P: 983
Waqas,

Glad to help. Now, just hope that there is never a NUL byte that actually should be in your data stream.

Still, if there is, you can implement an escape character easily enough. But that's an exercise for next week.

Good Luck! I hope the rest of your project goes smoothly.

Oralloy
Nov 4 '10 #4

P: n/a
Hi Oralloy,

Can you please shed some light on how to avoid a null character in data stream provided that we don't know the data length?

Regards,
Waqas Danish
Nov 5 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.