Getting socket tcp / udp data

You need to get the data packet to the public IP and return the message.

The code below only handles local requests 192.168.0.0: 1100. When sending data to a public IP (for example, via telnet), the application does not see anything.

The firewall is completely disabled (public, private, and domain profile-all disabled).

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SocketTcpServer
{
    class Program
    {
        static int port = 1100; // порт для приема входящих запросов
        static void Main(string[] args)
        {
            IPEndPoint ipPoint = new IPEndPoint(IPAddress.Any, port);

            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                listenSocket.Bind(ipPoint);
                listenSocket.Listen(10);
                Console.WriteLine("Сервер запущен. Ожидание подключений...");
                while (true)
                {
                    Socket handler = listenSocket.Accept();
                    StringBuilder builder = new StringBuilder();
                    int bytes = 0;
                    byte[] data = new byte[256];
                    do
                    {
                        bytes = handler.Receive(data);
                        builder.Append(Encoding.UTF8.GetString(data, 0, bytes));
                    }
                    while (handler.Available > 0);
                    Console.WriteLine(builder);

                    data = Encoding.ASCII.GetBytes("message");
                    handler.Send(data);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception ex) {Console.WriteLine(ex.Message);}
        }
    }
}

UPD: I use STUN to" punch " the port

STUN = STUN_Client.Query(StunServer, StunServerPort, socket);
Console.WriteLine("Public address: {0}", STUN.PublicEndPoint);

I get the public IP and port automatically, but packets they still don't reach To check, I use the linux commands: echo "hello" > /dev/udp/ip/port and ping

Author: au.tat, 2020-10-23