Thread: HELP with socket!

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    HELP with socket!

    Hi all! I'm writing an application client-server in c using sockets AF_UNIX. Once the two processes are connected they enter in these infinity loops:

    server ==> receive_message-send_message

    client ==> send_message_receive_message

    The problem is that i get Broken pipe.
    Any idea?

    cheers

  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
    Not without seeing some code.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    "Broken pipe" happens when either side attempts to send data when the connection is "broken," i.e. shut down on one side.

    Either you are not connection correctly to the server, or the connection is getting reset somewhere in between. Without code we can't help.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Sure! I'm implementing a library that uses sockets so there are funcionts and structures defined by me, i will paste the most significant part:

    Code:
    //Server
    in main() {
            struct sockaddr_un sa;
    	message_t msg_s, msg_r;
    	int res, i=0, sd1, sd2;
    	msg_s.type=MSG_MKAGENDA;
    	msg_s.length=19;
    	ec_null1( (msg_r.buffer=(char*)malloc(sizeof(char)*19)), 0 )
    	ec_null1( (msg_s.buffer=(char*)malloc(sizeof(char)*19)), 0 )
    	sprintf(msg_s.buffer, "The server says hi!");
            //The following code is executed by a function
            /* FROM HERE */
            strncpy(sa.sun_path, "tmp/sock", UNIX_PATH_MAX);
    	sa.sun_family=AF_UNIX;
    	ec_neg1( (sd1=socket(AF_UNIX, SOCK_STREAM, 0)), 0 )
            /* TO HERE */
            //The following code is executed by a function
            /* FRO HERE */
    	ec_neg1( bind(sd1, (struct sockaddr*)&sa, sizeof(sa)), 0 )
    	ec_neg1( listen(sd1, SOMAXCONN), 0 )
    	ec_neg1( (sd2=accept(s, NULL, 0)), 0 )
            /* TO HERE */
    	while (1) {
    		if (-1==read(sd2, msg_r.buffer,  19)<0) {
    			perror("No message received from the client");
    			pthread_exit(&fail);
    		}
    		if (-1==write(sd2, msg_s.buffer, msg_s.length)){
    				perror("No message received from the client");
    				pthread_exit(&fail);
    		}
    		else printf("Server got: %s\n", msg_r.buffer);
    		sleep(1);
    	}
    }
    
    //Client
    
    int main(int argc, char *argv[]) {
            struct sockaddr_un sa;
    	message_t msg_s, msg_r;
    	int res, i, sd=0;
    	msg_s.type=MSG_MKAGENDA;
    	msg_s.length=19;
    	ec_null1( (msg_r.buffer=(char*)malloc(sizeof(char)*19)), 0 )
    	ec_null1( (msg_s.buffer=(char*)malloc(sizeof(char)*19)), 0 )
    	sprintf(msg_s.buffer, "The client says hi!");
            //The following code is executed by a function
            /* FROM HERE */
            strncpy(sa.sun_path, "tmp/sock", UNIX_PATH_MAX);
    	sa.sun_family AF_UNIX;
    	ec_neg1( (sd=socket(AF_UNIX, SOCK_STREAM, 0)), 0 )
            /* TO HERE */
            //The following code is executed by a function
            /* FROM HERE */
    	while (connect(sd, (struct sockaddr*)&sa, sizeof(sa))==-1)
    		if (errno==ENOENT) {
    			sleep(1);
    			continue;
    		}
                    else perror("Error!");
             /* TO HERE */
             	
                    do {
    			if (write (sd, msg_s.buffer, msg_s.length) == -1)
                                 perror("Error in write() client side");
    			if (-1==read(sd, msg_r.buffer,  19))
                                 perror("Error in read() client side");
    			sleep(1);
    			}
    			while (1);
    }
    The functions i talk about in the comments are written in a file.c where the structure sockaddr_un sa is declared, so i just put the declaration of it in both of them (client-server) to be formal!
    i'm sorry if there is some banal mistake, the code has been adapted to be readable!

    Thanks!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sprintf(msg_s.buffer, "The client says hi!");
    Well this overflows your buffer.
    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.

  6. #6
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Ok i added one byte more! It's the same,...any idea?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > in main()
    Does it compile?

    > sa.sun_family AF_UNIX;
    Does it compile?

    > the code has been adapted to be readable!
    Lemme guess, it's an unreadable mess and you butchered it to post it.

    Dunno, there's not really enough information, and very little confidence that the actual problem has been faithfully represented in your post.
    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.

  8. #8
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    No the thing is that i should have post all the structures and the functions that works with sockets cause the main works trough them.... And yes the code compiles. Otherwise i wouldn't ask here why it gives me a broken pipe error. don't you think?

  9. #9
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I think he meant, how can it compile? Atleast that's my question, 'cause what's
    Code:
    in main()
    ? ain't it supposed to be int main() ?
    Currently research OpenGL

  10. #10
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    So, The original code just calls functions that i wrote in separate files. Here i wrote what happen using library functions so sorry there is some mistake (i did lot of cut and paste), but the code compiles.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So how do we know you didn't omit some vital detail in all the editing for brevity?
    Not that the result is that neat to begin with.

    You haven't even posted the actual error messages.
    For instance is it
    "Error in read() client side" - broken pipe

    Or something else?
    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.

  12. #12
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Ok i will try to explain it better:
    I wrote some function in a file.c where a variable of type struct sockaddr is declared as well.

    Code:
    serverChannel_t createServerChannel(const char* path) {
    	serverChannel_t sd;			/* socket descriptor */
    	ec_null1( path, EINVAL )
    	if (strlen(path) > UNIX_PATH_MAX) {
    		errno=EINVAL;
    		return SOCKNAMETOOLONG;
    	}
    	strncpy(sa.sun_path, path, UNIX_PATH_MAX);
    	sa.sun_family=AF_UNIX;
    	ec_neg1( (sd=socket(AF_UNIX, SOCK_STREAM, 0)), 0 )
    	return sd;
    }
    
    channel_t acceptConnection(serverChannel_t s) {
    	channel_t c_sd;		/* client socket descriptor */
    	ec_neg1( s-1, EINVAL )
    	ec_neg1( bind(s, (struct sockaddr*)&sa, sizeof(sa)), 0 )
    	ec_neg1( listen(s, SOMAXCONN), 0 ) /* marks s to accept at most SOMAXCONN clients */
    	ec_neg1( (c_sd=accept(s, NULL, 0)), 0 )
    	return c_sd;
    }
    These two above are called bye the server in the main, so basically the struct sockaddr_un is not used at all in the main (could this be a problem?).
    This is the function the client calls:

    Code:
    channel_t openConnection(const char* path) {
    	channel_t ch;
    	ec_neg1( (ch=(channel_t)createServerChannel(path)), 0)
    	while (connect((int)ch, (struct sockaddr*)&sa, sizeof(sa))==-1)
    		if (errno==ENOENT) {
    			sleep(1);
    			continue;
    		}
    	return ch;
    }
    channel_t and serverChannel_t are integers. So the code become:
    Code:
    //Server
    int main() {
           	message_t msg_s, msg_r;
    	int res, i=0;
            serverChannel_t sd1;
            channel_t sd2;
    	msg_s.type=MSG_MKAGENDA; 
    	msg_s.length=20;
    	ec_null1( (msg_r.buffer=(char*)malloc(sizeof(char)*20)), 0 )
    	ec_null1( (msg_s.buffer=(char*)malloc(sizeof(char)*20)), 0 )
    	sprintf(msg_s.buffer, "The server says hi!");
            ec_neg1( (s1=createServerChannel("tmp/socket")), 0 )
            ec_neg1( (s2=acceptConnection(s1), 0 )
    	while (1) {
    		if (-1==read(sd2, msg_r.buffer,  20)<0) {
    			perror("No message received from the client");
    			pthread_exit(&fail);
    		}
    		if (-1==write(sd2, msg_s.buffer, msg_s.length)){
    				perror("No message written to the client");
    				pthread_exit(&fail);
    		}
    		else printf("Server got: %s\n", msg_r.buffer);
    		sleep(1);
    	}
    }
    
    //Client
    
    int main(int argc, char *argv[]) {
            message_t msg_s, msg_r;
    	int res, i;
            channel_t sd;
    	msg_s.type=MSG_MKAGENDA;
    	msg_s.length=20;
    	ec_null1( (msg_r.buffer=(char*)malloc(sizeof(char)*20)), 0 )
    	ec_null1( (msg_s.buffer=(char*)malloc(sizeof(char)*20)), 0 )
    	sprintf(msg_s.buffer, "The client says hi!");
            ec_neg1( (sd=openConnection("tmp/sock"), 0 )
            do {
    		if (write (sd, msg_s.buffer, msg_s.length) == -1)
                       perror("Error in write() client side");
    		if (-1==read(sd, msg_r.buffer,  20))
                       perror("Error in read() client side");
    		sleep(1);
    	}
    	while (1);
    }
    Now i hope it's better to understand . I think i'm doing some stupid mistake witch the socket initialization! The output is this:

    client output:

    [cls2@it159 DPLAN]$ Error in write() client side : Broken pipe

    server output:

    [cls2@it159 DPLAN]$ No message received from the client: Transport endpoint is not connected

    Please help me! I am stuck and the deathline of this assignment is near! Thanks

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  4. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

Tags for this Thread