Identify a specific TCP connection

B

brambilla

Hi to all !

I have a windows ce embedded device that establish some TCP connection
with a server.

I need to identify a specific connection among others.

I use Net.Sockets.TcpClient() for establing my connection and then I
want use the OpenNetCF TcpConnectionInformation class because i need
to monitor its status, but how can i identify this TCPconnection among
others?

I develop in VB.NET 2005.... Can you help me ?

Thank in advance!

Ale
 
S

Scott Gifford

Hi to all !

I have a windows ce embedded device that establish some TCP connection
with a server.

I need to identify a specific connection among others.

I use Net.Sockets.TcpClient() for establing my connection and then I
want use the OpenNetCF TcpConnectionInformation class because i need
to monitor its status, but how can i identify this TCPconnection among
others?

I haven't used this class, but the MSDN docs appear to show an example
of getting the address and port of the local and remote endpoint. You
should be able to get the same information about your app's TCP
connection, and just look through all of the TcpConnectionInformation
to find it. But it's not clear what information you would get that's
not already available in the socket itself.

What exactly are you trying to do? If you're just trying to detect
when your own connection stops, the normal thing to do is read from it
and receive an error if anything happens. Random tip: the OS won't
necessarily notice that a connection has gone until it has sent data,
whether you use TcpConnectionInformation or not, so normally you will
periodically send data or use TCP keepalives.

Good luck!

----Scott.
 
B

brambilla

I haven't used this class, but the MSDN docs appear to show an example
of getting the address and port of the local and remote endpoint.  You
should be able to get the same information about your app's TCP
connection, and just look through all of the TcpConnectionInformation
to find it.  But it's not clear what information you would get that's
not already available in the socket itself.

What exactly are you trying to do?  If you're just trying to detect
when your own connection stops, the normal thing to do is read from it
and receive an error if anything happens.  Random tip: the OS won't
necessarily notice that a connection has gone until it has sent data,
whether you use TcpConnectionInformation or not, so normally you will
periodically send data or use TCP keepalives.

Good luck!

----Scott.


Hi !
I need to know if the connection is alive or not before send my data.
When the TCP connection is established the
TcpConnectionInformation.status return "Established", if the server go
down the TcpConnectionInformation.status return "SynSent". Like this i
can know if the connection is down (and raise the alarm before the
user try to send the data).
The host port and remote port showing by the TcpConnectionInformation
are the external/random ports, not the port that i have set as
endpoint (ip:port).
I dont know how TcpConnectionInformation work. But the MSDN example
can help me to identify my TCPconnection among others ? Have you the
link?

If i can obtain this informations using the Socket class, can you send
me a little example ? I am really desperate !

Thankyou very much !
Ale
 
P

Paul G. Tobey [eMVP]

You can't know that in all cases. It's impossible. As indicated, you can
send the data and wait to see if there's an exception. You can detect, by
looking for SYN, whether the server cleanly broke the connection, but this
is not a suitable general-purpose detection mechanism for broken
connections. If I go just past your Ethernet switch and cut the cable, you
won't ever get that SYN indication. The only indication that you'll ever get
that send is not possible is when you get a time-out for a sent packet that
was never acknowledged because the cable was cut.

So, with that information in hand, why do you need to verify continuity of
the connection before sending? I never do that and all of my network
programs work just fine.

Paul T.

I haven't used this class, but the MSDN docs appear to show an example
of getting the address and port of the local and remote endpoint. You
should be able to get the same information about your app's TCP
connection, and just look through all of the TcpConnectionInformation
to find it. But it's not clear what information you would get that's
not already available in the socket itself.

What exactly are you trying to do? If you're just trying to detect
when your own connection stops, the normal thing to do is read from it
and receive an error if anything happens. Random tip: the OS won't
necessarily notice that a connection has gone until it has sent data,
whether you use TcpConnectionInformation or not, so normally you will
periodically send data or use TCP keepalives.

Good luck!

----Scott.


Hi !
I need to know if the connection is alive or not before send my data.
When the TCP connection is established the
TcpConnectionInformation.status return "Established", if the server go
down the TcpConnectionInformation.status return "SynSent". Like this i
can know if the connection is down (and raise the alarm before the
user try to send the data).
The host port and remote port showing by the TcpConnectionInformation
are the external/random ports, not the port that i have set as
endpoint (ip:port).
I dont know how TcpConnectionInformation work. But the MSDN example
can help me to identify my TCPconnection among others ? Have you the
link?

If i can obtain this informations using the Socket class, can you send
me a little example ? I am really desperate !

Thankyou very much !
Ale
 
B

brambilla

