Hi
Something very strange is happening in my code...
I have a client application that communicates with a server application over TCP/IP. If the server application isnt running then the client attempts to reconnect every 5 seconds.
The strange thing is that if I leave the client long enough then it appears to establish a connection and successfully send data, even though the server application definitely isnt running!
The client then doesnt get back the response it was expecting from the server so the connection is dropped and then we go back to retrying unsuccessfully for a long time before again we appear to connect. And so on.
Can you think of any reason why the "connect" and "send" winsock functions are occasionally succeeding even though the server application isnt running?
Here is a summary of the code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // Attempt to connect to the server
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( <span style="color:#A31515; "127.0.0.1" );
clientService.sin_port = htons( m_SocketPort );
<span style="color:Blue; if (connect(m_Socket, (SOCKADDR*)&clientService, <span style="color:Blue; sizeof(clientService)) == SOCKET_ERROR)
{
TraceWarning(<span style="color:#A31515; "Reconnect - Could not connect to Server! ReasonCode = %d", WSAGetLastError());
<span style="color:Blue; return <span style="color:Blue; false;
}
<span style="color:Green; // Send identification message
<span style="color:Blue; char message_type = 0;
<span style="color:Blue; unsigned <span style="color:Blue; long seq = m_SequenceNumber++;
<span style="color:Green; // Get the client name to use
string client_name = m_ClientName;
<span style="color:Blue; unsigned <span style="color:Blue; short message_len = <span style="color:Blue; sizeof(message_type) + <span style="color:Blue; sizeof(seq) + client_name.length();
string message((<span style="color:Blue; char*)&message_len, 2); <span style="color:Green; // First short of the message is the payload size
message += string((<span style="color:Blue; char*)&seq, 4); <span style="color:Green; // Add the sequence number
message += string((<span style="color:Blue; char*)&message_type, 1); <span style="color:Green; // Add the message type (0 = Identification Message)
message += client_name; <span style="color:Green; // Add the clients name
<span style="color:Blue; if (send(m_Socket, &message[0], message.length(), 0) == SOCKET_ERROR)
{
TraceWarning(<span style="color:#A31515; "Reconnect - Error occurred whilst sending identification to the server! Disconnecting. ErrorCode = %d", WSAGetLastError());
Disconnect();
<span style="color:Blue; return <span style="color:Blue; false;
}
[/code]
Thanks<br/>
Ben<br/>
<br/>
View the full article
Something very strange is happening in my code...
I have a client application that communicates with a server application over TCP/IP. If the server application isnt running then the client attempts to reconnect every 5 seconds.
The strange thing is that if I leave the client long enough then it appears to establish a connection and successfully send data, even though the server application definitely isnt running!
The client then doesnt get back the response it was expecting from the server so the connection is dropped and then we go back to retrying unsuccessfully for a long time before again we appear to connect. And so on.
Can you think of any reason why the "connect" and "send" winsock functions are occasionally succeeding even though the server application isnt running?
Here is a summary of the code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // Attempt to connect to the server
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( <span style="color:#A31515; "127.0.0.1" );
clientService.sin_port = htons( m_SocketPort );
<span style="color:Blue; if (connect(m_Socket, (SOCKADDR*)&clientService, <span style="color:Blue; sizeof(clientService)) == SOCKET_ERROR)
{
TraceWarning(<span style="color:#A31515; "Reconnect - Could not connect to Server! ReasonCode = %d", WSAGetLastError());
<span style="color:Blue; return <span style="color:Blue; false;
}
<span style="color:Green; // Send identification message
<span style="color:Blue; char message_type = 0;
<span style="color:Blue; unsigned <span style="color:Blue; long seq = m_SequenceNumber++;
<span style="color:Green; // Get the client name to use
string client_name = m_ClientName;
<span style="color:Blue; unsigned <span style="color:Blue; short message_len = <span style="color:Blue; sizeof(message_type) + <span style="color:Blue; sizeof(seq) + client_name.length();
string message((<span style="color:Blue; char*)&message_len, 2); <span style="color:Green; // First short of the message is the payload size
message += string((<span style="color:Blue; char*)&seq, 4); <span style="color:Green; // Add the sequence number
message += string((<span style="color:Blue; char*)&message_type, 1); <span style="color:Green; // Add the message type (0 = Identification Message)
message += client_name; <span style="color:Green; // Add the clients name
<span style="color:Blue; if (send(m_Socket, &message[0], message.length(), 0) == SOCKET_ERROR)
{
TraceWarning(<span style="color:#A31515; "Reconnect - Error occurred whilst sending identification to the server! Disconnecting. ErrorCode = %d", WSAGetLastError());
Disconnect();
<span style="color:Blue; return <span style="color:Blue; false;
}
[/code]
Thanks<br/>
Ben<br/>
<br/>
View the full article