Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash when sending data without connection via Socket in Linux

I'm using C language to send data via socket in Linux by using command

send(ServerSocket, cSendBuff, strlen(cSendBuff), 0);

The procedure is:

  1. Create socket
  2. Connect to Server
  3. while (condition): Send data

When I run this program in Linux environment, if it is connected, there is no problem. But in order to take care of failed connection when running, I check some cases and got results as below:

Case 1:

  1. Create socket: No creation (comment out creating function)
  2. Connection to Server: No connection (comment out connecting function)
  3. Send data --> Failed (return -1) but no crash

Case 2:

  1. Create socket: Successfully
  2. Connection to Server: Failed or even No connection (comment out connecting function)
  3. Send data --> Crash

And then, i tried 3 different values of socket WITHOUT connection to server

Case 3:

  1. ServerSocket = -1 --> Send data: Failed
  2. ServerSocket = 0 --> Send data: Failed
  3. ServerSocket = 4 --> Send data: Crash

In case of failed sending, it is correct but I don't understand why i got crash in other cases. I tried with Windows but no problem, is that a different between Linux and Windows? does it make sense? I want to open socket and connect to server only once time and after that sending data a thousand times, so if "the crash" makes sense in this case, how can I fix this problem in case of failed connection? Thanks for your time.

Here is the Case 2 (connect failed by stopping Server in order to test a case of failed connection):

ServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_IP) ;
...
iResult = connect(ServerSocket,(struct sockaddrx *)&server , sizeof(server));
iResult = send(ServerSocket, cSendBuff, strlen(cSendBuff), 0);
if(iResult<0)
{...}

Here is the Case 3:

    //ServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_IP) ;
    ...
    //iResult = connect(ServerSocket,(struct sockaddrx *)&server , sizeof(server));
    ServerSocket = 0;
    iResult = send(ServerSocket, cSendBuff, strlen(cSendBuff), 0);
    log();
    ServerSocket = -1;
    iResult = send(ServerSocket, cSendBuff, strlen(cSendBuff), 0);
    log();
    ServerSocket = 4;
    iResult = send(ServerSocket, cSendBuff, strlen(cSendBuff), 0);
    log();
    if(iResult<0)
    {...}
like image 712
hoangsonvnu Avatar asked Feb 26 '26 20:02

hoangsonvnu


1 Answers

Your program does not crash!

It receives a SIGPIPE signal because the local end of the socket has been shut down. Read man 2 send, especially the EPIPE error case. (So, to be precise, your program is terminated due to an unhandled SIGPIPE signal.)

As the man 7 signal man page says, the default disposition for SIGPIPE is to terminate the process. To avoid the termination, either set a SIGPIPE signal handler, or use send(socket, buffer, length, MSG_NOSIGNAL).

Do not use Windows as your measuring stick. It is not a sane method. You have much better, actually reliable documentation available. The Linux man-pages project is one of the best sources for the C library documentation (section 3). Even though it is focused on Linux and GNU C library, every page has a Conforming to section which tells you when and where that functionality is available.

The above links refer to the up-to-date web pages, but you can also use man 2 send, man 7 signal on the command line to browse the same information. The web pages are more up to date than the distributions, but the project only includes standard man pages, not those installed by applications or extra libraries you might have.

Questions?

like image 153
Nominal Animal Avatar answered Mar 01 '26 11:03

Nominal Animal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!