//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////SERVER/////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Server Functions implementation void InitializeServer() { try { //write code to initialize currentSocket to be server socket currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, groupPort); currentSocket.Bind(serverEndPoint); Console.WriteLine("Initialized server"); //add the server to clientList //set the servers rank = 0; numberOfPlayers = 0; Client server = new Client(); server.IP = GameStart.GetLocalIPAddress(); server.Rank = numberOfPlayers; server.playerSocket = currentSocket; clients.Add(server); listBox1.Items.Add(server.Rank + ". " + server.IP); numberOfPlayers++; Console.WriteLine("Added the server to player list"); } catch (Exception ex) { Console.WriteLine("Failed to initialize server\n" + ex.Message + '\n' + ex.Data + '\n' + ex.StackTrace); } }
void SendTheWinnerIsMeToServer() { //use the currentPlayer socket to send to server the winner message //message should look like this: //IP# currentPlayer.Send(Encoding.ASCII.GetBytes(GameStart.GetLocalIPAddress() + "#")); Console.WriteLine("Sent the winner is me to server"); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////CLIENT/////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void SendLocationToServer() { //use the currentPlayer socket to send to server "PlayersLocation[myIndex]" //message should look like this: //IP#PlayersLocation[myIndex]# currentPlayer.Send(Encoding.ASCII.GetBytes(GameStart.GetLocalIPAddress() + "#" + PlayersLocation[myIndex].X + "," + PlayersLocation[myIndex].Y + "#")); Console.WriteLine("Sent new location to server"); }
void BroadCastIP() { //write code to broadcast your IP IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, groupPort); String serverIP = GameStart.GetLocalIPAddress(); byte[] serverIPBytes = Encoding.ASCII.GetBytes(serverIP); udp.Send(serverIPBytes, serverIPBytes.Length, endPoint); Console.WriteLine("Broadcasted IP\nServer IP: " + serverIP); broadcastPlayerList(); //Hint: this function is called repeatedly using timer every 5seconds }
public GamePlayingScreen(char[,] board, Dictionary <Point, int> snakes, Dictionary <Point, int> ladders, List <Client> clients, int numberOfPlayers, Socket me, bool Server, UdpClient udp) { activeForm = this; InitializeComponent(); Clients = clients; gameBoard = board; Snakes = snakes; Ladders = ladders; currentPlayer = me; this.numberOfPlayers = numberOfPlayers; PlayersLocation = new List <Point>(); for (int i = 0; i < numberOfPlayers; i++) { PlayersLocation.Add(new Point(0, 0)); } GeneratePlayerList(numberOfPlayers); isServer = Server; DoubleBuffered = true; SetStyle(ControlStyles.OptimizedDoubleBuffer, true); DrawBoard(); if (isServer) { this.serverUdpClient = udp; btnRollTheDice.Enabled = true; myIndex = 0; for (int i = 1; i < clients.Count; i++) { Thread t = new Thread(new ParameterizedThreadStart(RecieveFromClients)); t.Start(clients[i]); } for (int i = 1; i < clients.Count; i++) { clients[i].playerSocket.Send(Encoding.ASCII.GetBytes(clients[i].Rank.ToString())); } } else { Console.WriteLine("Sending join game notification to server"); currentPlayer.Send(Encoding.ASCII.GetBytes("Client with IP " + GameStart.GetLocalIPAddress() + " joined the game")); Console.WriteLine("Sent join game notification to server"); byte[] myIndexBytes = new byte[20]; currentPlayer.Receive(myIndexBytes); Console.WriteLine("My index is " + Encoding.ASCII.GetString(myIndexBytes)); myIndex = Int32.Parse(Encoding.ASCII.GetString(myIndexBytes)); } }
void RecieveFromServer() { //use the currentPlayer socket to recieve from the server //parse the recieved message while (recieving) { byte[] arr = new byte[10000]; int size = currentPlayer.Receive(arr); if (arr[0] == (byte)'l' && arr[1] == (byte)'o') { byte[] newByte = new byte[size]; for (int i = 4; i < size; i++) { newByte[i - 4] = arr[i]; } PlayersLocation = (List <Point>)GameStart.ByteArrayToObject(newByte); Console.WriteLine("playerLocationRecieved "); } else { var message = Encoding.ASCII.GetString(arr); Console.WriteLine(message); ParseMessage(message); } } //if turn message check if the IP matched with my IP //then check if currentPlayer boolean = true //enable "RollTheDice" button and play //else keep it disabled //if location message then update the location of player n //update client n location //if winning message //go to WinningForm with the playerNumber }
private void btnStartGame_Click(object sender, EventArgs e) { //foreach(Client c in clients) //{ // byte[] numPlayersBytes = new byte[20]; // numPlayersBytes = Encoding.ASCII.GetBytes(numberOfPlayers.ToString()); // c.playerSocket.Send(numPlayersBytes); //} tmrBroadCastIP.Stop(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, groupPort); String serverIP = GameStart.GetLocalIPAddress(); byte[] serverIPBytes = Encoding.ASCII.GetBytes("start"); udp.Send(serverIPBytes, serverIPBytes.Length, endPoint); Console.WriteLine("Broadcasted start game"); GenerateSnakesAndLadders(); char[,] board = GenerateBoard(snakes, ladders); gpc = new GamePlayingScreen(board, snakes, ladders, clients, clients.Count, currentSocket, true, udp); this.Visible = false; gpc.Show(); }