0

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

  • Do not paste code as links. If it is relevant for the question, add it as text. – Fildor May 26 '21 at 13:23
  • Ok i pasted on answer place – C_Compiler May 26 '21 at 13:46
  • Refactor your code, so there are no more `goto`s and labels. Then forget that C# supports `goto`. – Fildor May 26 '21 at 13:47
  • OK, now you can dete the answer. I edited for you. – Fildor May 26 '21 at 13:51
  • The thing is: any sane person wanting to help will dismiss and tell you "get rid of those gotos, first. Maybe that 's enough to make it work". :( – Fildor May 26 '21 at 13:52
  • I removed gotos and labels – C_Compiler May 26 '21 at 13:58
  • _"it still does not work."_ -- "does not work" is not a useful problem statement. Also, your code example _must_ be self-contained so that another SO user can copy and paste the code into an empty project and with minimal extra effort produce a running example that reliably reproduces your problem. Please fix your post so that it includes a proper [mcve] along with a clear explanation of what the code does now, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho May 26 '21 at 16:42
  • In the meantime see duplicate for the answer to your question as it's asked now. I.e. there's all of the details you need there in the answer to that question for you to correctly implement UDP hole punching. – Peter Duniho May 26 '21 at 16:43

0 Answers0