简体   繁体   English

c#udp无法接收

[英]c# udp can not receive

I have been searching the internet for a week or two now to find a UDP client program that can send and receive at the same time but there is nothing on this subject for c#. 我一直在互联网上搜索一两个星期,以找到可以同时发送和接收的UDP客户端程序,但是对于C#而言,这个主题没有任何内容。 In the last few days I have tried create a UDP client with a thread that receives. 在最近的几天里,我尝试用接收线程创建一个UDP客户端。

Sending UDP packets works great but the program can not receive for the server i sent to, I believe the server is sending all packets to a different port. 发送UDP数据包效果很好,但该程序无法接收我发送到的服务器,我相信服务器会将所有数据包都发送到另一个端口。

How do i fix this program? 我该如何修复该程序?

Is there an easier way to do UDP programming like StreamReader and StreamWriter for TCP? 有没有更简单的方法来进行诸如TCP的StreamReader和StreamWriter的UDP编程?

    static void CONNECTudp()
    {
        Console.WriteLine("Host:");
        IPAddress ipAddress = Dns.Resolve(Console.ReadLine()).AddressList[0];
        Console.WriteLine("Port:");
        int Port = int.Parse(Console.ReadLine());
        IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Port);
        Thread UDPthread = new Thread(() => CONNECTudpthread(ipEndPoint));
        UDPthread.Start();
        UdpClient udp = new UdpClient();
        do
        {
            Byte[] sendBytes = Encoding.ASCII.GetBytes(Console.ReadLine());
            udp.Send(sendBytes, sendBytes.Length, ipEndPoint);
        } while (true);
    }

    static void CONNECTudpthread(IPEndPoint ipEndPoint)
    {
        UdpClient udp = new UdpClient();
        do
        {
            try
            {
                Byte[] receiveBytes = udp.Receive(ref ipEndPoint);

                string returnData = Encoding.ASCII.GetString(receiveBytes);
                Console.WriteLine(returnData);
            }
            catch (Exception)
            {
            }
        } while (true);
    }

Since UDP is message-oriented and not stream-oriented, there's not really a practical way to use StreamReader and StreamWriter with UDP. 由于UDP是面向消息而不是面向流的,因此,实际上并没有一种将StreamReader和StreamWriter与UDP一起使用的实用方法。 Sticking with the message-oriented I/O as in your example is best. 像您的示例一样,坚持使用面向消息的I / O是最好的。

The problem in your code is that you are using two different UdpClient instances for sending and receiving. 代码中的问题是您正在使用两个不同的UdpClient实例进行发送和接收。 You don't show the UDP server code, so I can't guarantee that's correct either. 您没有显示UDP服务器代码,所以我也不能保证这是正确的。 But if it is, then if you fix your code to be something more like the following, it should work: 但是如果是这样,那么如果您将代码修复为类似于以下内容,则它应该可以工作:

static void CONNECTudp()
{
    Console.WriteLine("Host:");
    IPAddress ipAddress = Dns.Resolve(Console.ReadLine()).AddressList[0];
    Console.WriteLine("Port:");
    int Port = int.Parse(Console.ReadLine());
    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Port);

    // Bind port immediately
    UdpClient udp = new UdpClient(0);

    // Pass UdpClient instance to the thread
    Thread UDPthread = new Thread(() => CONNECTudpthread(udp));
    UDPthread.Start();
    do
    {
        Byte[] sendBytes = Encoding.ASCII.GetBytes(Console.ReadLine());
        udp.Send(sendBytes, sendBytes.Length, ipEndPoint);
    } while (true);
}

static void CONNECTudpthread(UdpClient udp)
{
    do
    {
        try
        {
            // Though it's a "ref", the "remoteEP" argument is really just
            // for returning the address of the sender.
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
            Byte[] receiveBytes = udp.Receive(ref ipEndPoint);

            string returnData = Encoding.ASCII.GetString(receiveBytes);
            Console.WriteLine(returnData);
        }
        catch (Exception)
        {
        }
    } while (true);
}

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

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