Can't send a udp packet to the local PC?

S

Sin Jeong-hun

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
//ServerPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),
50000); //<---(1) This has no effect.
ServerPoint = new IPEndPoint(IPAddress.Parse("100.1.2.3"),
50000); //<---(2) This works.
int sent = s.SendTo(new byte[100], ServerPoint);
Debug.WriteLine("===> " + sent + " bytes sent");

After two hours of head scratching, I'm finally asking here. I tried
to test a simple UDP server / client applications on my PC. But I
couldn't send a UDP packet to the local (the same pc where the sending
application is on) pc. If I set the ServerPoint with line (1), even
though SendTo returns 100, I cannot see an outgoing UDP packet on the
Ethereal. Only if I change the destination address to another PC like
line (2), does a UDP packet go out. What is wrong with the code above?
Of course I don't think there is a limitation as two local
applications cannot communicate with UDP, since I did that once with
Java a few years ago.

I tried UdpClient (of course I didn't use the same port for both
client/server), my public IP address instead of 127.0.0.1,
IPAddress.Loopback, Dns.GetHostByName("localhost"), etc but the result
was the same.

Thank you for any hint.
 
S

Sin Jeong-hun

                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
                        //ServerPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),
50000); //<---(1) This has no effect.
                        ServerPoint = new IPEndPoint(IPAddress.Parse("100.1.2.3"),
50000); //<---(2) This works.
                        int sent = s.SendTo(newbyte[100], ServerPoint);
                        Debug.WriteLine("===> " + sent + " bytes sent");

After two hours of head scratching, I'm finally asking here. I tried
to test a simple UDP server / client applications on my PC. But I
couldn't send a UDP packet to the local (the same pc where the sending
application is on) pc. If I set the ServerPoint with line (1), even
though SendTo returns 100, I cannot see an outgoing UDP packet on the
Ethereal. Only if I change the destination address to another PC like
line (2), does a UDP packet go out. What is wrong with the code above?
Of course I don't think there is a limitation as two local
applications cannot communicate with UDP, since I did that once with
Java a few years ago.

I tried UdpClient (of course I didn't use the same port for both
client/server), my public IP address instead of 127.0.0.1,
IPAddress.Loopback, Dns.GetHostByName("localhost"), etc but the result
was the same.

Thank you for any hint.

It looks like that when the destination address is the same PC, the
UDP stack doesn't pass down the packet to the IP layer, but handles it
on its own. For some complicated reason, I want it to pass down the
packet to the physical Ethernet interface so that Ethereal can catch
the frame. I searched MSDN and found that there is a socket option
named UseLookback. I tried to set it false, but an exception has
occurred, and I cannot find any example to do this on Google.

The followings are what I have tried.
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.UseLoopback,
0);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.UseLoopback,
false);
s.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.UseLoopback,
0);
s.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.UseLoopback,
false);
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.UseLoopback, 0);
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.UseLoopback, false);
All of them throws an exception saying something like invalid option
level or invalid value.
 

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

Similar Threads


Top