c# - 如何在 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 上测试了这段代码,但没有工作
解决方案
推荐阅读
- scheme - 从 call/cc 返回延续
- python - 如何从输入中获取布尔值?
- javascript - 为什么我在“console.log”对象时看不到从父对象继承的那些属性;在javascript中
- flutter - 颤振:使用加速度计时基数 10 无效
- java - 使用 Spring Security 使用 Telegram 登录
- vba - 如何更新多张幻灯片上的 ActiveX 标签?
- java - 启动Tomcat时加载Java类
- powershell - 尝试在 Powershell 中签署代码时出现 UnknownError
- vba - 循环浏览下拉列表
- python - 如何加快枕头(python)中的图像加载速度?