-
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
-
Not without seeing some code.
-
"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.
-
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!
-
> sprintf(msg_s.buffer, "The client says hi!");
Well this overflows your buffer.
-
Ok i added one byte more! It's the same,...any idea?
-
> 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.
-
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?
-
I think he meant, how can it compile? Atleast that's my question, 'cause what's ? ain't it supposed to be int main() ?
-
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.
-
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?
-
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
-