Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

c# udp socket communication - Only one usage of each socket address (protocol/network address/port) is normally permitted

I'm trying to make two applications. One will be sending data to a specific udp port, and the other will be reading it. I am having 2 problems:

  1. when running on the same machine, I get an error: "Only one usage of each socket address (protocol/network address/port) is normally permitted" so I need to figure out how to test it if I cannot have multiple socket connections at the same port.
  2. when I tried using an internal ip of another computer within my network, I did not get any reading at all.

server:

private Socket sock;
private const int PORT = 5000;
public void start()
{
        sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        sock.ReceiveTimeout = 1;// seconds
        sock.SendTimeout = 1;// seconds
        IPEndPoint iep = new IPEndPoint(IPAddress.Any, PORT);
        EndPoint ep = (EndPoint)iep;
        sock.Bind(iep);
        MulticastView view_obj = new MulticastView();
        while(true)
        {
            try
            {
                if (sock.Connected)
                {
                    sock.Send(Serializer.ObjectToByteArray(view_obj));
                }
            }catch(Exception ex){
                Console.WriteLine(ex);
            }
            Thread.Sleep(1000); // milliseconds
        }
}

client:

        IPAddress ip = IPAddress.Parse("127.0.0.1");
        IPEndPoint iep = new IPEndPoint(ip, PORT);
        EndPoint ep = (EndPoint)iep;
        UdpClient client = new UdpClient(PORT);
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        // or using: Byte[] receiveBytes = client.Receive(ref ep); 
        Byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint); 
        MulticastView view;
        view = (MulticastView)Serializer.ByteArrayToObject(receiveBytes);

Note that I need separate apps (on separate processes). Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't have 2 separate sockets open on the same IP address / port. Each IP/Port is a unique mailbox, so to speak. (Technically, it is possible to have 2 processes attached to the same socket, under certain circumstances, but that isn't relevant to your problem). The rule is simple, but the workarounds are several.

I would ask if the client really needs to have a standard port number. Normally, UDP/TCP clients just use a random socket and communicate with the server. The server is usually the only side of the connection that needs a standard, fixed port. The UdpClient.Receive() method will populate the IPEndPoint with the ip/port of the sender of the packet so that you can reply to it.

Either don't bind to IPAddress.Any (that causes it to bind to all interfaces). Replace the Any with a specific IP address (one machine can have multiple if you setup IP aliasing or multiple adapters), or simpler, change the port number of the client to differ from the server. You should be able to bind one end to the primary ethernet interface IP address, and bind the client to the loopback (127.0.0.1) address.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...