S
S
Senture2018-10-25 21:24:17
C++ / C#
Senture, 2018-10-25 21:24:17

Can't send a message from one PC to another connected to the Internet via UDP?

There are 2 applications server and client:

Server
class Program
    {
        static void Main(string[] args)
        {
            UDPSocket s = new UDPSocket();
            s.Server(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Сервер запущен, ожидайте...");

            Console.ReadKey();
        }
    }

 class UDPSocket
    {
        private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        private const int bufSize = 8 * 1024;
        private State state = new State();
        private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
        private AsyncCallback recv = null;
        private List<Clients> clients = new List<Clients>();

        public void AddUser(Clients client)
        {
            clients.Add(client);
        }

        public class State
        {
            public byte[] buffer = new byte[bufSize];
        }

        public void Server(string address, int port)
        {
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            socket.Bind(new IPEndPoint(IPAddress.Parse(address), port));
            Recive();
        }

        private void Recive()
        {
                socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
                {
                    State so = (State)ar.AsyncState;
                    int bytes = socket.EndReceiveFrom(ar, ref epFrom);
                    socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
                    Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
                    BroadcastMessage(epFrom, Encoding.ASCII.GetString(so.buffer, 0, bytes));
                }, state);
        }

        public void BroadcastMessage(EndPoint end, string msg)
        {
            byte[] data = Encoding.ASCII.GetBytes(msg);
            for(int i = 0; i < clients.Count; i++)
            {
                if (clients[i].epFrom != end)
                    socket.BeginSendTo(state.buffer, 0, bufSize, SocketFlags.None, clients[i].epFrom, (ar) =>
                    {
                        State so = (State)ar.AsyncState;
                        int bytes = socket.EndSend(ar);
                    }, state);
            }
        }
    }

    class Clients
    {
        public EndPoint epFrom = null;
        UDPSocket udpSocket = null;

        public Clients(EndPoint end, UDPSocket udp)
        {
            epFrom = end;
            udpSocket = udp;
            udpSocket.AddUser(this);
        }
    }
Customer
class Program
    {
        static void Main(string[] args)
        {
            UDPSocket c = new UDPSocket();
            c.Client(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()));
            c.Send("TEST!");

            Console.ReadKey();
        }
    }
    class UDPSocket
    {
        private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        private const int bufSize = 8 * 1024;
        private State state = new State();
        private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
        private AsyncCallback recv = null;

        public class State
        {
            public byte[] buffer = new byte[bufSize];
        }


        public void Client(string address, int port)
        {
            socket.Connect(IPAddress.Parse(address), port);
            Recive();
        }

        public void Send(string text)
        {
            byte[] data = Encoding.ASCII.GetBytes(text);
            socket.BeginSend(data, 0, data.Length, SocketFlags.None, (ar) =>
            {
                State so = (State)ar.AsyncState;
                int bytes = socket.EndSend(ar);
                Console.WriteLine("SEND: {0}, {1}", bytes, text);
            }, state);
        }

        private void Recive()
        {
                socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
                {
                    State so = (State)ar.AsyncState;
                    int bytes = socket.EndReceiveFrom(ar, ref epFrom);
                    socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
                    Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
                }, state);
        }
    }

Everything works on the local machine, but when you try to send a message from the client to the server (a PC from different cities and on the PC that runs the application server, the port (on the screenshots) is open), the message does not reach the server.
We start the server like this: ip 127.0.0.1, and port 48855 or 10308 (both open).
We start the client like this: we look at the ip of the PC on which the server is installed through the 2ip site, and specify the port 48855 or 10308, but the message is not sent. They tried to run it this way, but it throws out exceptions:
5bd209691484f037350794.png
And on the 2nd screen, ip is taken from the site 2ip
5bd2099450284486323895.png
Tell me what am I doing wrong?
PS Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Senture, 2018-10-26
@Senture

Answer found:
The ip in the server application must be set to 0.0.0.0 to listen on all interfaces. Actually, that's all.

1
15432, 2018-10-25
@15432

Yes, a lot of everything. The firewall in Windows cuts any such external requests, the computer can be turned on through a router, and in order to forward from the router to the PC, you need to configure it, finally, the provider itself can have NAT, and the IP that you see on 2ip.ru belongs to half of the area .. Here, lift VPN, get both computers into it and inside this virtual network, you can get through to each other

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question