Unix Programming - Linux sockets.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > Linux sockets.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Linux sockets.
Anonymous

2006-10-15, 7:39 pm

Hi Folks,

I am running into an interesting issue with my Fedora Linux box ... if I
call send on an fd that corresponds to a TCP socket on which the peer has
disconnected, the program simply drops back to the command prompt. No core
file, the program simply stops. Is there something special that I should
know about calling send on a socket on which the other end has disconnected?

Thanks.


Jens Thoms Toerring

2006-10-15, 7:39 pm

Anonymous <Anonymous@home.net> wrote:
> I am running into an interesting issue with my Fedora Linux box ... if I
> call send on an fd that corresponds to a TCP socket on which the peer has
> disconnected, the program simply drops back to the command prompt. No core
> file, the program simply stops. Is there something special that I should
> know about calling send on a socket on which the other end has disconnected?


Difficult to say anything useful unless one knows a bit more about your
program. In principle, if the other side has disconnected, send(2) will
return -1 and errno should be set to ECONNRESET. Unfortunately, you do
no write if you check for that (and what your program does if send(2)
returns -1). But you can't expect a core just because send(2) failed -
many programs have to deal with this possibility and handle such a
situation gracefully (you wouldn't e.g. like your web browser to dump
core just because the server it was talking to died).

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Anonymous

2006-10-15, 7:39 pm

Please allow me to post some code and a bit of additional explanation - I
appreciate your taking time to read this:

I look for a lost connection by waiting for send to
return -1 or 0; however, what seems to be happening is that the application
crashes (no core file or anything) before I get a -1. Some sample code ...
if I connect and disconnect before the 3 seconds is up (note the sleep(3)),
then the output is something along the lines of :

**OUTPUT***
send 1: 2
errno 1: 0

If I connect and wait to receive the data then the output is:

***OUTPUT***
send 1: 2
errno 1: 0
send 2: 2
errno 2: 0
Hello world!

So it looks like the program is crashing on the second send in the first
scenario; however, I have no core file or any indication that it's anything
other than the end of the program (well, other than the fact that I'm
missing the additional printouts) ... that is, it simply drops to the
command prompt. Could someone help?

***CODE***
int main(int argc, char *argv[]) {
// open
int fd = socket(AF_INET, SOCK_STREAM, 0);

// bind
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_family=AF_INET;
address.sin_port=htons(1025);
address.sin_addr.s_addr=htonl(INADDR_ANY);
bind(fd, (struct sockaddr*)&address, sizeof(address));

//listen
listen(fd, 16);

// accept
unsigned int n = sizeof(sockaddr);
sockaddr_in clientAddress;
int cfd = accept(fd, (sockaddr*)&clientAddress, &n);

sleep(3);

char buffer[512];

cout << "send 1: " << send(cfd, buffer, 2, 0) << endl;
cout << "errno 1: " << errno << endl;
cout << "send 2: " << send(cfd, buffer, 2, 0) << endl;
cout << "errno 2: " << errno << endl;

cout << "Hello world!\n";
close(fd);
return 0;
}

"Jens Thoms Toerring" <jt@toerring.de> wrote in message
news:4pfq42Fikb7hU1@uni-berlin.de...
> Anonymous <Anonymous@home.net> wrote:
>
> Difficult to say anything useful unless one knows a bit more about your
> program. In principle, if the other side has disconnected, send(2) will
> return -1 and errno should be set to ECONNRESET. Unfortunately, you do
> no write if you check for that (and what your program does if send(2)
> returns -1). But you can't expect a core just because send(2) failed -
> many programs have to deal with this possibility and handle such a
> situation gracefully (you wouldn't e.g. like your web browser to dump
> core just because the server it was talking to died).
>
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ jt@toerring.de
> \__________________________ http://toerring.de



William Ahern

2006-10-15, 7:39 pm

On Sun, 15 Oct 2006 13:56:20 -0500, Anonymous wrote:

> Hi Folks,
>
> I am running into an interesting issue with my Fedora Linux box ... if I
> call send on an fd that corresponds to a TCP socket on which the peer has
> disconnected, the program simply drops back to the command prompt. No
> core file, the program simply stops. Is there something special that I
> should know about calling send on a socket on which the other end has
> disconnected?
>


By default if you attempt a write to a disconnected socket or pipe your
program is immediately sent a SIGPIPE; the default action is to terminate
the application. If you think how command pipe lines are implemented it
makes sense (likewise, returning zero instead of a signal from a read
makes sense for the same reason). For network software it's usually not
the best default behavior, and it simply becomes an annoyance.

Therefore, it's very common to ignore SIGPIPE early on in a network
application (early in main(), for instance). Just make sure you check
the return value of both reads and writes everywhere and take the
appropriate action manually.

raxitsheth2000@yahoo.co.in

2006-10-16, 1:36 am

Anonymous wrote:
> Hi Folks,
>
> I am running into an interesting issue with my Fedora Linux box ... if I
> call send on an fd that corresponds to a TCP socket on which the peer has
> disconnected, the program simply drops back to the command prompt. No core
> file, the program simply stops. Is there something special that I should
> know about calling send on a socket on which the other end has disconnected?
>
> Thanks.


when peer terminate connection your prog. get SIGPIPE, it may not mean
to generate corefile.

checkout this on ur prompt without "": "ulimit -c unlimited", and then
run your prog. again

or best way is to use errno, ( i dont know as per the man page of send
its not mentioning anything about errno)

--raxit

Nils O. Selåsdal

2006-10-16, 1:36 am

Anonymous wrote:
> Hi Folks,
>
> I am running into an interesting issue with my Fedora Linux box ... if I
> call send on an fd that corresponds to a TCP socket on which the peer has
> disconnected, the program simply drops back to the command prompt. No core
> file, the program simply stops. Is there something special that I should
> know about calling send on a socket on which the other end has disconnected?


In certan situations that can lead to a SIGPIPE signal terminating the
process. You won't get any coredimp unless you change the core ulimit.
Normally you want to ignore or handle SIGPIPE when using TCP.

Also, what does your program do when send fails ?
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com