Hi --
Thanks for the info --
I tried using the SetSocketOption. To test it I did:
-Created a test listener
-Launched the testlistener.exe 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 StartReceiveFrom()
{
IPHostEntry localHostEntry;
try
{
Socket soUdp =
new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
soUdp.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
try
{
localHostEntry = Dns.GetHostByName(Dns.GetHostName());
}
catch(Exception)
{
Console.WriteLine("Local Host not found");
return ;
}
IPEndPoint localIpEndPoint =
new IPEndPoint(localHostEntry.AddressList[0], UdpPort);
soUdp.Bind(localIpEndPoint);
while (true)
{
Byte[] received = new Byte[1500];
IPEndPoint tmpIpEndPoint =
new IPEndPoint(localHostEntry.AddressList[0], UdpPort);
EndPoint remoteEP = (tmpIpEndPoint);
int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
String dataReceived = System.Text.Encoding.ASCII.GetString(received);
Console.WriteLine(dataReceived.Substring(i, j-i));
}
}
catch (SocketException se)
{
Console.WriteLine("Socket Exception:\n" + se.ToString());
}
}
"BMermuys" <bm**************@hotmail.com> wrote in message
news:mP*********************@phobos.telenet-ops.be...
Hi,
"BMermuys" <bm**************@hotmail.com> wrote in message
news:wM*********************@phobos.telenet-ops.be... Hi,
"mBird" <no@spam.com> wrote in message
news:Ez*********************@twister.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.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at UdpReceive.Class1.StartReceiveFrom() in
c:\myprojects\dotnet\cs\networking
Is it possible to listen to the same port with two apps at once?
Try to set the following option before calling bind :
_socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.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(socketoptions) 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.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.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#.