private void DoAcceptSocketCallback(IAsyncResult ar) { try { // Get the listener that handles the client request TcpListener listener = (TcpListener)ar.AsyncState; //End operation and display the received data on the screen TcpClient clientSocket = listener.EndAcceptTcpClient(ar); //Add client to client list clientSockets.Add(clientSocket.Client.RemoteEndPoint.ToString(), clientSocket); //ThreadHelper.lsbAddItem(this, lsbJoined, "Client connected" + clientSocket.Client.RemoteEndPoint.ToString()); //Send a confirmation message to client // Send("CONFIRM", clientSocket); //Acccept another TCPClient connection listener.BeginAcceptTcpClient(DoAcceptSocketCallback, listener); //Begin recieving data from the client socket Receive(clientSocket); } catch (Exception ex) { ThreadHelper.lsbAddItem(this, lsbJoined, ex.ToString()); } }
private void Receive(TcpClient clientSocket) { try { clientSocket.Client.BeginReceive(buffer, 0, buffer_size, SocketFlags.None, new AsyncCallback(BeginReceiveCallback), clientSocket); } catch (Exception ex) { ThreadHelper.lsbAddItem(this, lsbJoined, ex.ToString()); } }
private void BeginReceiveCallback(IAsyncResult ar) { // get the client socket TcpClient client = (TcpClient)ar.AsyncState; int bytesRead = client.Client.EndReceive(ar); string message = System.Text.Encoding.ASCII.GetString(_buffer, 0, bytesRead); Receive(); if (message.Contains("GAMEQB")) { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "QB"; // GameRules GR = new GameRules(); // GR.ShowDialog(); // testing GR = new testing(); GameParticipant GR = new GameParticipant(); GR.ShowDialog(); } else if (message.Contains("GAMEPZ")) { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "PZ"; // GameRules GR = new GameRules(); // GR.Show(); GameParticipant GR = new GameParticipant(); GR.ShowDialog(); } //client.Client.Shutdown(SocketShutdown.Both); //client.Client.Close(); else { ThreadHelper.lsbAddItem(this, lsbWait, message); Receive(); } }
private void Send(string text, TcpClient client) { try { //Set a NetworkStream for sending data NetworkStream stream = client.GetStream(); //Store the message into the buffer byte[] buffer = System.Text.Encoding.ASCII.GetBytes(text); //Begin writing into the stream bufer for sending stream.BeginWrite(buffer, 0, buffer.Length, BeginSendCallback, stream); } catch (Exception ex) { ThreadHelper.lsbAddItem(this, lsbJoined, ex.ToString()); } }
public void updateScore(string Score) { string up = Score.ToString(); // string name = up.Substring(8,up.Length-12); string name; int nameindex = up.LastIndexOf(','); name = up.Substring(8, nameindex - 8); int index = up.LastIndexOf('('); string points = up.Substring(index + 1); points = points.Substring(0, points.Length - 1); ThreadHelper.lsbAddItem(this, listBox1, up + "(" + DateTime.Now.TimeOfDay + ")"); this.Invoke((MethodInvoker)(() => updatescore(name, points))); }
private void Send(string message) { try { //Translate the message into its byte form byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message); //Get a client stream for reading and writing NetworkStream stream = _client.GetStream(); //Send the message to the connected server //stream.Write(buffer, 0, buffer.length); stream.BeginWrite(buffer, 0, buffer.Length, BeginWriteCallback, stream); } catch (Exception ex) { ThreadHelper.lsbAddItem(this, lsbWait, ex.ToString()); } }
public void StartConnect() { try { if (_client.Connected == false) { _client = new TcpClient(); } _client.NoDelay = true; //Begin connecting to server _client.BeginConnect(IPAddress.Parse(_IPAddress), _PORT, BeginConnectCallBack, _client); ThreadHelper.lsbAddItem(this, lsbWait, " You are now connected! Please Wait"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void BeginReceiveCallback(IAsyncResult ar) { try { //Get the client socket TcpClient clientSocket = (TcpClient)ar.AsyncState; //Get the received bytes from the client int received = clientSocket.Client.EndReceive(ar); // Get the string value of the received buffer string message = System.Text.Encoding.ASCII.GetString(buffer, 0, received); //Check if a disconenct flag was received if (message.Contains("SCORE")) { //ThreadHelper.lsbAddItem(this, lsbJoined, message); GameFacilitator GF = (GameFacilitator)Application.OpenForms["GameFacilitator"]; GF.updateScore(message.ToString()); // Send("You ("+message+ ") are now connected! Please Wait",clientSocket); //Begin receiving data from the client socket Receive(clientSocket); } else if (message.Contains("IMAGE")) { GameFacilitator GF = (GameFacilitator)Application.OpenForms["GameFacilitator"]; GF.PUZZLEScore(message.ToString()); Receive(clientSocket); } else if (message.Contains("DISCONNECT")) { ThreadHelper.lsbAddItem(this, lsbJoined, clientSocket.Client.RemoteEndPoint.ToString() + " has disconnected."); //send a disconnect flag to the client to complete disconnection Send("DISCONNECT", clientSocket); //remove the client socket from the client list clientSockets.Remove(clientSocket.Client.RemoteEndPoint.ToString()); } else { ThreadHelper.lsbAddItem(this, lsbJoined, message); // Send("You ("+message+ ") are now connected! Please Wait",clientSocket); //Begin receiving data from the client socket Receive(clientSocket); } } catch (Exception ex) { // ThreadHelper.lsbAddItem(this, lsbJoined, ex.ToString()); ThreadHelper.lsbAddItem(this, lsbJoined, "Error Detected!,Please Close this Lobby and Reconnect."); } }
private void BeginReceiveCallback(IAsyncResult ar) { try { // get the client socket TcpClient client = (TcpClient)ar.AsyncState; ///error forcibly close int bytesRead = client.Client.EndReceive(ar); string message = System.Text.Encoding.ASCII.GetString(_buffer, 0, bytesRead); Receive(); if (message.Contains("CONFIRM")) { ThreadHelper.lsbAddItem(this, lsbWait, " You are now connected! Please Wait"); } else if (message.Contains("GAMEIPQB")) { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "QB"; // GameRules GR = new GameRules(); // GR.ShowDialog(); // testing GR = new testing(); GameParticipant GR = new GameParticipant(); GR.ShowDialog(); } else if (message.Contains("GAMEIPPZ")) { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "PZ"; // GameRules GR = new GameRules(); // GR.Show(); GameParticipant GR = new GameParticipant(); GR.ShowDialog(); } else if (message.Contains("GAMEGPQB")) //group { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "QB"; // GameRules GR = new GameRules(); // GR.Show(); GameGroup GR = new GameGroup(); GR.ShowDialog(); } else if (message.Contains("GAMEGPPZ"))//group { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "PZ"; // GameRules GR = new GameRules(); // GR.Show(); GameGroup GR = new GameGroup(); GR.ShowDialog(); } else if (message.Contains("GAMEFPQB")) { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "QB"; // GameRules GR = new GameRules(); // GR.Show(); GameFree GF = new GameFree(); GF.ShowDialog(); } else if (message.Contains("GAMEFPPZ")) { ThreadHelper.Hide(this); //this.Hide();//kaipuhn muna ithread GameType = "PZ"; // GameRules GR = new GameRules(); // GR.Show(); GameFree GF = new GameFree(); GF.ShowDialog(); } //client.Client.Shutdown(SocketShutdown.Both); //client.Client.Close(); else { ThreadHelper.lsbAddItem(this, lsbWait, message); Receive(); } } catch (Exception ex) { Dialogue.Show(" ' " + ex.Message.ToString() + "' ", "", "Ok", "Cancel"); } }