Check for activity on TCP port

  • Thread starter Thread starter Sir C4
  • Start date Start date
S

Sir C4

Can someone give a quick example of how to check for activity on a TCP
port? For example, I'd like to watch and listen for activity on port
80. Once activity is detected, I'd like an event to be triggered.

Thanks in advanced.
 
Hello Sir,

See this C# sniffer http://www.c-sharpcorner.com/Upload...erInCS11222005232804PM/SimpleSnifferInCS.aspx


---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

SC> Can someone give a quick example of how to check for activity on a
SC> TCP port? For example, I'd like to watch and listen for activity on
SC> port 80. Once activity is detected, I'd like an event to be
SC> triggered.
SC>
SC> Thanks in advanced.
SC>
 


Thanks for the quick response. That example was not the most
complete / easy to follow example :(

Let me clarify my request a little. I can get an array of tcp
listeners via the following code:

IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = properties.GetActiveTcpListeners();

I'm looking for an event that will fire once a tcp port moves from the
listening status to established status. I want this to run as a
service so I don't want it to chew up cycles constantly checking for a
specific port to move form listening to established that's why I ask
for an event. Basically I want to execute some code once the port
moves from listening to established, and then run some clean up code
once it moves from established back to listening.
 
[...]
I'm looking for an event that will fire once a tcp port moves from the
listening status to established status.

You should probably learn more about TCP first.

There's no such thing as a TCP port moving "from listening status to
established status". An entirely new connection, with the "established"
status, is created when an application with a "listening" TCP port accepts
the connection. But the "listening" TCP port remains, and is still
listening for new connections.
I want this to run as a
service so I don't want it to chew up cycles constantly checking for a
specific port to move form listening to established that's why I ask
for an event.

Short of sniffing the network traffic and watching for TCP connections
being created, I'm not aware of any way to do what you want. Not directly
anyway.

One thing you could do is poll the TCP status and check for new
"established" connections on the same port as a "listening" port.
Obviously you wouldn't want to poll too often (maybe once every 5 or 10
seconds), but this would be one way to do it.

The obvious problem with this is that a connection could be closed and
then reopened between the time you've polled, or opened and closed for
that matter. You won't get guaranteed notification of network connection
changes this way, but it might be close enough for your purposes. Depends
on what you're actually doing.
Basically I want to execute some code once the port
moves from listening to established, and then run some clean up code
once it moves from established back to listening.

If you think it might be useful for getting better advice, you might
consider being more specific about the "clean up code". I'm having a hard
time imagining what one process would be able to usefully "clean up" in
response to some other process connecting or disconnecting TCP
connections. There's a small possibility that there's an entirely
different approach to accomplish what you're trying to do, but no one can
offer that information if they don't know what the end goal here is.

Pete
 
[...]
I'm looking for an event that will fire once a tcp port moves from the
listening status to established status.

You should probably learn more about TCP first.

There's no such thing as a TCP port moving "from listening status to
established status". An entirely new connection, with the "established"
status, is created when an application with a "listening" TCP port accepts
the connection. But the "listening" TCP port remains, and is still
listening for new connections.
I want this to run as a
service so I don't want it to chew up cycles constantly checking for a
specific port to move form listening to established that's why I ask
for an event.

Short of sniffing the network traffic and watching for TCP connections
being created, I'm not aware of any way to do what you want. Not directly
anyway.

One thing you could do is poll the TCP status and check for new
"established" connections on the same port as a "listening" port.
Obviously you wouldn't want to poll too often (maybe once every 5 or 10
seconds), but this would be one way to do it.

The obvious problem with this is that a connection could be closed and
then reopened between the time you've polled, or opened and closed for
that matter. You won't get guaranteed notification of network connection
changes this way, but it might be close enough for your purposes. Depends
on what you're actually doing.
Basically I want to execute some code once the port
moves from listening to established, and then run some clean up code
once it moves from established back to listening.

If you think it might be useful for getting better advice, you might
consider being more specific about the "clean up code". I'm having a hard
time imagining what one process would be able to usefully "clean up" in
response to some other process connecting or disconnecting TCP
connections. There's a small possibility that there's an entirely
different approach to accomplish what you're trying to do, but no one can
offer that information if they don't know what the end goal here is.

Pete

Pete,

Thanks for your insight. Basically I'm using VNC for remote
administration. Vista's new aero glass slows down the screen refresh
rate. Basically I want to watch for the VNC connection, when it's
detected, disable the aero experience, and when the connection is
closed, turn aero back on.

I think your explination will work out just fine.
 

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

Back
Top