c# udp socket 通信 - 每个套接字地址(协议/网络地址/端口)通常只允许使用一次 [英] c# udp socket communication - Only one usage of each socket address (protocol/network address/port) is normally permitted

查看:19
本文介绍了c# udp socket 通信 - 每个套接字地址(协议/网络地址/端口)通常只允许使用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

解决方案

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.

这篇关于c# udp socket 通信 - 每个套接字地址(协议/网络地址/端口)通常只允许使用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