首页 > 解决方案 > 如何在 C# 上进行 UDP 打孔

问题描述

我有一个控制台应用程序我的主要代码:

class Program { 
    static FrevanceUDP.UDPConnector udpserver; 
    static FrevanceUDP.UDPConnectorClient udpclient;

    static void Main(string[] args)
    {
        Console.WriteLine("Local UDP Server Port : ");
        if (int.TryParse(applyParse(Console.ReadLine()),out int acilcakport))
        {
            udpserver = new FrevanceUDP.UDPConnector(acilcakport);
            Console.WriteLine("Remote UDP Server IP : ");
            if (IPAddress.TryParse(applyParse(Console.ReadLine()),out IPAddress uzakserverip)){
                Console.WriteLine("Remote UDP Server Port : ");
                if(int.TryParse(applyParse(Console.ReadLine()),out int uzakserverport))
                {
                    udpclient = new FrevanceUDP.UDPConnectorClient(uzakserverip,uzakserverport);
                    udpclient.OnData += DataC;
                    while (true)
                    {
                        string c = Console.ReadLine();
                        udpserver.SendData(Encoding.UTF8.GetBytes(c));
                    }
                }
           }
       }  
    }

    private static void DataC(byte[] gelenveri)
    {
        try
        {
            string gelenveris = Encoding.UTF8.GetString(gelenveri);
            Logger.GetLogger().LogAl("Gelen Yazı : " + gelenveris);
        }
        catch (Exception)
        {
            Logger.GetLogger().LogAl("Gelen Veri Yazı Türünde Değil !");
        }
    }

    static string applyParse(string data)
    {
        return data.Replace(" ", "").Replace(Environment.NewLine, "");
    }
}

我的 UDPConnector 和 UDPConnectorClient 代码

public class UDPConnectorClient
{
    private int BufferLenght = 4096;
    private int ServerPort = 11000;
    private IPAddress serverip;

    private bool UseThread = false;

    private Socket datasendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    public delegate void DataGet(byte[] gelenveri);
    public DataGet OnData;

    public UDPConnectorClient(IPAddress ipc,int port = 11000, bool thread = true, int bufferlenght = 4096)
    {
        serverip = ipc;
        BufferLenght = bufferlenght;
        ServerPort = port;
        UseThread = thread;
        if (thread)
        {
            new Thread((() => 
            {
                while (true)
                {
                    IPEndPoint sending_end_point = new IPEndPoint(serverip, ServerPort);
                    datasendsocket.SendTo(new byte[] { 0 }, sending_end_point);
                    var newport = datasendsocket.LocalEndPoint.ToString().Split(':')[1];
                    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(newport));
                    byte[] buffer = new byte[BufferLenght];
                    datasendsocket.Receive(buffer);
                    Logger.GetLogger().LogAl("Gelen Veri : " + buffer.ToString());
                    OnData(buffer);
                }
            })) { IsBackground = false }.Start();
        }
    }
}

public class UDPConnector
{
    private int BufferLenght = 4096;
    private int ServerPort = 11000;

    private bool UseThread = false;

    public UDPConnector(int port = 11000,bool thread = true,int bufferlenght = 4096)
    {
        BufferLenght = bufferlenght;
        ServerPort = port;
        UseThread = thread;
    }

    public void SendData(byte[] data)
    {
        var info = GetClientInfo();
        UdpClient newClient = ConstructUdpClient(info);
        newClient.Send(data, data.Length);
        Logger.GetLogger().LogAl("Giden Veri : " + data.ToString());
    }

    private IPEndPoint GetClientInfo()
    {
        using (UdpClient listener = new UdpClient(ServerPort))
        {
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, ServerPort);
            byte[] receive_byte_array = listener.Receive(ref groupEP);
            return groupEP;
        }
    }

    private UdpClient ConstructUdpClient(IPEndPoint clientInfo)
    {
        var ip = clientInfo.Address.ToString();
        var port = clientInfo.Port;
        UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, ServerPort));
        client.Connect(ip, port);

        return client;
    }
}

我无法发送或接收数据。我需要使用 UDP 打孔,这不起作用:(

我找到了这段代码 [https://try2explore.com/questions/11064423] 并进行了更改,但它仍然无法正常工作。

我在 Windows 10/8.1/7 上测试了这段代码,但没有工作

标签: c#networkingtcpudpconsole-application

解决方案


推荐阅读