I Have A Console App My Main code:
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, "");
}
}
My UDPConnector and UDPConnectorClient code
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;
}
}
I cannot send or receive data. I need to use UDP Hole Punching and this does not work :(
I found this code [https://try2explore.com/questions/11064423] and changed but it still does not work.
I tested this code on windows 10/8.1/7 but not working