Click to See Complete Forum and Search --> : winsock w98 can't connect to wxp socket server!


Altor
December 16th, 2004, 07:59 AM
Hi,

I just try a very simple sample of a client/server using sockets.
I have a strange(?) problem.

My network configuration is:
S=W2K domain server
A=W2K workstation
B=W98 workstation
C=WXP workstation

When I run my socket server on A, socket client B connect, socket client C does too
When I run my socket server on B, socket client A connect, socket client C does too
When I run my socket server on C, socket client A connect, socket client B DOESN'T

Any idea ? (I searched the web, etc ... and found nothing)

here is the sample code I used
SERVER:
int main(int argc, char* argv[])
{
WSADATA wsaData;

int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
{
printf("Error at WSAStartup()\n");
getchar();
return 0;
}

SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET )
{
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
getchar();
return 0;
}

sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr( "192.168.1.7" );
service.sin_port = htons( 27015 );
if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
printf( "bind() failed.\n" );
closesocket(m_socket);
WSACleanup();
getchar();
return 0;
}

if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");

SOCKET AcceptSocket;
printf( "Waiting for a client to connect...\n" );
while (1)
{
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR )
{
AcceptSocket = accept( m_socket, NULL, NULL );
}
printf( "Client Connected.\n");
m_socket = AcceptSocket;
break;
}

getchar();
return 0;
}
CLIENT:
int main(int argc, char* argv[])
{
WSADATA wsaData;

int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
{
printf("Error at WSAStartup()\n");
getchar();
return 0;
}

SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET )
{
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
getchar();
return 0;
}

sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "192.168.1.7" );
clientService.sin_port = htons( 27015 );
if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR)
{
printf( "Failed to connect.\n" );
WSACleanup();
getchar();
return 0;
}

getchar();
return 0;
}

MikeAThon
December 16th, 2004, 11:39 AM
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr( "192.168.1.7" );
service.sin_port = htons( 27015 );
if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
printf( "bind() failed.\n" );
closesocket(m_socket);
WSACleanup();
getchar();
return 0;
}

Is 192.168.1.7 the local address in all cases (i.e., for S, A, B and C)? If not, then the bind() function might be binding to a remote address.

Mike

Altor
December 16th, 2004, 01:16 PM
Hi mike,

Thx for your post.
I just forget it, but INDEED, I changed the IP address for each of my test cases:
When server was A(win2k), address was 192.168.1.4
When server was B(win98), address was 192.168.1.2
When server was C(winxp), address was 192.168.1.7

All of this SHOULD work, and all of this WORK but C as server won't
What is this XP ghost ?

sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr( "192.168.1.7" );
service.sin_port = htons( 27015 );
if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
printf( "bind() failed.\n" );
closesocket(m_socket);
WSACleanup();
getchar();
return 0;
}

Is 192.168.1.7 the local address in all cases (i.e., for S, A, B and C)? If not, then the bind() function might be binding to a remote address.

Mike

MikeAThon
December 16th, 2004, 04:16 PM
service.sin_port = htons( 27015 );
OK, here's another idea. Eliminate the htons() call, at both the client and server sides. It's not needed, and maybe the XP machine is implementing it differently than the others (is it a 64-bit OS?)

Incidentally, with htons(), you probably are not really connecting on port 27015 as you obviously intend to. While the server is running, open a command window and type "netstat -an" to list all active ports. On what port is your server listening (with and without the htons() call)?

Mike

btpanek09
December 21st, 2004, 08:24 AM
Immediately after creating the socket I set it to reuse the address.
SET_OPT_RETURN = setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR , &optval, sizeof(int));

Then before calling accept I set the socket to non-blocking.
argp = 1;//Set Socket to 1 for NON-Blocking...
ioc_RETURN = ioctlsocket(hCS_CLIENT_SOCKET, FIONBIO, &argp);

Even though the socket is set to non-blocking before the accept, it does not behave like it is non-blocking instead it blocks until the accept is made (no need to loop).

Even with these fixes in which were not needed before it will sometimes never complete the accept. I am surprised MS has messed this up in XP.