Thread: Need some help with my chat room code !!

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    1

    Need some help with my chat room code !!

    This is my project in college. This is a 1 to 1 chatroom code which i have implemented. i need a smalll alteration in this. when a client types ""exit"" in the dialog box, the session should close automatically.

    This is the SERVER side code

    Code:
    /* ECE 545 Project - 1*/
    /* One to one chat room (server) using TCP */
    /* Group No. - 16 */
    
    
    #include	<stdio.h>
    #include	<sys/types.h>
    #include	<sys/socket.h>
    #include	<netinet/in.h>
    #include	<netdb.h>
    
    #define	SERVER_PORT	6667
    			/* Server Port Defined */
    #define	MAX_PENDING	5
    			/* Maximum number of clients */
    #define	MAX_LINE	256
    			/* Maximum String Length Defined */
    
    int
    main () 
    {
    	struct sockaddr_in sin;
    	char buf[MAX_LINE];
    	int len,pid,ptr;
    	int s, new_s;
    	
    /* Build address data structure	*/
    
    	bzero ((char *)&sin, sizeof(sin));
    	sin.sin_family = AF_INET;
    	sin.sin_addr.s_addr = INADDR_ANY;
    	sin.sin_port = htons(SERVER_PORT);
    
    /* Setup passive open socket */
    
    	if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 
    	{
    		perror("simplex-talk:socket");
    		exit(1);
    	}
    	if ((bind(s,(struct sockaddr *)&sin,sizeof(sin))) < 0) 
    	{
    		perror("simplex-talk:bind");
    		exit(1);
    	}
    	listen (s, MAX_PENDING);
    	
    /* Wait for a connection, then receive and print text	*/
    
    	while(1) 
    	 {
    	ptr=sizeof(sin);
    	 
    		if ((new_s = accept(s, (struct sockaddr *)&sin, &ptr)) < 0) 
    		{
    			perror("simplex-talk: accept");
    			exit(1);
    		}
    		
    /* Process is forked to recieve and send text */
    		
    		if((pid=fork())>0) 
    		{
    			while(len=recv (new_s, buf, sizeof(buf), 0))
    			fputs(buf, stdout);
    			 close(new_s);
    		}
    		
    		else 
    		{	
    	             while(fgets(buf,sizeof(buf),stdin))
    	             {
    			buf[MAX_LINE-1]='\0'; 
    			len=strlen(buf)+1;
    			send(new_s,buf,len,0); 
    		     } 
    		}	
    	}
    	
    		 }










    This is the CLIENT CODE




    Code:
    
    
    /* ECE 545 Project - 1*/
    /* One to one chat room (client) using TCP */
    
    
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    #define SERVER_PORT 6667
    			/* Server Port Defined */
    #define MAX_LINE 256
    				/* Maximum String Length Defined */	
    
    int
    						
    main (int argc, char *argv[])
    {
    	FILE *fp;
    	struct hostent	*hp;
    	struct	sockaddr_in sin;
    	char *host;
    	char buf[MAX_LINE];
    	int s,new_s;
    	int len,pid;
    	
    /* Checking the presence of host name */
    
    	if (argc == 2) 
    	{
    		host = argv[1];
    	} 
    	else 
    	{
    		fprintf(stderr,"usage:simplex-talk host\n"); 		exit (1);
    	}
    
    /* Translate host name into peer’s IP address*/
    
    	hp = gethostbyname(host);
    	if (!hp) 
    	{
    		fprintf(stderr,"simplex-talk:unknown host:%s\n",host);
    		exit (1);
    	}
    
    /* Build address data structure	*/
    
    	bzero((char *)&sin, sizeof(sin));
    	sin.sin_family = AF_INET;
    	bcopy (hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    	sin.sin_addr.s_addr = INADDR_ANY;
    	sin.sin_port = htons (SERVER_PORT);
    	
    /* Active opening of Socket */
    	
    	if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 
    	{
    		perror("simplex-talk:socket");
    		exit (1);
    	}
    	
    	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) 
    	{
    		perror("simplex-talk:connect");
    		close(s);
    		exit(1);
    	}
    
    /* Main loop : Get and send lines of text */
    /* Forking the process : To send and recieve the lines of text */
    	
    	if((pid=fork())>0)
    	{
    		while(fgets(buf,sizeof(buf), stdin))
    		{
    			buf[MAX_LINE-1]='\0';
    			len=strlen(buf)+1;
    			send (s, buf, len, 0) ;
    		}
    	}
    	else
    	{		
    		while(len=recv(s,buf,sizeof(buf),0))
    		fputs(buf,stdout);
    	        close(s);
    	}		 
    		
    		
    		
    }



    Thanx for the help

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use strcmp() to compare the string that you receive from the user with the text "exit", if it matches, close(socket);.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there's a number of bugs to be fixed.

    > while(len=recv (new_s, buf, sizeof(buf), 0))
    1. What if recv() returns -1 (like it does on error). Since you do nothing to clear the error, most likely this code would loop forever.
    2. recv() does NOT add a \0 to the end of whatever it receives, so your fputs() just wanders off into la-la land. I see your send() sends a \0, but you take no account of the fact that messages can be fragmented.

    > buf[MAX_LINE-1]='\0';
    Even if fgets() fills the buffer, the last character in the buffer will be \0. It is an unnecessary line of code.

    > send (s, buf, len, 0) ;
    Same as recv(), send calls can fragment (do not assume that all of your data is sent on the first call). Also, pay attention to it's return result.

    > if((pid=fork())>0)
    If it returns -1, then you really shouldn't just run one half of the protocol.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Chat room software.
    By adrianxw in forum Tech Board
    Replies: 4
    Last Post: 01-14-2004, 07:59 PM
  3. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 01:57 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM