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

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

问题描述

我正在尝试提出两个申请.一个将向特定的udp端口发送数据,而另一个将对其进行读取.我有2个问题:

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. 在同一台计算机上运行时,出现错误消息:仅一种用法每个套接字地址(协议/网络地址/端口)的大小通常是允许",所以我需要弄清楚如果我不能同一端口上有多个套接字连接.
  2. 当我尝试使用网络中另一台计算机的内部ip,我没有得到任何阅读都没有.

服务器:

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
        }
}

客户端:

        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.

推荐答案

您不能在同一IP地址/端口上打开2个单独的套接字.可以这么说,每个IP/端口都是一个唯一的邮箱.(从技术上讲,在某些情况下,可以将2个进程连接到同一套接字,但这与您的问题无关).规则很简单,但是解决方法很多.

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.

我会问客户端是否真的需要一个标准的端口号.通常,UDP/TCP客户端仅使用随机套接字并与服务器通信.通常,服务器是连接中唯一需要标准固定端口的部分.UdpClient.Receive()方法将使用数据包发送者的ip/端口填充IPEndPoint,以便您可以回复它.

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.

不要绑定到IPAddress.Any(这将导致它绑定到所有接口).用特定的IP地址替换任何一个(如果您设置了IP别名或多个适配器,则一台计算机可以有多个),或更简单的方法是,将客户端的端口号更改为与服务器不同.您应该能够将一端绑定到主以太网接口IP地址,并将客户端绑定到回送(127.0.0.1)地址.

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套接字通信-每个套接字地址(协议/网络地址/端口)通常仅允许使用一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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