正在设置UDP C#代码

本文关键字:代码 UDP 设置 | 更新日期: 2023-09-27 17:59:02

我对使用C#的套接字非常陌生,我一直在努力让套接字与localhost一起工作。这是主要的UDP代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApplication1
    {
        class NetUDP
        {
             private UdpClient udp;
             private IPEndPoint endPont;
        public NetUDP(int port_local, int port_global, string ip_global, string ip_local)
        {
            Console.WriteLine(IPAddress.Any.ToString());
            udp = new UdpClient(IPAddress.Any.ToString(), port_global);
            endPont = new IPEndPoint(IPAddress.Parse(ip_global), port_global);
            udp.Connect(ip_global, port_global);
        }
        public NetUDP()
        {
        }

        public void UDPinit(int port_local, int port_global, string ip_global, string ip_local)
        {
            Console.WriteLine();
            udp = new UdpClient("127.0.0.1", port_global);
            endPont = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port_global);
            udp.Connect("127.0.0.1", port_global);
        }
        public void sendDataUDP(string info)
        {
            Byte[] SendByte = Encoding.ASCII.GetBytes(info);
            udp.Send(SendByte, SendByte.Length);
        }
        public string getDataUDP()
        {
            Byte[] get = udp.Receive(ref endPont);
            return Encoding.ASCII.GetString(get);
        }
        public void closeUDP()
        {
           udp.Close();
        }
      }
    }

问题是,每当我尝试从另一个UDP C#程序(在同一台机器上)检索信息时,它都会冻结。我知道它一直在寻找信息,但我知道有一种方法可以在接收命令上设置超时,或者我必须制作一个事件处理程序来防止这种冻结吗?端口连接良好,不会显示任何形式的错误或异常。提前感谢您的帮助!

正在设置UDP C#代码

class Program
{
    static void Main(string[] args)
    {
        var serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8500);
        NetUDP udp = new NetUDP(serverEndPoint);

        UdpClient localClient = new UdpClient();
        Task.Delay(20000);
        var dgram = Encoding.ASCII.GetBytes("Hello Server");
        localClient.Send(dgram, dgram.Length, serverEndPoint);  // Send "Hello Server" to Server
        Console.Read();
    }
}
class NetUDP
{
    private UdpClient udp;
    private IPEndPoint endPont;
    public NetUDP() { }
    public NetUDP(IPEndPoint serverEndPoint)
    {
        udp = new UdpClient(serverEndPoint);  // Bind the EndPoint
        // Receive any IPAddress send to the 8555 port
        IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 8555);
        Task.Run(() =>
            {
                try
                {
                    while (true)
                    {
                        // This Receive will block until receive an UdpClient
                        // So, run in another thread in thread pool
                        var bytes = udp.Receive(ref receiveEndPoint);
                        Console.WriteLine(Encoding.ASCII.GetString(bytes));
                    }
                }
                catch (Exception ex)
                {
                    // TODO
                }
                finally
                {
                    udp.Close();
                }
            });
    }
}

这是github中的UdPhat演示:UdPhat