Below is the relevant code snippet which is working now due to using 4 bytes
in the initial command message. If you change this code
and the peers one to send less than four bytes for the "cmd" token after
the connection is established, then the problem starts to occur. No problem
when doing this between SUNs/Linux server/clients. I initially did this with
one cmd byte as I only have a few commands I want to send to the client.
One thing to mention is probably that the network below is a ppp direct serial
connection which has come up right before (you plug in a scanner handeld
device
to its cradle and the network is established. This triggers the creation of
the
communication thread with the socket stuff below). I also placed some Sleeps
before to make sure the network layer is completely up and there is also
a network share connected and some files transferred before, so the network
layer is already working fine when coming to this point.
while ( bNetworkUp )
{
SOCKADDR_IN local_sin,destination_sin;
PHOSTENT phostent;
// create and bind socket
sock=socket(AF_INET,SOCK_STREAM,0);
local_sin.sin_family = AF_INET;
local_sin.sin_port = htons (0);
local_sin.sin_addr.s_addr = htonl (INADDR_ANY);
bind (sock, (struct sockaddr *) &local_sin, sizeof (local_sin));
// set to nonblocking operation
unsigned long noblock=1;
ioctlsocket( sock,FIONBIO,&noblock );
// prepare destination
destination_sin.sin_family = AF_INET;
//phostent = gethostbyname ("scanhost");
// Assign the socket IP address.
//memcpy ((char FAR *)&(destination_sin.sin_addr),
// phostent->h_addr,
// phostent->h_length);
char addr[4];
addr[0]=(char)192;
addr[1]=(char)169;
addr[2]=(char)1;
addr[3]=(char)1;
memcpy ((char FAR *)&(destination_sin.sin_addr),addr,4);
//memcpy ((char FAR *)&(destination_sin.sin_addr),
// phostent->h_addr,
// phostent->h_length);
destination_sin.sin_port = htons (16325);
// trigger connection attempt to remote host
connect (sock,
(PSOCKADDR) &destination_sin,
sizeof (destination_sin));
// wait for succesful connection and poll network status
BOOL connected=FALSE;
int attempts=5;
while ( bNetworkUp && connected==FALSE && attempts-- > 0)
{
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
fd_set wr;
FD_ZERO(&wr);
FD_SET(sock,&wr);
int rc=select(0, NULL, &wr, NULL, &timeout);
if ( rc==1 && FD_ISSET(sock,&wr) )
connected=TRUE;
}
int collected=0;
char rxbuffer[4];
while ( connected && bNetworkUp )
{
fd_set rd;
FD_ZERO(&rd);
FD_SET(sock,&rd);
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
int rc=select(0, &rd, NULL, NULL, &timeout);
if ( rc==1 && FD_ISSET(sock,&rd) )
{
int len=recv(sock,&rxbuffer[collected],4-collected,0);
if (len>0)
{
collected+=len;
if ( collected==4 )
{
unsigned long cmd=ntohl(*(unsigned long*)rxbuffer);
if ( cmd==0x01 )
transmit_codes(sock);
}
collected=0;
}
else
{
connected=FALSE;
}
}
}
closesocket(sock);
}
Paul G. Tobey said:
Try this in C/C++ instead. I've never seen any such problem and I suspect
that your code is wrong, but the first step in localizing the problem is
eliminating WinSock itself.
Paul T.