private void OnConnect(IAsyncResult ar) { try { clientSocket.EndConnect(ar); //We are connected so we login into the server Data msgToSend = new Data(); msgToSend.cmdCommand = Command.Login; msgToSend.strName = strName; msgToSend.strMessage = null; byte[] b = msgToSend.ToByte(); //Send the message to the server clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null); byteData = new byte[1024]; //Start listening to the data asynchronously clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), byteData); } catch (Exception ex) { Console.WriteLine("Loi " + ex.ToString() + " Connect phia client"); } }
public void Request(string strMess) { try { Data msgToSend = new Data(); msgToSend.cmdCommand = Command.Message; msgToSend.strName = strName; msgToSend.strMessage = strMess; byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); //byteData = new byte[1024]; //Start listening to the data asynchronously //clientSocket.BeginReceive(byteData, // 0, // byteData.Length, // SocketFlags.None, // new AsyncCallback(OnReceive), // null); } catch (Exception) { } }
public void LogOut() { try { //Send a message to logout of the server Data msgToSend = new Data(); msgToSend.cmdCommand = Command.Logout; msgToSend.strName = strName; msgToSend.strMessage = null; byte[] b = msgToSend.ToByte(); clientSocket.Send(b, 0, b.Length, SocketFlags.None); clientSocket.Close(); } catch (ObjectDisposedException) { } catch (Exception ex) { Console.WriteLine("Loi " + ex.ToString() + " Logout phia client"); } }
public void SendMess(string strMessage, Command c) { try { //Fill the info for the message to be send Data msgToSend = new Data(); msgToSend.strName = strName; msgToSend.strMessage = strMessage; msgToSend.cmdCommand = c; byte[] byteData = msgToSend.ToByte(); //Send it to the server clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); //txtMessage.Text = null; } catch (Exception) { MessageBox.Show("Unable to send message to the server.", "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }