473,544 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UDP Listen on same port by two apps at same time

I wrote a service that listens for broadcast messages from my firewall (UDP
snmp trap messages) and parses and puts the data in an database.

I'd also like to write an app that is a simple form that can listen in when
it runs (so I can see messages in a form as they occur.)

So I need the ability to listen to a UDP port with two apps at the same time
(the service and my app). But when I do that I get an error:
System.Net.Sock ets.SocketExcep tion: Only one usage of each socket address
(protocol/network address/port) is normally permitted
at System.Net.Sock ets.Socket.Bind (EndPoint localEP)
at UdpReceive.Clas s1.StartReceive From() in
c:\myprojects\d otnet\cs\networ king

Is it possible to listen to the same port with two apps at once?

Thank you

p.s. One way I could do this is to use a packet driver and something like
WinPCap and just grab the frames from any app I have -- but I'd like to try
this strictly from the DotNet Framework and C#.
Nov 15 '05 #1
9 43089
U¿ytkownik "mBird" <no@spam.com> napisa³ w wiadomo¶ci
news:Ez******** *************@t wister.nyroc.rr .com...
I wrote a service that listens for broadcast messages from my firewall (UDP snmp trap messages) and parses and puts the data in an database.

I'd also like to write an app that is a simple form that can listen in when it runs (so I can see messages in a form as they occur.)

So I need the ability to listen to a UDP port with two apps at the same time (the service and my app). But when I do that I get an error:
System.Net.Sock ets.SocketExcep tion: Only one usage of each socket address
(protocol/network address/port) is normally permitted
at System.Net.Sock ets.Socket.Bind (EndPoint localEP)
at UdpReceive.Clas s1.StartReceive From() in
c:\myprojects\d otnet\cs\networ king

Is it possible to listen to the same port with two apps at once?

Thank you

p.s. One way I could do this is to use a packet driver and something like
WinPCap and just grab the frames from any app I have -- but I'd like to try this strictly from the DotNet Framework and C#.

I think you can't (Or maybe you shouldn't). It's incompatible with UDP
standard.
Nov 15 '05 #2
Hi,

"mBird" <no@spam.com> wrote in message
news:Ez******** *************@t wister.nyroc.rr .com...
I wrote a service that listens for broadcast messages from my firewall (UDP snmp trap messages) and parses and puts the data in an database.

I'd also like to write an app that is a simple form that can listen in when it runs (so I can see messages in a form as they occur.)

So I need the ability to listen to a UDP port with two apps at the same time (the service and my app). But when I do that I get an error:
System.Net.Sock ets.SocketExcep tion: Only one usage of each socket address
(protocol/network address/port) is normally permitted
at System.Net.Sock ets.Socket.Bind (EndPoint localEP)
at UdpReceive.Clas s1.StartReceive From() in
c:\myprojects\d otnet\cs\networ king

Is it possible to listen to the same port with two apps at once?
Try to set the following option before calling bind :
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
HTH,
greetings


Thank you

p.s. One way I could do this is to use a packet driver and something like
WinPCap and just grab the frames from any app I have -- but I'd like to try this strictly from the DotNet Framework and C#.

Nov 15 '05 #3
"mBird" <no@spam.com> wrote in message news:Ez******** *************@t wister.nyroc.rr .com...

I would have thought the best method would be to have the service send a message to your form.

--
Michael Culley
Nov 15 '05 #4
> Is it possible to listen to the same port with two apps at once?

Don't bind the port, just receive on it?
Nov 15 '05 #5
Hi,

"BMermuys" <bm************ **@hotmail.com> wrote in message
news:wM******** *************@p hobos.telenet-ops.be...
Hi,

"mBird" <no@spam.com> wrote in message
news:Ez******** *************@t wister.nyroc.rr .com...
I wrote a service that listens for broadcast messages from my firewall (UDP
snmp trap messages) and parses and puts the data in an database.

I'd also like to write an app that is a simple form that can listen in

when
it runs (so I can see messages in a form as they occur.)

So I need the ability to listen to a UDP port with two apps at the same

time
(the service and my app). But when I do that I get an error:
System.Net.Sock ets.SocketExcep tion: Only one usage of each socket address (protocol/network address/port) is normally permitted
at System.Net.Sock ets.Socket.Bind (EndPoint localEP)
at UdpReceive.Clas s1.StartReceive From() in
c:\myprojects\d otnet\cs\networ king

Is it possible to listen to the same port with two apps at once?


Try to set the following option before calling bind :
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);


Some explenation.

