Thread: chat server error

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Unhappy chat server error

    hi,
    i am making a chat server....but the problem is only one way chat occurs....the connection gets lost on the client side....cud u plz let me know where i am going wrong
    thanks in advance

    Code:
    
    ///   CLIENT   
    
    
    int main(int argc, char *argv[])
    {
    	printf("This is the client program\n");
    
    	int sockfd;
    	int len, rc ;
    	int result;
    	struct sockaddr_in address;
            char arr[]={'\0'};
    printf("enter the string\n");
    gets(arr);
    
    int i;
    int length=strlen(arr);
    printf("len of the arr is %d\n", length);
    
      
    
       //Create socket for client.
    	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	if (sockfd == -1) { 
    		perror("Socket create failed.\n") ; 
    		return -1 ; 
    	} 
    	
    	//Name the socket as agreed with server.
    	address.sin_family = AF_INET;
    	address.sin_addr.s_addr = inet_addr("127.0.0.1");
    	address.sin_port = htons(7764);
    	len = sizeof(address);
    
    	result = connect(sockfd, (struct sockaddr *)&address, len);
    	if(result == -1)
    	{
    		perror("Error has occurred");
    		exit(-1);
    	}
    
    	
    		//Read and write via sockfd
    		rc = write(sockfd, &length, 20);
    		printf("write rc = %d\n", rc ) ; 
    		if (rc == -1) 
    return -1; 
    		
    		read(sockfd, &length, 20);
    		printf("Char from server = %c\n", arr);
    		//if (ch == 'A') sleep(5) ;  // pause 5 seconds 
    	
    	close(sockfd);
    
    	exit(0);
    }
    
    
    // SERVER
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <sys/un.h>
    
    int main()
    {
     printf("This is the server program\n");
    //naming variables
     int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in  server_address;
    struct sockaddr_in  client_address;
    char ch='B';
    int result;
    int res;
    int rc;
    // removing old sockets
    unlink("server_socket");
    server_sockfd=socket(AF_INET,SOCK_STREAM,0);
    if (server_sockfd == -1) { 
    		perror("Socket create failed.\n") ; 
    		return -1 ; 
    	} 
    //naming of socket
    server_address.sin_family = AF_INET;
    	server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
    	server_address.sin_port = htons(7764);
    	server_len = sizeof(server_address);
    
    	result = bind(server_sockfd, (struct sockaddr *)&server_address,server_len);
    	if(result == -1)
    	{
    		perror("Error has occurred");
    		exit(-1);
    	}
    res=listen(server_sockfd, 5);
    if(res==-1)
    {
    perror("error");
    exit(-1);
    }
    client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,client_len);
    
    
    read(client_sockfd,&ch,1);
    ch++;
    while ( ch < 'z') {
    
    		//Read and write via sockfd
    		rc = read(client_sockfd, &ch, 1);
    		printf("write rc\n", rc ) ; 
    		if (rc == -1) break ; 
    		
    		write(client_sockfd, &ch, 1);
    		printf("Char from server = %c\n", ch);
    		
    	} 
    	close(client_sockfd);
    
    	exit(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    >         char arr[]={'\0'};
    > printf("enter the string\n");
    > gets(arr);
    How many characters can you store in this array - answer a) 0, b) 1, c) as many as I like

    SourceForge.net: Gets - cpwiki

    Next, your indentation is so poor that no one really wants to read more than a few lines of it. I spotted the first easy problem and stopped looking.
    http://sourceforge.net/apps/mediawik...le=Indentation

    Finally, drop the "cud u plz " sms chat
    How To Ask Questions The Smart Way
    You have a proper keyboard, and almost certainly the ability to have a spell checker in your browser.
    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.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
        int length=strlen(arr);
    ...
    ...
        //Read and write via sockfd
        rc = write(sockfd, &length, 20);
        printf("write rc = &#37;d\n", rc ) ; 
        if (rc == -1) 
            return -1; 
    		
        read(sockfd, &length, 20);
        printf("Char from server = %c\n", arr);
        //if (ch == 'A') sleep(5) ;  // pause 5 seconds
    What documentation are you reading as far as the parameters for read/write?

    Normally the second parameter takes the buffer being written:

    ssize_t read(int fd, void *buf, size_t count);
    ssize_t write(int fd, const void *buf, size_t count);

    You are passing the address of an integer as your buffer?

    This occurs both in server and client btw...so amazed you got any text(legible) transmitted whatsoever..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. udp chat server
    By cemalinanc in forum Linux Programming
    Replies: 5
    Last Post: 11-22-2010, 06:41 AM
  2. Chat udp server/client
    By Phoenix_Rebirth in forum C Programming
    Replies: 1
    Last Post: 11-17-2008, 12:30 PM
  3. chat server problem
    By sPyd3r in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-03-2004, 04:54 PM
  4. Chat Client/Server Problems
    By _Cl0wn_ in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 11:38 AM
  5. Client/Server chat program! Please Help
    By Silverdream in forum C Programming
    Replies: 2
    Last Post: 03-21-2002, 06:34 AM