设置UDP C#代码

时间:2014-04-19 04:05:38

标签: c# sockets udp

我对使用C#的套接字非常新,我一直在努力尝试使用socket来使用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#程序(在同一台机器上)检索信息时我会执行此代码,它就会冻结。我知道它一直在寻找信息,但是有没有办法在接收命令上设置超时,或者我必须制作一个事件处理程序来防止这种冻结?端口连接正常,不显示任何形式的错误或异常。提前谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

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中的UdpChat演示:UdpChat