c# udp socket 通信 - 每个套接字地址(协议/网络地址/端口)通常只允许使用一次端口、只允许、网络地址、协议

2023-09-08 09:09:50 作者:故事太长太心酸

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:

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. 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
        }
}
Linux Socket 通信 UDP原理

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.

解决方案

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.