You can't know that in all cases.  It's impossible.  As indicated, you can
send the data and wait to see if there's an exception.  You can detect,by
looking for SYN, whether the server cleanly broke the connection, but this
is not a suitable general-purpose detection mechanism for broken
connections.  If I go just past your Ethernet switch and cut the cable,you
won't ever get that SYN indication. The only indication that you'll ever get
that send is not possible is when you get a time-out for a sent packet that
was never acknowledged because the cable was cut.

So, with that information in hand, why do you need to verify continuity of
the connection before sending?  I never do that and all of my network
programs work just fine.

Paul T.








Hi !
I need to know if the connection is alive or not before send my data.
When the TCP connection is established the
TcpConnectionInformation.status return "Established", if the server go
down the TcpConnectionInformation.status return "SynSent". Like this i
can know if the connection is down (and raise the alarm before the
user try to send the data).
The host port and remote port showing by the TcpConnectionInformation
are the external/random ports, not the port that i have set as
endpoint (ip:port).
I dont know how TcpConnectionInformation work. But the MSDN example
can help me to identify my TCPconnection among others ? Have you the
link?

If i can obtain this informations using the Socket class, can you send
me a little example ? I am really desperate !

Thankyou very much !
Ale

Ok Paul

help me to understand: how can know if the connection is up (and the
cable is not cut) without wait the timeout ?
I need immediately know if there's a possible loss of data (for
example with OpenNETCF.Net.AdapterNotification class i immediately
know if you cut my cable).
But i'm confused, which is the right way? What is the suitable
detection mechanism for broken connections?
Thankyou very much.
Ale
 
S

Scott Gifford

(e-mail address removed) writes:


[...]

http://msdn.microsoft.com/en-us/lib...rmation.tcpconnectioninformation_members.aspx
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket_members.aspx

Both support the LocalEndPoint and RemoteEndPoint properties (I'm
assuming OpenNetCF has implemented these the same way). If they are
the same for both the TcpConnectionInformation and the Socket, they
should both be talking about the same connection.

[...]
help me to understand: how can know if the connection is up (and the
cable is not cut) without wait the timeout ?
I need immediately know if there's a possible loss of data (for
example with OpenNETCF.Net.AdapterNotification class i immediately
know if you cut my cable).
But i'm confused, which is the right way? What is the suitable
detection mechanism for broken connections?
Thankyou very much.

As Paul said, there's not a general way to know for sure. There are
some specific cases where it might be possible to know, for example if
the link layer of the interface you are using to access the Internet
goes down, but those are special cases, and you would have to code for
them specifically (somehow figuring out which interface the connection
went through then monitoring that interface). They will not tell you
if anything else between your device and the other host you are
talking to has gone down.

The general advice you will see in sockets programming is to wait for
the timeout. You can probably shorten the timeout if you need to fail
more quickly, although I don't know off the top of my head how to do
this.

The SynSent state you are seeing indicates that the remote side hasn't
yet acknowledged some data you sent, and it's not useful on its own.
If you happen to check immediately after sending some data, you may
see that state even in a perfectly healthy connection. If it has been
in that state for awhile, the connection is either broken or stalled,
but you can check for that more easily with a timeout.

Hope this helps,

----Scott.
 
P

Paul G. Tobey [eMVP]

You'd know because your packets are being delivered and you're not getting
exceptions. It's the equivalent of saying, "How do I know if my phone call
is still connected?" Well, you'd usually say something like "Are you
there?" and the other party would respond, "Yes, I'm here." If you just sit
there quietly without saying anything, you'd never know if there was a
connection unless you kept getting data from your talkative friend. If he
just sits there quietly, you'd never know. Of course, this isn't a perfect
parallel, as the phone system uses voltages to indicate if there's really a
connection there, but you get the idea.

There's no way to know immediately if there's a loss of data. If you're
streaming timely data, all you can do is see if data is arriving in a timely
manner by having the other end acknowledge it and make sure that you get
your acknowledgement as fast as you need to get it to indicate that
everything is fast enough. Remember that TCP itself does *NOT* guarantee
any particular timeliness of delivery. It simply guarantees that all
packets that can be delivered will be delivered, in the order they were
sent, or that you'll be notified as soon as the attempted delivery times
out. If you need more information or more-detailed information than that,
you have to do that yourself, on top of TCP or UDP.

