Aladdin
Aladdin

Reputation: 2509

TCP bus error in Linux

I have a client-server application and I am using tcp sockets. Upon a send() request from the client side I always get bus error and the program terminates. Now I did a little wikipedia search and the article attributes bus errors to (non-existent physical address, unaligned memory access, and accessing an mmapped file that has been truncated). My structure that I am sending is just three ints and an enum instance so I don't think alignment is an issue. Here is the relevant snippet of code:

typedef struct _commsg
{
    RequestType requestType;
    int client_pid;
    int request_id;
    int sector;
}ComMsg; 

in Main:

/* Create a reliable, stream socket using TCP */
if ((sock_id = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    DieWithError("socket() failed\n");

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
echoServAddr.sin_family      = AF_INET;             /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(loopback_addr);   /* Server IP address */
echoServAddr.sin_port        = htons(SERVER_PORT); /* Server port */

/* Establish the connection to the echo server */
if (connect(sock_id, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("connect() failed");


if (send(sock_id, (void *)&_commsg, sizeof(ComMsg), 0) != sizeof(ComMsg))
    DieWithError("send() sent a different number of bytes than expected");

And on the server side:

recv(clntSocket_fd, &_commsg, sizeof(ComMsg), 0);

Thanks in advance.

Upvotes: 0

Views: 2400

Answers (3)

Armali
Armali

Reputation: 19375

Running your client with strace -esend or with gdb might by enlightening.

Upvotes: 0

Perry
Perry

Reputation: 4487

Are you compiling your application with full warnings on (for example, with gcc, -Werror)? You should. A bus error almost certainly means that you are accessing a bad pointer, violating an API in the parameters you are passing to a function, etc. Try compiling with full warnings on, fix every error you see cleanly, and see if that fixes your issue.

By the way, with TCP sockets, read(2) and write(2) are more often used than send(2) and recv(2).

Upvotes: 1

quicoju
quicoju

Reputation: 1711

As stated in the send(2) man page "The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2)"

As stated by Stevens in his Unix Network Programming Book. "Stream sockets (e.g., TCP sockets) exhibit a behavior with the read and write functions that differs from normal file I/O. A read or write on a stream socket might input or output fewer bytes than requested"

Hence may want to test for errors this way instead:

  if (send(sock_id, (void *)&_commsg, sizeof(ComMsg), 0) < 0)

and not testing for the bytes sent. You may be closing your application when there's actually not a problem in it.

Besides sending a structure through the network is usually not a good idea since you can fall into problems related to endianess when connecting with other systems different than yours, the preferred way is working with byte strings.

Upvotes: 0

Related Questions