?
Solved

client server chat - broken pipe

Posted on 2003-03-30
6
Medium Priority
?
328 Views
Last Modified: 2010-08-05
0
Comment
Question by:jarmstro12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 

Author Comment

by:jarmstro12
ID: 8236050
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8239804
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
 
LVL 5

Accepted Solution

by:
Kocil earned 400 total points
ID: 8239872
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
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.

 

Author Comment

by:jarmstro12
ID: 8240759
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
 

Author Comment

by:jarmstro12
ID: 8241083
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
 

Author Comment

by:jarmstro12
ID: 8242671
thanks thats what i asked for
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question