TCPClient - Write timeout

  • Thread starter Thread starter Niklas
  • Start date Start date
N

Niklas

I'm having a problem with the TCPClient/NetworkStream that I can't get
around. When I send something on the socket I don't know if it's
received by the server or not. I always get IAsyncResult.IsCompleted
== true no matter if the server receives the packet or not. After 2
minutes I get the socket timeout if the server is unreachable.

I know the following:
SetSocketOption – Send timeout is not implemented on Win CE
The default Send timeout is 2 minutes
I can use socket Keep Alive but that affects all sockets on the system

The commonly suggested solution:
Use a timer to se if there is a pending message.
Let the server send a response for each packet. (We use GPRS and would
like to keep the network traffic to a minimum)

My question is:
How can I see if a socket (or NetworkStream) has a pending packet?
Some how the network layer knows it since it reports it after 2
minutes.

Thanx,
Niklas



//TcpClient m_tcpClient;
private void Send()
{
try
{
string tmpString = "Hello";
Encoding encStr = Encoding.Default;
Byte[] ByteGet = encStr.GetBytes(tmpString);
m_tcpClient.BeginSend(ByteGet, 0, ByteGet.Length,
0, new AsyncCallback(SendMsgCallBack),
new SentMsgStatStruct(m_tcpClient, tmpString));
}
catch( Exception exc )
{
MessageBox.Show("Exception: " + exc.ToString());
}
}

private void SendMsgCallBack(System.IAsyncResult ar)
{
try
{
MessageBox.Show("Completed: " + ar.IsCompleted.ToString());
MessageBox.Show("Completed Sync: " +
ar.CompletedSynchronously.ToString());
socket.EndSend(ar);
}
catch( SocketException e )
{
MessageBox.Show("Socket exception: " + e.ToString());
}
catch( Exception exc )
{
MessageBox.Show("Exception: " + exc.ToString());
}
}
 
I'm sorry if so is the case but the only answer I've found is "use a
timer" but I still need to find out if a socket (or NetworkStream) has
a pending packet. What good would the timer do otherwise?

Niklas

Paul G. Tobey said:
Haven't we just been through this?

Paul T.

Niklas said:
I'm having a problem with the TCPClient/NetworkStream that I can't get
around. When I send something on the socket I don't know if it's
received by the server or not. I always get IAsyncResult.IsCompleted
== true no matter if the server receives the packet or not. After 2
minutes I get the socket timeout if the server is unreachable.

I know the following:
SetSocketOption - Send timeout is not implemented on Win CE
The default Send timeout is 2 minutes
I can use socket Keep Alive but that affects all sockets on the system

The commonly suggested solution:
Use a timer to se if there is a pending message.
Let the server send a response for each packet. (We use GPRS and would
like to keep the network traffic to a minimum)

My question is:
How can I see if a socket (or NetworkStream) has a pending packet?
Some how the network layer knows it since it reports it after 2
minutes.

Thanx,
Niklas



//TcpClient m_tcpClient;
private void Send()
{
try
{
string tmpString = "Hello";
Encoding encStr = Encoding.Default;
Byte[] ByteGet = encStr.GetBytes(tmpString);
m_tcpClient.BeginSend(ByteGet, 0, ByteGet.Length,
0, new AsyncCallback(SendMsgCallBack),
new SentMsgStatStruct(m_tcpClient, tmpString));
}
catch( Exception exc )
{
MessageBox.Show("Exception: " + exc.ToString());
}
}

private void SendMsgCallBack(System.IAsyncResult ar)
{
try
{
MessageBox.Show("Completed: " + ar.IsCompleted.ToString());
MessageBox.Show("Completed Sync: " +
ar.CompletedSynchronously.ToString());
socket.EndSend(ar);
}
catch( SocketException e )
{
MessageBox.Show("Socket exception: " + e.ToString());
}
catch( Exception exc )
{
MessageBox.Show("Exception: " + exc.ToString());
}
}
 
A 'pending packet' doesn't exist. That's not something exposed to the
interface. If the interface dies after you send your last packet and after
it's acknowledged, you'll never know (unless you use keep-alive), that the
connection is gone. If the wire is disconnected during a send before you
get acknowledgement of the packet, then, as you found, you will be notified
that the connection is broken, but the time it takes to *really* tell that
the connection is down is not a few seconds. There is no access to 'was
this packet acknowledged' information.

You can implement something for yourself using UDP. There's just no access
to that information for TCP. This is a big job, however, and it's not clear
to me that you really need it. You'll find out in an amount of time
designed to give you a reliable result, that the connection was broken.

Paul T.

Niklas said:
I'm sorry if so is the case but the only answer I've found is "use a
timer" but I still need to find out if a socket (or NetworkStream) has
a pending packet. What good would the timer do otherwise?

Niklas

Paul G. Tobey said:
Haven't we just been through this?

Paul T.

Niklas said:
I'm having a problem with the TCPClient/NetworkStream that I can't get
around. When I send something on the socket I don't know if it's
received by the server or not. I always get IAsyncResult.IsCompleted
== true no matter if the server receives the packet or not. After 2
minutes I get the socket timeout if the server is unreachable.

I know the following:
SetSocketOption - Send timeout is not implemented on Win CE
The default Send timeout is 2 minutes
I can use socket Keep Alive but that affects all sockets on the system

The commonly suggested solution:
Use a timer to se if there is a pending message.
Let the server send a response for each packet. (We use GPRS and would
like to keep the network traffic to a minimum)

My question is:
How can I see if a socket (or NetworkStream) has a pending packet?
Some how the network layer knows it since it reports it after 2
minutes.

Thanx,
Niklas



//TcpClient m_tcpClient;
private void Send()
{
try
{
string tmpString = "Hello";
Encoding encStr = Encoding.Default;
Byte[] ByteGet = encStr.GetBytes(tmpString);
m_tcpClient.BeginSend(ByteGet, 0, ByteGet.Length,
0, new AsyncCallback(SendMsgCallBack),
new SentMsgStatStruct(m_tcpClient, tmpString));
}
catch( Exception exc )
{
MessageBox.Show("Exception: " + exc.ToString());
}
}

private void SendMsgCallBack(System.IAsyncResult ar)
{
try
{
MessageBox.Show("Completed: " + ar.IsCompleted.ToString());
MessageBox.Show("Completed Sync: " +
ar.CompletedSynchronously.ToString());
socket.EndSend(ar);
}
catch( SocketException e )
{
MessageBox.Show("Socket exception: " + e.ToString());
}
catch( Exception exc )
{
MessageBox.Show("Exception: " + exc.ToString());
}
}
 
Back
Top