Сервер не может отправить пакет пользователю

я не так уж и давно начал изучать сокеты и столкнулся с парой проблем, на которые я сегодня хотел бы получить решения: 1. Я сделал небольшой "мессенджер" между пользователем и сервером, вот код: сервер

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace GameServer
{
    class Program
    {
        static Socket client;
        static Socket ServerSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("***************************************************");
            Console.WriteLine("***********************СЕРВЕР**********************");
            Console.WriteLine("***************************************************");
            Console.Write("Введите ip: ");
            string ip = Console.ReadLine();
            Console.WriteLine();
            Console.Write("Введите порт: ");
            string port = Console.ReadLine();
            var ServerPoint = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
            try  
            {
                ServerSocket.Bind(ServerPoint);
                ServerSocket.Listen(5);
                Console.WriteLine("Сервер запущен!!!");
                Thread NewUsers = new Thread(UserAccept);
                Thread NewMessages = new Thread(GetMessage);
                NewUsers.Start();
                NewMessages.Start();
            }
            catch(Exception ex)  //!
            {
                Console.WriteLine();
                Console.WriteLine("Ошибка запуска сервера:" + ex.Message);
                Console.WriteLine("Скорее всего порт " + port + " Уже используется другой программой.");
            }
            Console.ReadLine();
        }
        static void UserAccept()
        {
            int size = 0;
            Console.WriteLine("Готово");
                client = ServerSocket.Accept();
                Console.WriteLine("Новый юзер");
            while (true)
            {
                var data = new StringBuilder();
                var buffer = new byte[1024];
                size = client.Receive(buffer);
                Console.WriteLine("~Новое сообщение");
                data.Append(Encoding.Unicode.GetString(buffer, 0, size));
                Console.WriteLine(data);
            }
        }
        static void GetMessage()
        {
            while (true)
            {
                if (client != null)
                {
                    string UrMessage = Console.ReadLine();
                    client.Send(Encoding.Unicode.GetBytes(UrMessage));
                }
            }
        }

    }
}

Клиент

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace client
{
    class Program                                                        //Клиент
    {
        static Socket client = new Socket(SocketType.Stream, ProtocolType.Tcp);
        static void Main(string[] args)
        {
            Console.WriteLine("***************************************************");
            Console.WriteLine("***********************КЛИЕНТ**********************");
            Console.WriteLine("***************************************************");
            Console.Write("Введите ip: ");
            string ip = Console.ReadLine();
            Console.WriteLine();
            Console.Write("Введите порт: ");
            string port = Console.ReadLine();
            Thread GetMess = new Thread(GetMessages);
            GetMess.Start();
            var Server = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
            try
            {
                client.Connect(Server);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Выполненно!");
                Console.ForegroundColor = ConsoleColor.White;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка: " + ex.Message);
                return;
            }
            while (true)
            {
                Console.Write("Введите сообщение: ");
                string Message = Console.ReadLine();
                client.Send(Encoding.Unicode.GetBytes(Message));
            }

        }
        static void GetMessages()
        {
            int size = 0;
            if (client.Connected)
            {
                while (true)
                {
                    var data = new StringBuilder();
                    var buffer = new byte[1024];
                    size = client.Receive(buffer);
                    Console.WriteLine("~Новое сообщение");
                    data.Append(Encoding.Unicode.GetString(buffer, 0, size));
                    Console.WriteLine(data);
                }
            }
        }
    }
}

Когда я запускал сервер и клиент локально, то они оба могли обмениваться сообщениями, но когда я запустил сервер на своём пк, а друг клиент на своём (мы пользовались radmin vpn), то отправлять сообщения мог только друг, мои никуда не попадали. 2. Я хочу знать, от кого я получаю сообщения, изначально я использовал такой код:

//Сервер
Console.Write("Введите сообщение: от " + client.LocalEndPoint);
string Message = Console.ReadLine();
client.Send(Encoding.Unicode.GetBytes(Message));

Но в итоге выводится ip самого сервера.


Ответы (0 шт):