简体   繁体   English

客户端-使用UDP的同一台计算机上的服务器

[英]Client - Server on same machine using UDP

I am trying to simulate a Client - Server scenario on my machine in c#. 我正在尝试在C#的计算机上模拟客户端-服务器方案。 But when i am executing it an exception pops up saying: 但是当我执行它时,弹出一个异常说:

No such host is known 尚无此类主机

My code: 我的代码:

namespace TCPClient
{
    public class Program
    {
        public static void Main(string[] args)
        {
            UdpClient udpc = new UdpClient(args[0], 2055);
            IPEndPoint ep = null;
            while (true)
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                if (name == "") break;
                byte[] sdata = Encoding.ASCII.GetBytes(name);
                udpc.Send(sdata, sdata.Length);
                byte[] rdata = udpc.Receive(ref ep);
                string job = Encoding.ASCII.GetString(rdata);
                Console.WriteLine(job);
            }
        }
    }
}

I don't understand where I'm going wrong. 我不明白我要去哪里错了。

thanks Dev's ! 感谢Dev的! your answers were helpful, however I found an easiest way for the same. 您的回答很有帮助,但是我找到了最简单的方法。

 public class Program
 {
    public static void Main(string[] args)
    {
        UdpClient udpc = new UdpClient( System.Net.Dns.GetHostName(), 2055);
        IPEndPoint ep = null;
        while (true)
        {
            Console.Write("Name: ");
            string name = Console.ReadLine();
            if (name == "") break;
            byte[] sdata = Encoding.ASCII.GetBytes(name);
            udpc.Send(sdata, sdata.Length);
            byte[] rdata = udpc.Receive(ref ep);
            string job = Encoding.ASCII.GetString(rdata);
            Console.WriteLine(job);
        }
    }
 }

Isolate the issue. 隔离问题。 You're calling new UdpClient(args[0], 2055) and udpc.Receive(ref ep) that can throw this exception, but don't say which one does. 您正在调用new UdpClient(args[0], 2055)udpc.Receive(ref ep) ,它们可能会引发此异常,但不要说是哪个异常。 Either debug it or try it with a constant string: 调试它或尝试使用常量字符串:

string host = args[0];
new UdpClient(host, 2055);

You'll then see that host is most probably not an existing hostname. 然后,您会看到该host很可能不是现有主机名。 If it is, check what you are doing with ep : nothing, so it will be null . 如果是这样,请检查您在用ep做什么:什么都没有,所以它将为null I guess you'll want to listen to any UDP datagram as explained in the documentation , so specify the endpoint: 我想您会想要听文档中说明的所有UDP数据报,因此请指定端点:

ep = new IPEndPoint(IPAddress.Any, 0);

I do believe your issue lies within this call: 我相信您的问题在于此电话中:

byte[] rdata = udpc.Receive(ref ep)

The problem is that, in order to be able to listen to any incoming content, you need first to bind the UdpClient to a valid endpoint - like this: 问题在于,为了能够收听任何传入的内容,您需要首先将UdpClient绑定到有效的端点-如下所示:

IPEndPoint ep = new IPEndPoint(IPAddress.Any, 8192);
//You will be listening to port 8192.

Also, keep in mind that you can't both listen AND emit from the same UdpClient; 另外,请记住,您不能同时收听和发送来自同一UdpClient的消息。 You'll need two clients, and if you want to use the same IP Port for both, you'll need to initialize the class using the SocketOptionName.ReuseAddress parameter. 您将需要两个客户端,并且如果要为两个客户端使用相同的IP端口,则需要使用SocketOptionName.ReuseAddress参数初始化该类。 A good example is provided on the following post: 以下帖子提供了一个很好的示例:

Connecting two UDP clients to one port (Send and Receive) 将两个UDP客户端连接到一个端口(发送和接收)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM