static void Main(string[] args) { Console.Title = "GD Multiplayer - By Bonkeyzz -- Ver 1.0b"; MainFunctions mf = new MainFunctions(); Program prg = new Program(); InitializeVariables(); Thread tmain = new Thread(prg.thread_InitializeTCP); while (!new Config().Success) { mf.ConsoleWrite("[GDMP] Please Select Mode[Client = 2/Host = 1]:", ConsoleColor.Cyan); try { iType = Convert.ToInt32(Console.ReadLine()); if (iType == (int)Config.Types.TCP_HOST) { Console.Title = "GD Multiplayer[SERVERMODE] - By Bonkeyzz -- Ver 1.0b"; mf.ConsoleWrite("[GDMP] Enter the IP Address for the server: ", ConsoleColor.Cyan); Config.sIPAddress = Console.ReadLine(); if (!string.IsNullOrEmpty(Config.sIPAddress) && Config.sIPAddress.Contains(".")) { mf.ConsoleWrite("[GDMP] Starting...", ConsoleColor.Cyan); tmain.Start(); break; } else { mf.ConsoleWrite("[GDMP] IP Address is invalid! Try again.", ConsoleColor.Red); } } else if (iType == (int)Config.Types.TCP_CLIENT) { Console.Title = "GD Multiplayer[CLIENTMODE] - By Bonkeyzz -- Ver 1.0b"; mf.ConsoleWrite("[GDMP] Enter the IP Address to connect: ", ConsoleColor.Cyan); Config.sIPAddress = Console.ReadLine(); if (!string.IsNullOrEmpty(Config.sIPAddress)) { mf.ConsoleWrite("[GDMP] Connecting...", ConsoleColor.Cyan); tmain.Start(); break; } else { mf.ConsoleWrite("[GDMP] IP Address is invalid! Try again.", ConsoleColor.Red); } } else { mf.ConsoleWrite("[GDMP] Incorrect Type -> 1-1-6", ConsoleColor.Red); iType = 0; } } catch { mf.ConsoleWrite("[GDMP] Incorrect Type -> 1-2-2", ConsoleColor.Red); } } }
void thread_InitializeTCP() { Config cfg = new Config(); MainFunctions mf = new MainFunctions(); Thread keylistener = new Thread(thread_KeyPressDetection); Thread tcpthread = new Thread(thread_tcpreceivesend); cfg.IPEnd = new IPEndPoint(IPAddress.Parse(Config.sIPAddress), cfg.iPort); switch (iType) { case (int)Config.Types.TCP_HOST: { new MainFunctions().ConsoleWrite($"[GDMP Log] -> Detected Type: 1", ConsoleColor.White); using (Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { new MainFunctions().ConsoleWrite( $"[GDMP Log] -> Bound IP Address: {cfg.IPEnd.Address} with Port: {cfg.IPEnd.Port}", ConsoleColor.White); server.Bind(cfg.IPEnd); new MainFunctions().ConsoleWrite($"[GDMP Log] -> Listening for 1 client...", ConsoleColor.White); server.Listen(1); // Number of players that can join. new MainFunctions().ConsoleWrite($"[GDMP] Server is running and is waiting for Player 2...", ConsoleColor.Green); // using (Socket accepted = server.Accept()) Config.tcp_server = server.Accept(); SendSocketMessage(Config.tcp_server, "connection_success"); while (true) { string connectionbuffer = ReceiveSocketMessage(Config.tcp_server, 18); if (connectionbuffer == "connection_success") { mf.ConsoleWrite("Player 2 Connected Successfully!", ConsoleColor.Green); new MainFunctions().ConsoleWrite( "[GDMP Notice] Player2's Color will be the opposite of Player1's color!", ConsoleColor.Yellow); break; } } keylistener.Start(); new MainFunctions().ConsoleWrite("[GDMP Log] -> Key listener thread started!", ConsoleColor.Yellow); tcpthread.Start(); new MainFunctions().ConsoleWrite("[GDMP Log] -> TCP Handler thread started!", ConsoleColor.Yellow); } break; } case (int)Config.Types.TCP_CLIENT: { new MainFunctions().ConsoleWrite($"[GDMP Log] -> Detected Type: 2", ConsoleColor.White); new MainFunctions().ConsoleWrite( $"[GDMP] Connecting to IP: {cfg.IPEnd.Address} with Port: {cfg.IPEnd.Port}...", ConsoleColor.White); try { Config.tcp_client.Connect(cfg.IPEnd); } catch (Exception) { new MainFunctions().ConsoleWrite("[GDMP Log] -> Connection Refused", ConsoleColor.Red); } SendSocketMessage(Config.tcp_client, "connection_success"); while (Config.tcp_client.Connected) { try { string connectionbuffer = ReceiveSocketMessage(Config.tcp_client, 18); if (connectionbuffer == "connection_success") { mf.ConsoleWrite("Connected to Server!", ConsoleColor.Green); new MainFunctions().ConsoleWrite( "[GDMP Notice] Your Color will be the opposite of Player1's color!", ConsoleColor.Yellow); break; } } catch (SocketException ex) { } } keylistener.Start(); new MainFunctions().ConsoleWrite("[GDMP Log] -> Key listener thread started!", ConsoleColor.Yellow); tcpthread.Start(); new MainFunctions().ConsoleWrite("[GDMP Log] -> TCP Handler thread started!", ConsoleColor.Yellow); break; } } }