You must always bind before you can receive. For connection-oriented
sockets (TCP) this might be done implicitly by a connect call or explicitly
by calling bind before calling listen.

For connection-less oriented sockets (UDP):
To receive unicast udp, you bind to a local addr/port to which udp packets
are sent.

To receive multicast udp, eg sent to 239.255.255.250 :1900, you add the ip
address as a multicast group(socketopt ions) and then bind to the local port
1900.

Bind allows only one process to bind to one local addr/port pair.

This is ussally fine, but multicast means for everyone, so it makes sense
that multiple processes (aplications) can receive them. SO_REUSEADDR makes
this possible. It allows multiple processes to receive the same broadcast
packet.

Note that even when using the SO_REUSEADDR option unicast udp packets will
only be received by one socket. This because unicast means only one.
Usually the last bounded socket receives it.

Now to set the socket option SO_REUSEADDR in c# call:

Socket _socket;
....
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
....

1 only indicates true.

When you set this option, you indicate that another process may bind to the
same local addr/port.
HTH,
Greetings


HTH,
greetings


Thank you

p.s. One way I could do this is to use a packet driver and something like WinPCap and just grab the frames from any app I have -- but I'd like to

try
this strictly from the DotNet Framework and C#.


Nov 15 '05 #6
Hi --

Thanks for the info --

I tried using the SetSocketOption . To test it I did:
-Created a test listener
-Launched the testlistener.ex e twice (so now two apps listening).

Both launched OK (so no longer get errror message "Only one usage of each
socket address (protocol/network address/port) is normally permitted")

But only one app instance sees UDP messages (they are simple console apps
and one sees and prints my UPD messages while the other apps instance just
sits there and does nothing)???

Here is the code for a test client I used -- any ideas greatly appretiated!
Thanks

public void StartReceiveFro m()
{
IPHostEntry localHostEntry;

try
{
Socket soUdp =
new Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p);
soUdp.SetSocket Option(SocketOp tionLevel.Socke t,
SocketOptionNam e.ReuseAddress, 1);

try
{
localHostEntry = Dns.GetHostByNa me(Dns.GetHostN ame());
}
catch(Exception )
{
Console.WriteLi ne("Local Host not found");
return ;
}

IPEndPoint localIpEndPoint =
new IPEndPoint(loca lHostEntry.Addr essList[0], UdpPort);

soUdp.Bind(loca lIpEndPoint);

while (true)
{
Byte[] received = new Byte[1500];

IPEndPoint tmpIpEndPoint =
new IPEndPoint(loca lHostEntry.Addr essList[0], UdpPort);
EndPoint remoteEP = (tmpIpEndPoint) ;
int bytesReceived = soUdp.ReceiveFr om(received, ref remoteEP);

String dataReceived = System.Text.Enc oding.ASCII.Get String(received );
Console.WriteLi ne(dataReceived .Substring(i, j-i));
}
}
catch (SocketExceptio n se)
{
Console.WriteLi ne("Socket Exception:\n" + se.ToString());
}
}
"BMermuys" <bm************ **@hotmail.com> wrote in message
news:mP******** *************@p hobos.telenet-ops.be...
Hi,

"BMermuys" <bm************ **@hotmail.com> wrote in message
news:wM******** *************@p hobos.telenet-ops.be...
Hi,

"mBird" <no@spam.com> wrote in message
news:Ez******** *************@t wister.nyroc.rr .com...
I wrote a service that listens for broadcast messages from my firewall (UDP
snmp trap messages) and parses and puts the data in an database.

I'd also like to write an app that is a simple form that can listen in

when
it runs (so I can see messages in a form as they occur.)

So I need the ability to listen to a UDP port with two apps at the
same
time
(the service and my app). But when I do that I get an error:
System.Net.Sock ets.SocketExcep tion: Only one usage of each socket address (protocol/network address/port) is normally permitted
at System.Net.Sock ets.Socket.Bind (EndPoint localEP)
at UdpReceive.Clas s1.StartReceive From() in
c:\myprojects\d otnet\cs\networ king

Is it possible to listen to the same port with two apps at once?


Try to set the following option before calling bind :
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);


Some explenation.

You must always bind before you can receive. For connection-oriented
sockets (TCP) this might be done implicitly by a connect call or

explicitly by calling bind before calling listen.

For connection-less oriented sockets (UDP):
To receive unicast udp, you bind to a local addr/port to which udp packets are sent.

To receive multicast udp, eg sent to 239.255.255.250 :1900, you add the ip
address as a multicast group(socketopt ions) and then bind to the local port 1900.