TCP itself will only report a broken connection in the way I said, by
throwing an exception when it detects that the connection is closed. You
can set up keep-alives, as Scott indicated, which can give you some
indication that a socket on which you are simply waiting for incoming data
has broken (it sends "Are you there?" packets behind the scenes periodically
and shuts down the socket if the other end doesn't repond repeatedly; this
is *NOT* done on a millisecond by millisecond basis, however, so it won't
tell you anything immediately; the default is not to even start asking, "Are
you there?" for two hours after the socket goes idle).

Paul T.

You can't know that in all cases. It's impossible. As indicated, you can
send the data and wait to see if there's an exception. You can detect, by
looking for SYN, whether the server cleanly broke the connection, but this
is not a suitable general-purpose detection mechanism for broken
connections. If I go just past your Ethernet switch and cut the cable, you
won't ever get that SYN indication. The only indication that you'll ever
get
that send is not possible is when you get a time-out for a sent packet
that
was never acknowledged because the cable was cut.

So, with that information in hand, why do you need to verify continuity of
the connection before sending? I never do that and all of my network
programs work just fine.

Paul T.








Hi !
I need to know if the connection is alive or not before send my data.
When the TCP connection is established the
TcpConnectionInformation.status return "Established", if the server go
down the TcpConnectionInformation.status return "SynSent". Like this i
can know if the connection is down (and raise the alarm before the
user try to send the data).
The host port and remote port showing by the TcpConnectionInformation
are the external/random ports, not the port that i have set as
endpoint (ip:port).
I dont know how TcpConnectionInformation work. But the MSDN example
can help me to identify my TCPconnection among others ? Have you the
link?

If i can obtain this informations using the Socket class, can you send
me a little example ? I am really desperate !

Thankyou very much !
Ale

Ok Paul

help me to understand: how can know if the connection is up (and the
cable is not cut) without wait the timeout ?
I need immediately know if there's a possible loss of data (for
example with OpenNETCF.Net.AdapterNotification class i immediately
know if you cut my cable).
But i'm confused, which is the right way? What is the suitable
detection mechanism for broken connections?
Thankyou very much.
Ale
 
S

Simon Hart [MVP]

You really need to use some sort of "handshaking" ontop of the TCP layer to
ensure your data arrived. This is known as "reliable messaging".

Forget about statuses, so walking upto your TCP socket asking "are you
connected" because this gurantees nothing. If you really wanted to, you
could implement this, something like:

public bool IsConnected
{
get
{
return socket != null && socket.Connected;
}
}

There is a bug with that BTW on CE.

So your Sender (client) could look something like:

socket.Send(Encoding.Unicode.GetBytes("Heres some data,
server""));
while (true)
{
//wait for a response...
byteLength = socket.Receive(receive);
if (byteLength == 0)
break;
else
response.Append(Encoding.Unicode.GetString(receive,
0, byteLength));

if (endOfData)
break;
}

The receiver could look something like:

Socket client = listener.Accept();
byteLength = client.Receive(buffer);
var partialPacket = new StringBuilder();
if (byteLength > 0)
{
try
{
partialPacket.Append(Encoding.Unicode.GetString(buffer,
0, byteLength));
while (true)
{
//Do stuff;
client.Send(Encoding.Unicode.GetBytes("I
got the data OK"));
}
}
Exception
{
//Something screwed up.
}

I will be doing a webcast on this stuff soon. It's complex stuff to do
right. I hope the above will help you on your way.

--
Simon Hart
Visual Developer - Device Application Development MVP
http://www.simonrhart.com

You can't know that in all cases. It's impossible. As indicated, you can
send the data and wait to see if there's an exception. You can detect, by
looking for SYN, whether the server cleanly broke the connection, but this
is not a suitable general-purpose detection mechanism for broken
connections. If I go just past your Ethernet switch and cut the cable, you
won't ever get that SYN indication. The only indication that you'll ever
get
that send is not possible is when you get a time-out for a sent packet
that
was never acknowledged because the cable was cut.

So, with that information in hand, why do you need to verify continuity of
the connection before sending? I never do that and all of my network
programs work just fine.

Paul T.








Hi !
I need to know if the connection is alive or not before send my data.
When the TCP connection is established the
TcpConnectionInformation.status return "Established", if the server go
down the TcpConnectionInformation.status return "SynSent". Like this i
can know if the connection is down (and raise the alarm before the
user try to send the data).
The host port and remote port showing by the TcpConnectionInformation
are the external/random ports, not the port that i have set as
endpoint (ip:port).
I dont know how TcpConnectionInformation work. But the MSDN example
can help me to identify my TCPconnection among others ? Have you the
link?

If i can obtain this informations using the Socket class, can you send
me a little example ? I am really desperate !

Thankyou very much !
Ale

Ok Paul

help me to understand: how can know if the connection is up (and the
cable is not cut) without wait the timeout ?
I need immediately know if there's a possible loss of data (for
example with OpenNETCF.Net.AdapterNotification class i immediately
know if you cut my cable).
But i'm confused, which is the right way? What is the suitable
detection mechanism for broken connections?
Thankyou very much.
Ale
 

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