public GameUI() { InitializeComponent(); this.listOfClients = new List<int>(); this.transferProtocol = new TransferProtocol(); int broadcastResult = this.transferProtocol.SendBroadcast(); if (broadcastResult == TransferProtocol.ClientState) { this.isClient = true; this.transferProtocol.StartAsClient(); Thread client = new Thread(new ParameterizedThreadStart(startClient)); client.Start(isClient); } else if (broadcastResult == TransferProtocol.ServerState) { this.isClient = false; this.transferProtocol.StartAsServer(); Thread trd = new Thread(new ParameterizedThreadStart(broadcast)); //make the loopStatusForBroadcast True so it will loop infinite loopStatusForBroadcast = true; trd.Start((object)loopStatusForBroadcast); //thread for accepting threads Thread acceptClientThread = new Thread(acceptClient); acceptClientThread.Start(); } else { this.isClient = null; } xPos = 2; yPos = 2; widthBorder = 1; graphicsObj = displayPnl.CreateGraphics(); myPen = new Pen(Color.Black, widthBorder); rectangle = new Rectangle(xPos, yPos, 50, 50); solidBrush = new SolidBrush(Color.PaleVioletRed); rand = new Random(); }
static void Main(string[] args) { TransferProtocol prot = new TransferProtocol(); int broadcastResult = prot.SendBroadcast(); if (broadcastResult == 2) { Console.WriteLine("Starting Client"); Thread.Sleep(1000); prot.StartAsClient(); Thread.Sleep(1000); prot.ClientSendTCP("hello"); Thread.Sleep(1000); Console.WriteLine(prot.ClientReadTCP()); prot.ClientExit("!exit"); Console.WriteLine("Finished Client"); } else if (broadcastResult == 3) { Console.WriteLine("Starting Server"); string content = "cont"; prot.ServerListenToBroadcast(ref content); Thread.Sleep(1000); prot.StartAsServer(); int id = prot.ServerAcceptOneClient(); Thread.Sleep(1000); Console.WriteLine(prot.ServerReadTCP(id)); Thread.Sleep(1000); prot.ServerSendTCP("message from server", id); Thread.Sleep(1000); prot.ServerReadTCP(id); prot.ServerExit(); Console.WriteLine("Finished Server"); } else { Console.WriteLine("error"); } Console.ReadKey(); }
public GameUI() { InitializeComponent(); this.listOfClients = new List<int>(); string additionalContent = ""; try { this.transferProtocol = new TransferProtocol(); // get the state of the current application int broadcastResult = this.transferProtocol.SendBroadcast(ref additionalContent); // start setting up as a client if (broadcastResult == TransferProtocol.ClientState) { this.isClient = true; this.Text = "Client"; this.transferProtocol.StartAsClient(); Thread client = new Thread(new ThreadStart(startClient)); client.Start(); } // start setting up as a server else if (broadcastResult == TransferProtocol.ServerState) { this.isClient = false; this.Text = "Server"; this.transferProtocol.StartAsServer(); this.StartServerThreads(); } else { this.isClient = null; } } catch (Exception ex) { MessageBox.Show("An error in the start up of the protocol, please restart the application"); } // start a thread for sending random messages Thread randomThread = new Thread(new ThreadStart(MakeRandomMoveThread)); randomThread.Start(); // update the position of the sqyare if (additionalContent == "") { xPos = 300; yPos = 150; this.UpdateAdditionalContent(); } else { string[] strs = additionalContent.Split(';'); xPos = Int32.Parse(strs[0]); yPos = Int32.Parse(strs[1]); } widthBorder = 1; graphicsObj = displayPnl.CreateGraphics(); myPen = new Pen(Color.Black, widthBorder); rectangle = new Rectangle(xPos, yPos, 50, 50); solidBrush = new SolidBrush(Color.PaleVioletRed); rand = new Random(); }