Bind allows only one process to bind to one local addr/port pair.

This is ussally fine, but multicast means for everyone, so it makes sense
that multiple processes (aplications) can receive them. SO_REUSEADDR makes this possible. It allows multiple processes to receive the same broadcast
packet.

Note that even when using the SO_REUSEADDR option unicast udp packets will
only be received by one socket. This because unicast means only one.
Usually the last bounded socket receives it.

Now to set the socket option SO_REUSEADDR in c# call:

Socket _socket;
...
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
...

1 only indicates true.

When you set this option, you indicate that another process may bind to the same local addr/port.
HTH,
Greetings


HTH,
greetings


Thank you

p.s. One way I could do this is to use a packet driver and something like WinPCap and just grab the frames from any app I have -- but I'd like

to try
this strictly from the DotNet Framework and C#.



Nov 15 '05 #7
Hi,

"mBird" <no@spam.com> wrote in message
news:H4******** ************@tw ister.nyroc.rr. com...
Hi --

Thanks for the info --

I tried using the SetSocketOption . To test it I did:
-Created a test listener
-Launched the testlistener.ex e twice (so now two apps listening).

Both launched OK (so no longer get errror message "Only one usage of each
socket address (protocol/network address/port) is normally permitted")

But only one app instance sees UDP messages (they are simple console apps
and one sees and prints my UPD messages while the other apps instance just
sits there and does nothing)???
This would indicate that the message is a unicast send. Are you sure that
the trap messages are broadcasted, I don't know much about snmp, after some
reading it seems most snmp agents let you specify a list of host addresses
to send the traps to. This is not the same as broadcasting.

Broadcast messages are send to addresses like 192.168.0.255,
255.255.255.255 . Message sent to these addresses can be received by
multiple applications on the same system using SO_REUSEADDR.
The same is true for multicasted messages send to addressses starting with
224 through 239.

If however the messages are sent to a host like 192.168.0.1, then indeed
there is no easy way to make two applications receive them both. If that's
the case then go with Micheal's answer.

HTH,
greetings

Here is the code for a test client I used -- any ideas greatly appretiated! Thanks

public void StartReceiveFro m()
{
IPHostEntry localHostEntry;

try
{
Socket soUdp =
new Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p);
soUdp.SetSocket Option(SocketOp tionLevel.Socke t,
SocketOptionNam e.ReuseAddress, 1);

try
{
localHostEntry = Dns.GetHostByNa me(Dns.GetHostN ame());
}
catch(Exception )
{
Console.WriteLi ne("Local Host not found");
return ;
}

IPEndPoint localIpEndPoint =
new IPEndPoint(loca lHostEntry.Addr essList[0], UdpPort);

soUdp.Bind(loca lIpEndPoint);

while (true)
{
Byte[] received = new Byte[1500];

IPEndPoint tmpIpEndPoint =
new IPEndPoint(loca lHostEntry.Addr essList[0], UdpPort);
EndPoint remoteEP = (tmpIpEndPoint) ;
int bytesReceived = soUdp.ReceiveFr om(received, ref remoteEP);

String dataReceived = System.Text.Enc oding.ASCII.Get String(received );
Console.WriteLi ne(dataReceived .Substring(i, j-i));
}
}
catch (SocketExceptio n se)
{
Console.WriteLi ne("Socket Exception:\n" + se.ToString());
}
}
"BMermuys" <bm************ **@hotmail.com> wrote in message
news:mP******** *************@p hobos.telenet-ops.be...
Hi,

"BMermuys" <bm************ **@hotmail.com> wrote in message
news:wM******** *************@p hobos.telenet-ops.be...
Hi,

"mBird" <no@spam.com> wrote in message
news:Ez******** *************@t wister.nyroc.rr .com...
> I wrote a service that listens for broadcast messages from my firewall (UDP
> snmp trap messages) and parses and puts the data in an database.
>
> I'd also like to write an app that is a simple form that can listen in when
> it runs (so I can see messages in a form as they occur.)
>
> So I need the ability to listen to a UDP port with two apps at the same time
> (the service and my app). But when I do that I get an error:
> System.Net.Sock ets.SocketExcep tion: Only one usage of each socket

address
> (protocol/network address/port) is normally permitted
> at System.Net.Sock ets.Socket.Bind (EndPoint localEP)
> at UdpReceive.Clas s1.StartReceive From() in
> c:\myprojects\d otnet\cs\networ king
>
> Is it possible to listen to the same port with two apps at once?

Try to set the following option before calling bind :
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);


