• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

client server chat - broken pipe

0
jarmstro12
Asked:
jarmstro12
  • 4
1 Solution
 
jarmstro12Author Commented:
When running this chat code the server sometimes passes keyboard input to client but gives a broken pipe error.  The client may pass input to server but always gives a segmentation fault.
I compile with: gcc -lnsl -lsocket -o server server.c
                 gcc -lnsl -lsocket -o client client.c
thanks ja

    /*SERVER:*/                
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

#define PORT           0x5050
#define DIRSIZE      8192

main()
{
        char     dir[DIRSIZE];  /* used for incomming and
                         outgoing data */
     int       sd, sd_current, cc, fromlen, tolen;
     int       addrlen;
     struct   sockaddr_in sin;
     struct   sockaddr_in pin;
 
     /* get an internet domain socket */
     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
          perror("socket");
          exit(1);
     }

     /* complete the socket structure */
     memset(&sin, 0, sizeof(sin));
     sin.sin_family = AF_INET;
     sin.sin_addr.s_addr = INADDR_ANY;
     sin.sin_port = htons(PORT);

     /* bind the socket to the port number */
     if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
          perror("bind");
          exit(1);
     }
     /* show that we are willing to listen */
     if (listen(sd, 5) == -1) {
          perror("listen");
          exit(1);
     }
     /* wait for a client to talk to us */
     if ((sd_current = accept(sd, (struct sockaddr *)  &pin, &addrlen)) == -1) {
          perror("accept");
          exit(1);
     }

      /* acknowledge the message, reply */
 do{
      printf("Message to send: ");
      gets(dir);
      send(sd_current,dir,strlen(dir),0);
     
      /* get a message from the client */  
      recv(sd_current,dir,strlen(dir),0);
      printf("Message recieved: %s\n",dir);
   }
 
      while(strcmp(dir,"/q")); /*user 'q' to quit*/
   
      /* close up both sockets */
     close(sd_current); close(sd);
       
       /* give client a chance to shutdown */
        sleep(1);
}

 
/*client*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

#define PORT       0x5050
             /* My server's name*/
#define HOST        "galatia"
#define DIRSIZE     8192

main(argc, argv)
int argc; char **argv;
{
        char hostname[100];
     char    dir[DIRSIZE];
     int     sd;
     struct sockaddr_in sin;
     struct sockaddr_in pin;
     struct hostent *hp;

        strcpy(hostname,HOST);
        if (argc>2)
            { strcpy(hostname,argv[2]); }

     /* go find out about the desired host machine */
     if ((hp = gethostbyname(hostname)) == 0) {
          perror("gethostbyname");
          exit(1);}

     /* fill in the socket structure with host information */
     memset(&pin, 0, sizeof(pin));
     pin.sin_family = AF_INET;
     pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
     pin.sin_port = htons(PORT);

     /* grab an Internet domain socket */
     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
          perror("socket");
          exit(1);}

     /* connect to PORT on HOST */
     if (connect(sd,(struct sockaddr *)  &pin, sizeof(pin)) == -1) {
          perror("connect");
          exit(1);}

 do{
      recv(sd,dir,DIRSIZE,0);
      printf("Message recieved: %s\n",dir);
      if (strcmp(dir,"/q")){
      printf("Message to send: ");
      gets(dir);
      send(sd,argv[1],strlen(argv[1]),0);
      }
   }while (strcmp(dir,"/q"));
   close(sd);
}
0
 
sarda_rameshCommented:
please try to debug ur code using gdb .. the debugger for C codes and see on which line it gets the segmentation fault

regards
ramesh
0
 
KocilCommented:
At the server
   
     /* get a message from the client */  
     // recv(sd_current,dir,strlen(dir),0); change to
     recv(sd_current,dir,DIRSIZE,0);
     printf("Message recieved: %s\n",dir);
 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jarmstro12Author Commented:
Kocil -
I maded the change and no broken pipe until client fails that seem to be normal it guess.
I inputed server with "wake up" then client 'echo' as you see server repeated itself with "wake up" instead of 'echo' & client croaks.

/server/ - first send
Message to send: wake up
Message recieved: wake up
Message to send:

/client/
Message recieved: wake up
Message to send: echo
Segmentation Fault
0
 
jarmstro12Author Commented:
Kocil -
I maded the change and no broken pipe until client fails that seem to be normal it guess.
I inputed server with "wake up" then client 'echo' as you see server repeated itself with "wake up" instead of 'echo' & client croaks.

/server/ - first send
Message to send: wake up
Message recieved: wake up
Message to send:

/client/
Message recieved: wake up
Message to send: echo
Segmentation Fault
0
 
jarmstro12Author Commented:
thanks thats what i asked for
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now