accept() always returns 204.204.204.204

  • Thread starter Thread starter TBass
  • Start date Start date
T

TBass

Hi,

I wrote an app that accepts a client connection.

[snip]
SOCKADDR_IN sockAddrIn;
unsigned int clientLen = sizeof( sockAddrIn );
SOCKET conn = accept( m_Socket, (struct sockaddr *)&sockAddrIn,
NULL );

if ( conn == INVALID_SOCKET )
{
int errorno = WSAGetLastError();
if ( errorno != WSAEWOULDBLOCK )
{
return -1;
}
else
{
return false;
}

}
else
{

CFusionSCK_Client myclient(m_ConnFlags);

myclient.Socket( &conn );
myclient.AddrIn( &sockAddrIn );
myclient.IPAddress( inet_ntoa( sockAddrIn.sin_addr ) );
myclient.Port( sockAddrIn.sin_port );

m_listClients.push_back( myclient );

}

return false;

[/snip]

The IP address pulled out of the SOCKADDRIN structure is always
204.204.204.204, no matter what machine I connect from. The connection
is otherwise functional. Has anyone had this problem before?

Thanks in advance,
Tom
 
TBass said:
Hi,

I wrote an app that accepts a client connection.

[snip]
SOCKADDR_IN sockAddrIn;
unsigned int clientLen = sizeof( sockAddrIn );
SOCKET conn = accept( m_Socket, (struct sockaddr *)&sockAddrIn,
NULL );


From the docs: "If addr and/or addrlen are equal to NULL, then no
information about the remote address of the accepted socket is returned."

Maybe try

SOCKADDR_IN sockAddrIn;
int clientLen = sizeof( sockAddrIn );
SOCKET conn = accept( m_Socket, (struct sockaddr *)&sockAddrIn,
&clientLen );

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

if ( conn == INVALID_SOCKET )
{
int errorno = WSAGetLastError();
if ( errorno != WSAEWOULDBLOCK )
{
return -1;
}
else
{
return false;
}

}
else
{

CFusionSCK_Client myclient(m_ConnFlags);

myclient.Socket( &conn );
myclient.AddrIn( &sockAddrIn );
myclient.IPAddress( inet_ntoa( sockAddrIn.sin_addr ) );
myclient.Port( sockAddrIn.sin_port );

m_listClients.push_back( myclient );

}

return false;

[/snip]

The IP address pulled out of the SOCKADDRIN structure is always
204.204.204.204, no matter what machine I connect from. The connection
is otherwise functional. Has anyone had this problem before?

Thanks in advance,
Tom
 
TBass said:
SOCKADDR_IN sockAddrIn;
unsigned int clientLen = sizeof( sockAddrIn );
SOCKET conn = accept( m_Socket, (struct sockaddr *)&sockAddrIn,
NULL );


You're passing NULL to accept() for the length of the address field,
which causes no information to be returned (sane behavior -- without
knowing the number of bytes to the memory pointed, it could not possibly
do anything reasonable)

Try:

SOCKET conn = accept(m_Socket,
(struct sockaddr *)&sockAddrIn,
&clientLen);

The IP address pulled out of the SOCKADDRIN structure is always
204.204.204.204, no matter what machine I connect from. The connection
is otherwise functional. Has anyone had this problem before?

204 is 0xCC, which is the value that the compiler uses for
uninitialized stack variables in debug builds to help catch bad
accesses. Since you passed NULL, nothing was returned, and sockAddrIn
was uninitialized.

-n
 
Back
Top