Some explenation.

You must always bind before you can receive. For connection-oriented
sockets (TCP) this might be done implicitly by a connect call or

explicitly
by calling bind before calling listen.

For connection-less oriented sockets (UDP):
To receive unicast udp, you bind to a local addr/port to which udp

packets
are sent.

To receive multicast udp, eg sent to 239.255.255.250 :1900, you add the ip
address as a multicast group(socketopt ions) and then bind to the local

port
1900.

Bind allows only one process to bind to one local addr/port pair.

This is ussally fine, but multicast means for everyone, so it makes sense that multiple processes (aplications) can receive them. SO_REUSEADDR

makes
this possible. It allows multiple processes to receive the same broadcast packet.

Note that even when using the SO_REUSEADDR option unicast udp packets will only be received by one socket. This because unicast means only one.
Usually the last bounded socket receives it.

Now to set the socket option SO_REUSEADDR in c# call:

Socket _socket;
...
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);
...

1 only indicates true.

When you set this option, you indicate that another process may bind to

the
same local addr/port.
HTH,
Greetings


HTH,
greetings
>
> Thank you
>
> p.s. One way I could do this is to use a packet driver and something

like
> WinPCap and just grab the frames from any app I have -- but I'd like

to try
> this strictly from the DotNet Framework and C#.
>
>



Nov 15 '05 #8
Hi --

You wrote:
This would indicate that the message is a unicast send.
....


That did it! I just changed my firewall to broadcast to 192.168.1.255
instead of just to my PC (192.168.1.100) and it works great! This is just my
home firewall so no problem broadcasting to the entire LAN at x.x.x.255

Thanks for the great help you gave -- it is appretiated.
Nov 15 '05 #9
Thanks for the info.
I got the issue of multiple apps figured out from help from BMermuy's post
(12/9/03) but I also like your idea and I was wondering what the best way to
send a message would be. I can think of two ways:
1.) rebroadcast the incoming UDP message on a different port and have my
other app lust listen for it there
2.) use Remoting
Is there a way to send message beside those and if so please recommend best
way.
Thank you fopr you help!
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:%2******** **********@tk2m sftngp13.phx.gb l...
"mBird" <no@spam.com> wrote in message news:Ez******** *************@t wister.nyroc.rr .com...
I would have thought the best method would be to have the service send a message to your form.
--
Michael Culley

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
15659
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port 1234, but each instance binds to a different ip address. that is to say: instance #1 binds to 192.168.1.5/port 1234 instance #2 binds to...
0
428
by: msavoj | last post by:
I have an application that needs to be listening at a specific port. I don't know for sure if another application running at the same desktop would listen at the same port or not. Is it possible that 2 different applications listen at the same port. My app is in C# and I can add some sort of prefix to my messages to identify them as...
3
1908
by: Tulga Kalayci | last post by:
Hi All, I need advise on how to proceed in a area which I have no experience. The problem is related to a call center solution. When a call comes, call center software + server handles call and redirects it to first avaliable agent. No problem here. But I also want to popup a screen on this agent's computer with related data. Now, how should...
0
387
by: sherif.elian | last post by:
hi all i want to listen to a port to get the data passong throught it for example to listen to port 80 to see all data the computer is receiveing throught the http how can i do that in dotnet. thanx
7
7540
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets, uses the same server port. I assumed that a new connection will receive a unique port. If this is the way its suppose to work, is it a performance...
0
1158
by: MuhammadUsman | last post by:
Hi I am writing a simple client server program in c# using sockets over UDP. Can I use same port for both the client and server to send and receive data (like in TCP) . I am trying to do the following but its not working. sockListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); ...
3
10270
by: Jason | last post by:
Hello I've got some test code that I've found on the web that is a TCP server and a TCP client. The server sends data to the client on the port i specify. I know because get the port number through an IPEndPoint. However when I send the data back to the server, the server receives it on a different port number then what I specify, and I...
9
364
by: markgoldin | last post by:
I am looking for code to listen to a tcp port. Another (not .net) process will be writing to that port on the same computer and I need c# code to be able to get data, which is a simple string, from that port. I am not a c# coder but can easy adapt working sample. Thanks alot for help.
1
1555
by: zacksoniar | last post by:
Hi everybody, I have created one web site project.In that i have added two existing websites. when i run it, it starts two development servers with different port nos in VS. How can i get both web sites to have same port no in development environment coz it is making call to each other & i need to debug it.Because of different port...
0
7617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7779
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7714
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5920
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5305
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4924
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
674
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.