private void btnStartAsServer_Click(object sender, EventArgs e) { GameSettingScreen gsc = new GameSettingScreen(true, null); gsc.Show(); this.Visible = false; t.Abort(); }
private void btnJoinAsClient_Click(object sender, EventArgs e) { // Clicked join as client IPAddress ip = IPAddress.Parse(serverIP); // Show game setting screen GameSettingScreen gsc = new GameSettingScreen(false, ip, null); gsc.Show(); this.Visible = false; }
private void btnJoinAsClient_Click(object sender, EventArgs e) { //recieve the servers IP using UDP and place it in the variable "ip" GameSettingScreen gsc = new GameSettingScreen(false, ip); gsc.Show(); this.Visible = false; }
private void btnStartAsServer_Click(object sender, EventArgs e) { // Clicked start as server IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, groupPort); serverIP = GetLocalIPAddress(); byte[] serverIPBytes = Encoding.ASCII.GetBytes(serverIP); // Broadcasting server IP udp.Send(serverIPBytes, serverIPBytes.Length, endPoint); Console.WriteLine("Broadcasted IP successfully\nServer IP: " + serverIP); // Showing game setting screen GameSettingScreen gsc = new GameSettingScreen(true, null, udp); gsc.Show(); this.Visible = false; }
void BroadCastLocation() { //here send the mssage to all clients, containing the location of PlayersLocation[playerNumber] and attach its IP and playerNumber List <byte> bytesToSend = new List <byte>(); bytesToSend.Add((byte)'l'); bytesToSend.Add((byte)'o'); bytesToSend.Add((byte)'c'); bytesToSend.Add((byte)'#'); var locArrAsBytes = GameSettingScreen.ObjectToByteArray(PlayersLocation); for (int j = 0; j < locArrAsBytes.Length; j++) { bytesToSend.Add(locArrAsBytes[j]); } for (int i = 1; i < Clients.Count; i++) { Clients[i].player.Send(bytesToSend.ToArray()); } }
//Form Constructor public GameSettingScreen(bool isServer, IPAddress IP, UdpClient udp) { activeForm = this; InitializeComponent(); serverIP = IP; GameSettingScreen.isServer = isServer; if (!isServer) { //joined as client (initialize the TCP client socket, connect to Servers IP and wait for the server to start the game timerStartGame.Start(); btnStartGame.Enabled = false; JoinServer(IP); Thread threadReceiveDataFromServer = new Thread(RecieveDataFromServer); threadReceiveDataFromServer.Start(); } else { //joined as server (start the TCP server socket, start UDP server socket to broadcasting the servers IP and finally accept client sockets using the TCP socket this.udp = udp; InitializeServer(); tmrBroadCastIP.Start(); AcceptPlayers(); } }