简体   繁体   English

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

[英]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.一个将数据发送到特定的 udp 端口​​,另一个将读取它。 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.当我尝试在我的网络中使用另一台计算机的内部 ip 时,我根本没有得到任何读数。

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.您不能在同一个 IP 地址/端口上打开 2 个单独的套接字。 Each IP/Port is a unique mailbox, so to speak.可以这么说,每个 IP/端口都是一个唯一的邮箱。 (Technically, it is possible to have 2 processes attached to the same socket, under certain circumstances, but that isn't relevant to your problem). (从技术上讲,在某些情况下,可以将 2 个进程连接到同一个套接字,但这与您的问题无关)。 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.通常,UDP/TCP 客户端只是使用随机套接字与服务器通信。 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. UdpClient.Receive() 方法将使用数据包发送者的 IP/端口填充 IPEndPoint,以便您可以回复它。

Either don't bind to IPAddress.Any (that causes it to bind to all interfaces).要么不绑定到 IPAddress.Any(这会导致它绑定到所有接口)。 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.将 Any 替换为特定的 IP 地址(如果设置 IP 别名或多个适配器,一台机器可以有多个),或者更简单,将客户端的端口号更改为与服务器不同。 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.您应该能够将一端绑定到主以太网接口 IP 地址,并将客户端绑定到环回 (127.0.0.1) 地址。

Even though this is an old question, I think it would be good to point out an exception to the one port rule.尽管这是一个老问题,但我认为指出一个端口规则的例外情况会很好。

If you are using multicast (which is UDP to a multicast address), you can set the SO_REUSEADDR option to use the port for multiple sockets.如果您使用多播(UDP 到多播地址),您可以设置 SO_REUSEADDR 选项以将端口用于多个套接字。

该帖子已被作者删除,因为它没有进行任何访问,因此无法解决任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#中通常只允许每个套接字地址(协议/网络地址/端口)使用一次 - Only one usage of each socket address (protocol/network address/port) is normally permitted in c# C#远程处理异常:每个套接字地址(协议/网络地址/端口)通常仅允许使用一种 - C# Remoting Exception : Only one usage of each socket address (protocol/network address/port) is normally permitted 在 Redis C# 中批量创建密钥 - SocketException:通常每个套接字地址(协议/网络地址/端口)只允许使用一次 - bulk creating keys in Redis C# - SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted UDP客户端 - 创建socketOnly时的异常通常允许使用每个套接字地址(协议/网络地址/端口) - UDP Client - Exception in creating socketOnly one usage of each socket address (protocol/network address/port) is normally permitted TCP套接字错误:通常只允许每个套接字地址(协议/网络地址/端口)使用一种 - TCP Socket Error: Only one usage of each socket address (protocol/network address/port) is normally permitted 通常,每个套接字地址(协议/网络地址/端口)只能使用一种 - Only one usage of each socket address (protocol/network address/port) is normally permitted 通常,每个套接字地址(协议/网络地址/端口)只能使用一种 - Only one usage of each socket address (protocol/network address/port) is normally permitted 通常,每个套接字地址(协议/网络地址/端口)只能使用一种 - Only one usage of each socket address (protocol/network address/port) is normally permitted TcpListener:通常只允许每个套接字地址(协议/网络地址/端口)使用一种 - TcpListener : Only one usage of each socket address (protocol/network address/port) is normally permitted TCP Server:“通常只允许每个套接字地址(协议/网络地址/端口)使用一种。” - TCP Server : “Only one usage of each socket address (protocol/network address/port) is normally permitted.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM