473,508 Members | 4,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem while reading through socket

57 New Member
Hi

I am developing a chat server program.
I am not able to get client name from this line

read(connfd,info[j].name,MAXLINE);

When i remove read(connfd,no,3);
It is reading name.. Can somebody solve my problem. I need both. Please help

///////////server
Expand|Select|Wrap|Line Numbers
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include <sys/shm.h>    
  9. #include "pthread.h"
  10. #define MAXLINE 4096
  11. #define LISTENQ 32
  12. #define SERVPORT 3002
  13. #define KEY IPC_PRIVATE
  14. #define NO_OF_THREADS 10
  15. char buff[MAXLINE];
  16. char no[2];
  17. int no_of_child=-1;
  18. int iden[4];
  19. struct cli_info
  20.  {
  21.     int read_from;
  22.     int write_to;
  23.     char name[50];
  24.     int myid;
  25.  };
  26.  
  27. struct cli_info info[NO_OF_THREADS];
  28. int j=0;
  29. int main(int argc,char **argv)
  30.  {
  31.     int listenfd,connfd;
  32.     struct sockaddr_in servaddr,cliaddr;
  33.     pthread_t thread[3];
  34.     socklen_t len;
  35.     pid_t childpid;
  36.     void str_echo();
  37.     void print_names();
  38.     if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
  39.         printf("\n\nError in creating socket");
  40.     bzero(&servaddr,sizeof(servaddr));
  41.     servaddr.sin_family=AF_INET;
  42.     servaddr.sin_port=htons(SERVPORT);
  43.     if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  44.         printf("\n\nError in bind");
  45.     if(listen(listenfd,LISTENQ)<0)
  46.         printf("\n\nError in listen");
  47.     for( ; ; )
  48.      {
  49.         len=sizeof(cliaddr);
  50.         if((connfd=iden[++no_of_child]=accept(listenfd,(struct sockaddr*)&cliaddr,&len))<0)
  51.             printf("Error in connect");
  52.             info[j].myid=connfd;
  53.             read(connfd,info[j].name,MAXLINE);
  54.             //fflush(stdin);
  55.             //fflush(stdout);
  56.             read(connfd,no,3);            
  57.             //fflush(stdin);
  58.             //fflush(stdout);
  59.             info[j].write_to=atoi(no);
  60.             //write(connfd,info[j].name,MAXLINE);
  61.         //    print_names(connfd);
  62.             pthread_create(&thread[j],NULL,(void *)&str_echo,(void *)&info[j]);
  63.             j++;
  64.                 //exit(0);
  65.             //str_echo(connfd);
  66.      }
  67.  }
  68. /*void print_names(int connfd)
  69.  {
  70.     int i=0;
  71.     for(i=0;i<j;i++)
  72.         write(connfd,"Hanji",MAXLINE);
  73.  }*/
  74. void str_echo(void *arg)
  75.  {
  76.     struct cli_info *my_data;
  77.     my_data=(struct cli_info*)arg;
  78.     int i;
  79.     int writeid=info[my_data->write_to].myid;
  80. //    char currname[55];
  81. //    strcpy(currname,my_data->name);
  82. //    strcpy(currname,": ");
  83.  
  84.     void clear_buffer();
  85.     clear_buffer();
  86.     for( ; ; )
  87.      {
  88.                     if(read(my_data->myid,buff,MAXLINE)==0)
  89.                      {
  90.                         printf("\n\nError in reading");
  91.                      }
  92.                     //write(writeid,currname,strlen(currname));
  93.                     //fflush(stdin);        
  94.                     //fflush(stdout);
  95.                     write(writeid,buff,MAXLINE);
  96.                     clear_buffer();
  97.      }            
  98.  
  99.  }
  100. void clear_buffer()
  101.  {
  102.     int i;
  103.     for(i=0;i<MAXLINE;i++)
  104.      {
  105.         buff[i]='\0';
  106.      }
  107.  }
  108.  
