TCPClient/NetworkStream packet status problem

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 IsCompleted. 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());
}
}
 
P

Paul G. Tobey [eMVP]

At the socket level, *every* packet on a connection-oriented socket is
delivered. The only indication that you can get that it was not delivered
is, as you saw, an exception indicating that the socket is down.

Keep-alive doesn't affect all sockets on the system; it's enabled on a
socket-by-socket basis (although the *settings* are global, of course). For
that matter, keep-alive is only sent when the socket is idle, so it won't
help in your case.

The only way that I can think of to actually do what you're talking about is
to replace the WinSock layer yourself or implement your own version of TCP
on top of datagram packets (you'd be responsible for acknowledging receipt
of packets, deciding when a socket was timed-out, etc., so you'd be able to
do whatever you want). Since this doesn't seem too practical to me, I'd
suggest that you either live with the time-out time you have now or bite the
bullet and implement a response to every message and your own 'send
time-out'.

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top