diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 3a3216e..ed77e89 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -47,7 +47,7 @@ namespace ZoFo.GameCore { networkManager.JoinRoom(ip,port); } - public void JoinYourself() { networkManager.JoinYourself(); } + public void JoinYourself(int port) { networkManager.JoinYourself(port); } List mapObjects = new List(); diff --git a/ZoFo/GameCore/GUI/SelectModeMenu.cs b/ZoFo/GameCore/GUI/SelectModeMenu.cs index 6ae2edc..94a9500 100644 --- a/ZoFo/GameCore/GUI/SelectModeMenu.cs +++ b/ZoFo/GameCore/GUI/SelectModeMenu.cs @@ -42,7 +42,7 @@ public class SelectModeMenu : AbstractGUI Server server = new Server(); Client client = new Client(); server.CreateRoom(1); - client.JoinYourself(); + client.JoinYourself(server.MyIp.Port); AppManager.Instance.SetServer(server); AppManager.Instance.SetClient(client); AppManager.Instance.ChangeState(GameState.HostPlaying); diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index ad36b4f..1827f03 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -84,7 +84,7 @@ public class SelectingServerGUI : AbstractGUI Server server = new Server(); //Server Logic MultiPlayer server.CreateRoom(5); AppManager.Instance.SetServer(server); - string key = server.MyIp; + string key = server.MyIp.ToString(); AppManager.Instance.debugHud.Set(key, "MultiPlayer"); // ваш код здесь AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true)); diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index 4c5c6b5..b0a808b 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -32,7 +32,7 @@ public class WaitingForPlayersGUI : AbstractGUI Elements.Add(menuBackground); menuBackground.LoadTexture(AppManager.Instance.Content); // string pcIp = - ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp, fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" }; + ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" }; Elements.Add(ip); if (isHost) { diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index 12ad84d..b55d8cb 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -16,12 +16,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public class ClientNetworkManager { private int port = 0; - private EndPoint endPoint; + private IPEndPoint endPoint; private Socket socket; List updates = new List(); public delegate void OnDataSent(string Data); public event OnDataSent GetDataSent; // event public bool IsConnected { get { return socket.Connected; } } + public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint; public ClientNetworkManager() { @@ -74,9 +75,9 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// создается одиночная комната к которой ты подключаешься /// - public void JoinYourself() // single player + public void JoinYourself(int port) // single player { - endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); + endPoint = new IPEndPoint(GetIp(), port); socket.Connect(endPoint); SendData(); Thread listen = new Thread(StartListening); @@ -84,6 +85,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager listen.Start(); } + public static IPAddress GetIp() + { + string hostName = Dns.GetHostName(); // Retrive the Name of HOST + string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP + return IPAddress.Parse(myIP); + } + //поток 2 public void StartListening() { diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index dbe149d..9b6259e 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -26,7 +26,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public event OnDataSend GetDataSend; // event Dictionary managerThread; Thread serverTheread; - public IPEndPoint InfoConnect => endPoint; + public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint; public ServerNetworkManager() { Init(); } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index edc1022..4b05b1d 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Text.Json; using System.Threading.Tasks; @@ -19,7 +20,7 @@ namespace ZoFo.GameCore { private ServerNetworkManager networkManager; private int ticks = 0; - public string MyIp { get { return networkManager.InfoConnect.ToString(); } } + public IPEndPoint MyIp { get { return networkManager.InfoConnect; } } public Server() { networkManager = new ServerNetworkManager();