Reputation: 33
I'm using C language to send data via socket in Linux by using command
send(ServerSocket, cSendBuff, strlen(cSendBuff), 0);
The procedure is:
- Create socket
- Connect to Server
- 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:
- Create socket: No creation (comment out creating function)
- Connection to Server: No connection (comment out connecting function)
- Send data --> Failed (return -1) but no crash
Case 2:
- Create socket: Successfully
- Connection to Server: Failed or even No connection (comment out connecting function)
- Send data --> Crash
And then, i tried 3 different values of socket WITHOUT connection to server
Case 3:
- ServerSocket = -1 --> Send data: Failed
- ServerSocket = 0 --> Send data: Failed
- 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)
{...}
Upvotes: 2
Views: 6131
Reputation: 39326
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?
Upvotes: 15
Reputation: 310913
Case 2.3: This should fail, by which I mean it should return -1 with an accompanying value of errno.
You don't state what you mean by 'fail' or 'crash,' so it is impossible to comment further.
Case 3: These should all fail ditto unless FD 0 or 4 happens to be a socket.
I have no idea why you're even testing any of this. A 'failed connection' is a completely different thing from a socket that has never been connected in the first place. A 'failed connection' manifests itself as a -1 return from send(), recv(),
and friends, with an accompanying value of errno
other than EAGAIN/EWOULDBLOCK.
You can't send to a TCP socket that isn't connected, and it shouldn't be possible for your code to even attempt it. If it is, your error handling code path is incorrect.
Upvotes: 0