Unix Programming - tcp socket connect reports fake success

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2004 > tcp socket connect reports fake success





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 tcp socket connect reports fake success
ten90425@gbg.bg

2004-12-30, 7:56 am

Hi all,
I've written a tcp client that connects from Linux (Fedora Core 1) to
Windows
and AIX. If the connection is lost it tries to connect in 5 sec
intervals.
The problem is that when in the evening we stop the servers (tested
with
AIX, Win NT and 2000 ) and let the client running over night, in the
morning the client says it is connected.
On the client OS netstat also reports connection ESTABLISHED. And thats
when the server software is not running. On the server OS netstat says
that no such connection exists.
My code is (removed logging and other stuff) :
// if connecting didn't succeed, this function is called every 5
seconds
int connect_server()
{
struct sockaddr_in _addr;
int res;
int s;

s = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );

_addr.sin_family = AF_INET;
_addr.sin_addr = gBindIp;
_addr.sin_port = 0;
if( -1 == bind( s, (struct sockaddr*)&_addr, sizeof( _addr ) )
){
close(s);
return -1;
}
_addr.sin_family = AF_INET;
_addr.sin_addr = gConnIp;
_addr.sin_port = htons(gPort);
res = connect( s, (struct sockaddr *)&_addr, sizeof(struct
sockaddr_in) );
if( -1 == res ) {
close(s);
return -1;
}

// make NONBLOCK to use the socket in select()
if( fcntl( s, F_SETFL, O_NONBLOCK | fcntl( s, F_GETFL ) ) == -1
){
close(s);
return -1;
}
return s;
}

Vendor Neutral

2004-12-30, 5:53 pm

ten90425@gbg.bg wrote...
> Hi all,
> I've written a tcp client that connects from Linux (Fedora Core 1) to
> Windows
> and AIX. If the connection is lost it tries to connect in 5 sec
> intervals.
> The problem is that when in the evening we stop the servers (tested
> with
> AIX, Win NT and 2000 ) and let the client running over night, in the
> morning the client says it is connected.


Put a packet sniffer on your network and see what the servers do when
they stop. Odds are very, very good that they die silently (ie, do
not issue a RST or FIN sequence) to the clients.

Without that indication of connection loss, the client assumes "no
news is good news" regarding the connection state.

> On the client OS netstat also reports connection ESTABLISHED. And thats
> when the server software is not running. On the server OS netstat says
> that no such connection exists.
> My code is (removed logging and other stuff) :
> // if connecting didn't succeed, this function is called every 5
> seconds
> int connect_server()
> {
> struct sockaddr_in _addr;
> int res;
> int s;
>
> s = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );
>
> _addr.sin_family = AF_INET;
> _addr.sin_addr = gBindIp;
> _addr.sin_port = 0;
> if( -1 == bind( s, (struct sockaddr*)&_addr, sizeof( _addr ) )
> ){
> close(s);
> return -1;
> }
> _addr.sin_family = AF_INET;
> _addr.sin_addr = gConnIp;
> _addr.sin_port = htons(gPort);
> res = connect( s, (struct sockaddr *)&_addr, sizeof(struct
> sockaddr_in) );
> if( -1 == res ) {
> close(s);
> return -1;
> }
>
> // make NONBLOCK to use the socket in select()
> if( fcntl( s, F_SETFL, O_NONBLOCK | fcntl( s, F_GETFL ) ) == -1
> ){
> close(s);
> return -1;
> }
> return s;
> }
>
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com