Problem while reading through socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    Problem while reading through socket

    Hi

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

    read(connfd,inf o[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
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/shm.h>	
    #include "pthread.h"
    #define MAXLINE 4096
    #define LISTENQ 32
    #define SERVPORT 3002
    #define KEY IPC_PRIVATE
    #define NO_OF_THREADS 10
    char buff[MAXLINE];
    char no[2];
    int no_of_child=-1;
    int iden[4];
    struct cli_info
     {
    	int read_from;
    	int write_to;
    	char name[50];
    	int myid;
     };
    
    struct cli_info info[NO_OF_THREADS];
    int j=0;
    int main(int argc,char **argv)
     {
    	int listenfd,connfd;
    	struct sockaddr_in servaddr,cliaddr;
    	pthread_t thread[3];
    	socklen_t len;
    	pid_t childpid;
    	void str_echo();
    	void print_names();
    	if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
    		printf("\n\nError in creating socket");
    	bzero(&servaddr,sizeof(servaddr));
    	servaddr.sin_family=AF_INET;
    	servaddr.sin_port=htons(SERVPORT);
    	if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
    		printf("\n\nError in bind");
    	if(listen(listenfd,LISTENQ)<0)
    		printf("\n\nError in listen");
    	for( ; ; )
    	 {
    		len=sizeof(cliaddr);
    		if((connfd=iden[++no_of_child]=accept(listenfd,(struct sockaddr*)&cliaddr,&len))<0)
    			printf("Error in connect");
    			info[j].myid=connfd;
    			read(connfd,info[j].name,MAXLINE);
    			//fflush(stdin);
    			//fflush(stdout);
    			read(connfd,no,3);			
    			//fflush(stdin);
    			//fflush(stdout);
    			info[j].write_to=atoi(no);
    			//write(connfd,info[j].name,MAXLINE);
    		//	print_names(connfd);
    			pthread_create(&thread[j],NULL,(void *)&str_echo,(void *)&info[j]);
    			j++;
    				//exit(0);
    			//str_echo(connfd);
    	 }
     }
    /*void print_names(int connfd)
     {
    	int i=0;
    	for(i=0;i<j;i++)
    		write(connfd,"Hanji",MAXLINE);
     }*/
    void str_echo(void *arg)
     {
    	struct cli_info *my_data;
    	my_data=(struct cli_info*)arg;
    	int i;
    	int writeid=info[my_data->write_to].myid;
    //	char currname[55];
    //	strcpy(currname,my_data->name);
    //	strcpy(currname,": ");
    
    	void clear_buffer();
    	clear_buffer();
    	for( ; ; )
    	 {
    					if(read(my_data->myid,buff,MAXLINE)==0)
    					 {
    						printf("\n\nError in reading");
    					 }
    					//write(writeid,currname,strlen(currname));
    					//fflush(stdin);		
    					//fflush(stdout);
    					write(writeid,buff,MAXLINE);
    					clear_buffer();
    	 }			
    
     }
    void clear_buffer()
     {
    	int i;
    	for(i=0;i<MAXLINE;i++)
    	 {
    		buff[i]='\0';
    	 }
     }
    //////////////Client
    Code:
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/select.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #define MAXLINE 4096
    #define LISTENQ 32
    #define SERVPORT 3002
    char send_to[MAXLINE],recv_from[MAXLINE];
    
    int main(int argc,char **argv)
     {
    	void echo_cli(FILE *,int);
    	int sockfd;
    	struct sockaddr_in servaddr;
    	//printf("Heelo");
    	bzero(&servaddr,sizeof(servaddr));
    	if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
    		printf("\n\nError in creating socket");
    	servaddr.sin_family=AF_INET;
    	servaddr.sin_port=htons(SERVPORT);
    	if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
    		printf("\n\nIncorrect Address");
    	if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
    		printf("\n\nError in connect");
    	fputs("Enter your name  ",stdout);
    	fgets(send_to,MAXLINE,stdin);
    	//fputs(send_to,stdout);
    	write(sockfd,send_to,strlen(send_to));
    	//fflush(stdin);
    	//fflush(stdout);
    	fputs("Enter the client number  ",stdout);
    	fgets(send_to,MAXLINE,stdin);
    	write(sockfd,send_to,strlen(send_to));
    	//fflush(stdin);
    	//fflush(stdout);
    
    	echo_cli(stdin,sockfd);
    	close(sockfd);
     }
    void clear_send_buffer()
     {
    	int i;
    	for(i=0;i<MAXLINE;i++)
    	 {
    		send_to[i]='\0';
    	 }
     }
    void clear_recv_buffer()
     {
    	int i;
    	for(i=0;i<MAXLINE;i++)
    	 {
    		recv_from[i]='\0';
    	 }
     }
    
    /*
    void echo_cli(FILE *fp,int sockfd)
     {
    	int maxfdp1;
    	fd_set rset;
    	FD_ZERO(&rset);
    	while(fgets(send_to,MAXLINE,fp)!=NULL)
    	 {
    		write(sockfd,send_to,strlen(send_to));
    		if(read(sockfd,recv_from,MAXLINE)==0)
    			printf("\n\nError in reading");
    		fputs(recv_from,stdout);
    		clear_buffer();
    	 }
     }*/
    int max(int a,int b)
     {
    	if(a>=b)
    		return a;
    	else 
    		return b;
     }
    void echo_cli(FILE *fp,int sockfd)
     {
    	int maxfdp1;
    	char cli_no[MAXLINE];
    	fd_set rset;
    	FD_ZERO(&rset);
    	clear_recv_buffer();
    	clear_send_buffer();
    	for( ; ; )
    	 {
    		//printf("hello");
    		FD_SET(fileno(fp),&rset);
    		FD_SET(sockfd,&rset);
    		maxfdp1=max(fileno(fp),sockfd)+1;
    		select(maxfdp1,&rset,NULL,NULL,NULL);
    		if(FD_ISSET(sockfd,&rset))
     		 {
    					if(read(sockfd,recv_from,MAXLINE)==0)
    					 {
    						//FD_CLR(sockfd,&rset);
    						printf("\n\nError in reading");
    					 }
    					fputs(recv_from,stdout);
    					clear_recv_buffer();
    		 }
    		if(FD_ISSET(fileno(fp),&rset))
    		 {			
    			if(fgets(send_to,MAXLINE,fp)!=NULL)
    				write(sockfd,send_to,strlen(send_to));
    			else
    				printf("\n\nError in reading");
    			clear_send_buffer();
    		 }
    	 }			
     }
    Last edited by Niheel; Jun 11 '11, 08:19 PM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    How do you know that it is the read(connfd,inf o[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,inf o[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.

    Comment

    • puneetsardana88
      New Member
      • Aug 2009
      • 57

      #3
      Thank You Banfa. I got your point. Thanks a lot. Will keep this in mind.

      Comment

      Working...