TCP/IP Monitoring in .NET

  • Thread starter Thread starter Randal
  • Start date Start date
R

Randal

Does anyone have a code sample of how one would "listen"
to a TCP/IP session between an application on the local
machine and a remote host.

I'm looking for code that would allow me to specify a
local IP and a local port which is already in use by
another application (outside of my control) and simply
watch the traffic.

I know there are open source packet analyzers available
that use special network drivers, use of permiscuous
network mode, etc., etc. But I'm not looking to watch
any traffic except to and from my local machine on a
known port and I am hoping that this is less complicated
and that .NET's rich netwoking classes will lend a hand.

The two biggest issues that exist are the fact that
windows sockets does not like an port/ip pair bound to
two different sockets. I can sidestep this issue by
using SetSocketOption and SocketOptionName.ReuseAddress,
but I have found that simply binding to the address is
not sufficient to create the desired effect.

Any help?
 
Hi Randal,

You may try to see the SetSocketOption function.
But if the other application use the socket as SocketOptionName
=ExclusiveAddressUse
Then you can not use SetSocketOption to set your socket as
SocketOptionName = ReuseAddress which allows the socket to be bound to an
address that is already in use.

Did I answer your question?

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
As noted in my post, I am already using SetSocketOption
with the ReuseAddress option, but I was looking for some
of the specifics of extracting the information, since the
underlying socket is already connected. The issue is
that Socket.Receive cannot be used without the new socket
being connected. I am trying to figure out the specifics
of making a "connection" to the connection that already
exists by the original application.
 
Hi Randal,

You may try to see the SetSocketOption function. By default, when you new a
socket, it will use the ExclusiveAddressUse option,
i.e. you can not use SetSocketOption to set your socket as
SocketOptionName = ReuseAddress which allows the socket to be bound to an
address that is already in use.
To let the ReuseAddress work, you may need to set the two application all
with ReuseAddress option.
e.g.
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,
ProtocolType.Tcp );
listener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddr
ess,1);

But, when the latter running application running the SetSocketOption and
bind to the socket, the first running application will no longer get data.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
Back
Top