Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Linux
Password Reminder
Register
Register | 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 27th, 2004, 03: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)

HI

    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////////////////////////////////
main()
{
    int server_sockfd,client_sockfd,server_len,client_len;

    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

    unlink("server_socket");

    server_sockfd = socket(AF_INET,SOCK_STREAM,0);

    server_address.sin_family =AF_INET;
        server_address.sin_addr.s_addr=inet_addr("localhos t");
    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);


    read(client_sockfd,&ch,1);
    ch++;

    write(client_sockfd,&ch,1);
    close(client_sockfd);

    }

}

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

main()
{


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

    sockfd=socket(AF_INET,SOCK_STREAM,0);

    address.sin_family=AF_INET;
    address.sin_addr.s_addr=inet_addr("localhost");
    address.sin_port=9734;

    len=sizeof(address);


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

    if(result==-1)
    {
        printf("Error Client");

    }


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

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



}


__________________
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
Default

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 */

main()
{
    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);

        read(client_sockfd,&ch,1);
        ch++;

        write(client_sockfd,&ch,1);
        close(client_sockfd);
    }
}


/* Client program */

main()
{
    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,
Cherub

Reply With Quote
Reply


Thread Tools
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 12:42 PM
Socket Programming lisamargaret C# 1 December 18th, 2007 09: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:29 PM.


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