//////////////Client
Expand|Select|Wrap|Line Numbers
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <sys/select.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #define MAXLINE 4096
  10. #define LISTENQ 32
  11. #define SERVPORT 3002
  12. char send_to[MAXLINE],recv_from[MAXLINE];
  13.  
  14. int main(int argc,char **argv)
  15.  {
  16.     void echo_cli(FILE *,int);
  17.     int sockfd;
  18.     struct sockaddr_in servaddr;
  19.     //printf("Heelo");
  20.     bzero(&servaddr,sizeof(servaddr));
  21.     if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
  22.         printf("\n\nError in creating socket");
  23.     servaddr.sin_family=AF_INET;
  24.     servaddr.sin_port=htons(SERVPORT);
  25.     if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
  26.         printf("\n\nIncorrect Address");
  27.     if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  28.         printf("\n\nError in connect");
  29.     fputs("Enter your name  ",stdout);
  30.     fgets(send_to,MAXLINE,stdin);
  31.     //fputs(send_to,stdout);
  32.     write(sockfd,send_to,strlen(send_to));
  33.     //fflush(stdin);
  34.     //fflush(stdout);
  35.     fputs("Enter the client number  ",stdout);
  36.     fgets(send_to,MAXLINE,stdin);
  37.     write(sockfd,send_to,strlen(send_to));
  38.     //fflush(stdin);
  39.     //fflush(stdout);
  40.  
  41.     echo_cli(stdin,sockfd);
  42.     close(sockfd);
  43.  }
  44. void clear_send_buffer()
  45.  {
  46.     int i;
  47.     for(i=0;i<MAXLINE;i++)
  48.      {
  49.         send_to[i]='\0';
  50.      }
  51.  }
  52. void clear_recv_buffer()
  53.  {
  54.     int i;
  55.     for(i=0;i<MAXLINE;i++)
  56.      {
  57.         recv_from[i]='\0';
  58.      }
  59.  }
  60.  
  61. /*
  62. void echo_cli(FILE *fp,int sockfd)
  63.  {
  64.     int maxfdp1;
  65.     fd_set rset;
  66.     FD_ZERO(&rset);
  67.     while(fgets(send_to,MAXLINE,fp)!=NULL)
  68.      {
  69.         write(sockfd,send_to,strlen(send_to));
  70.         if(read(sockfd,recv_from,MAXLINE)==0)
  71.             printf("\n\nError in reading");
  72.         fputs(recv_from,stdout);
  73.         clear_buffer();
  74.      }
  75.  }*/
  76. int max(int a,int b)
  77.  {
  78.     if(a>=b)
  79.         return a;
  80.     else 
  81.         return b;
  82.  }
  83. void echo_cli(FILE *fp,int sockfd)
  84.  {
  85.     int maxfdp1;
  86.     char cli_no[MAXLINE];
  87.     fd_set rset;
  88.     FD_ZERO(&rset);
  89.     clear_recv_buffer();
  90.     clear_send_buffer();
  91.     for( ; ; )
  92.      {
  93.         //printf("hello");
  94.         FD_SET(fileno(fp),&rset);
  95.         FD_SET(sockfd,&rset);
  96.         maxfdp1=max(fileno(fp),sockfd)+1;
  97.         select(maxfdp1,&rset,NULL,NULL,NULL);
  98.         if(FD_ISSET(sockfd,&rset))
  99.           {
  100.                     if(read(sockfd,recv_from,MAXLINE)==0)
  101.                      {
  102.                         //FD_CLR(sockfd,&rset);
  103.                         printf("\n\nError in reading");
  104.                      }
  105.                     fputs(recv_from,stdout);
  106.                     clear_recv_buffer();
  107.          }
  108.         if(FD_ISSET(fileno(fp),&rset))
  109.          {            
  110.             if(fgets(send_to,MAXLINE,fp)!=NULL)
  111.                 write(sockfd,send_to,strlen(send_to));
  112.             else
  113.                 printf("\n\nError in reading");
  114.             clear_send_buffer();
  115.          }
  116.      }            
  117.  }
  118.  
May 26 '11 #1
2 2555
Banfa
9,065 Recognized Expert Moderator Expert
How do you know that it is the read(connfd,info[j].name,MAXLINE); and not the read(connfd,no,3);?

The thing to remember is that a TCP socket is a byte stream not a packetised stream. That is just because you sent the name and then then number in 2 calls to write that is not necessarily how it will turn up at the server.

At the extreme each byte may turn up 1 at a time in separate calls to read and at the other extreme they may all turn up in a single call to read.

Suppose the name was "Client1" and the number "1". read(connfd,info[j].name,MAXLINE); attempts to read 4096 bytes, but your are sending 8, that could easily all turn up in a single read call leaving the read(connfd,no,3); hanging on data that wont arrive because it has already been read.

You can not hang you server on mimicking the same write calls the client makes with read calls. You will be doomed to failure.

The server needs to read the data as bytes (because it is a byte stream) and then parse those bytes into the field values it is expecting (a name and an ASCII formatted number). You need some way in the data to tell where the name stops and the number starts.
May 31 '11 #2
puneetsardana88
57 New Member
Thank You Banfa. I got your point. Thanks a lot. Will keep this in mind.
Jun 11 '11 #3

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

Similar topics

2
3016
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
1
2748
by: Boris Wilhelms | last post by:
Hello all, at first, sorry for my bad English, I’ll give my best  We have a strange problem reading Text- and VarChar-Fields. Our configuration: -Windows 2003 Server -MySQL Server 3.23.36...
6
1767
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
2
702
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
1
3601
by: Brad | last post by:
I'm having a problem reading a resource stream using the following syntax: Dim resStream As System.IO.TextReader = New _ ...
0
1240
by: Manfred Braun | last post by:
Hi All, I have a problem reading queue-messages async. My QueueReader has a Start() and a Stop() method and if my app starts, it calls Start(). The problem is, that there are possibly several...
4
3172
by: andreas.fabri | last post by:
I have a problem reading integers separated by commas with VC8 This program: ___________________________ // read.C #include <iostream> int main()
3
2148
by: Carl | last post by:
Recently I got a problem with LINUX C socket programming. I want to get how many clients are waiting for being accepted in the socket server pending queue . I'm trying to find the answer from the...
0
3548
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
8
3286
by: Can2002 | last post by:
I've been trying to put together an application to change channel on a media streaming server. The server is able to issue IR commands to its attached equipment using LIRC, with commands being...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.