Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Linux
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Linux General discussion of programming the various flavors of Linux operating systems.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Linux section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 27th, 2004, 04:35 AM
Authorized User
Join Date: Jan 2004
Location: Pune, MAHARASHTRA, India.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ankur_vachhani
Default Problem in Socket Programming (BROKEN PIPE)


    I am getting error BROKEN PIPE in Linux C Sokcet programming.

     i have written simple client-server program using socket.
        pls help me.
          The Code of programm is as follows.

/////////////////////////// server.c////////////////////////////////
    int server_sockfd,client_sockfd,server_len,client_len;

    struct sockaddr_in server_address;
    struct sockaddr_in client_address;


    server_sockfd = socket(AF_INET,SOCK_STREAM,0);

    server_address.sin_family =AF_INET;
        server_address.sin_addr.s_addr=inet_addr("localhos t");
    bind(server_sockfd,(struct sockaddr *) &

        char ch;
        printf("\n Server Waiting...\n");

        client_sockfd=accept (server_sockfd,(struct sockaddr *) &
client_address,(socklen_t *)&client_len);





////////////////////// client.c////////////////////////////////////


    int sockfd,len,result;
    struct sockaddr_in address;
    char ch='a';




    result=connect(sockfd,(struct sockaddr *) &address,len);

        printf("Error Client");



    printf("char from server = %c",ch);


Two things to be rememeber always......

(1) Never tell all that u know
Reply With Quote
  #2 (permalink)  
Old April 12th, 2004, 08:12 AM
Registered User
Join Date: Apr 2004
Location: , , India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Ankur,

U seem to be a little confused with socket programming... in the connect system call u have to give the details regarding the server... hence u have to give the same port number as the one the server is listening to...

int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);

A piece of advice to u... one basic idea of using sockets is to let 2 machines communicate with each other. Hence u shudnot use "localhost" to specify the IP address of the server... good practice wud be to send the server address as the argument to the client process or even better wud be to have a configuration file which contains the details of the server like the address and the port it is running on....

This code work now.... (note the changes made is only the port number at the client side)

/* Server program */

    int server_sockfd, client_sockfd, server_len, client_len;

    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

// unlink("server_socket"); /* Don't kno y u've used this. so commented it :) */

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = INADDR_ANY;
    server_address.sin_port = 9700;
    server_len = sizeof (server_address);

    bind (server_sockfd, (struct sockaddr *) &server_address, server_len);

    listen (server_sockfd, 5);
    while (1) {
        char ch;
        printf ("\n Server Waiting...\n");

        client_len = sizeof (client_address);
        client_sockfd = accept (server_sockfd,
                (struct sockaddr *) &client_address, (socklen_t *) &client_len);



/* Client program */

    int sockfd, len;
    struct sockaddr_in address;
    char ch = 'a';

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY; /* Give the server address here when running on 2 different machines */
    address.sin_port = 9700;

    len = sizeof(address);

    if (connect (sockfd, (struct sockaddr *) &address, len) == -1) {
        printf("Error Client");

    write (sockfd, &ch, 1);
    read (sockfd, &ch, 1);

    printf ("char from server = %c", ch);
    close (sockfd);

Hope this helps u out in ur problem,

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
broken pipe error in implementing client -server octavius BOOK Beginning Linux Programming, 3rd Edition 1 December 17th, 2011 01:42 PM
Socket Programming lisamargaret C# 1 December 18th, 2007 10:20 AM
Socket Programming ashu_from_india General .NET 0 June 28th, 2005 12:02 PM
Socket Programming chinni Pro PHP 1 August 18th, 2003 03:59 PM

All times are GMT -4. The time now is 04:42 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.