private void btnSend_Click_1(object sender, EventArgs e) { if (txtMessage.Text.Contains("@")) { try { Data msgToSend = new Data(); msgToSend.strName = "TEST_CLIENT"; msgToSend.strMessage = txtMessage.Text; msgToSend.cmdCommand = Command.Command; logging.Logging(string.Format("To Server : {0}\r\nPacket Type : Command\r\nServer : {1}", msgToSend.strMessage, msgToSend.strName)); byte[] byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); txtMessage.Text = null; } catch { } } else if (txtMessage.Text.Contains("/")) { try { string[] temp = new string[65535]; Data msgToSend = new Data(); msgToSend.strName = "TEST_CLIENT"; if (txtMessage.Text.Contains(" ")) { txtMessage.Text.Replace('/', ' '); temp = txtMessage.Text.Split(' '); } msgToSend.strToUser = temp[0]; for (int i =1; i<temp.Length; i++) { msgToSend.strMessage += temp[i] + " "; } if (msgToSend.strMessage.Length <= 0) return; msgToSend.cmdCommand = Command.PrivateMessage; byte[] byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); txtMessage.Text = null; } catch { } } else { try { //Fill the info for the message to be send Data msgToSend = new Data(); msgToSend.strName = "TEST_CLIENT"; msgToSend.strMessage = txtMessage.Text; msgToSend.cmdCommand = Command.Message; logging.Logging(string.Format("To Server : {0}\r\nPacket Type : Message\r\nServer : {1}", msgToSend.strMessage, msgToSend.strName)); 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 { } } }
private void Test_Client_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("Are you sure you want to leave the chat room?", "Test Client: " + strServerName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No) { e.Cancel = true; return; } try { //Send a message to logout of the server Data msgToSend = new Data (); msgToSend.cmdCommand = Command.Logout; msgToSend.strName = strServerName; msgToSend.strMessage = null; byte[] b = msgToSend.ToByte (); clientSocket.Send(b, 0, b.Length, SocketFlags.None); clientSocket.Close(); } catch (ObjectDisposedException) { } catch { } }
private void timer1_Tick(object sender, EventArgs e) { try { Data msgToSend = new Data(); msgToSend.cmdCommand = Command.Pulse; msgToSend.strName = "TEST_CLIENT"; msgToSend.strMessage = "TESTING"; byte[] message; message = msgToSend.ToByte(); clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), null); } catch { } }
private void OnReceive(IAsyncResult ar) { try { clientSocket.EndReceive(ar); Data msgReceived = new Data(byteData); //Accordingly process the message received switch (msgReceived.cmdCommand) { case Command.Login: lstChatters.Items.Add(msgReceived.strName); break; case Command.Logout: lstChatters.Items.Remove(msgReceived.strName); break; case Command.Message: break; case Command.Command: if (msgReceived.strMessage.Contains("@ERROR")) { string[] words = msgReceived.strMessage.Split('\t'); logging.Logging(string.Format("word 1 = {0} word 2 = {1}", words[0], words[1])); error.ERROR(Convert.ToInt32(words[1])); } else if (msgReceived.strMessage.Contains("@load_Char")) { logging.Logging(string.Format("From Server {0}", msgReceived.strMessage)); string[] words = msgReceived.strMessage.Split('=', ' '); int logThismany = words.Length; logging.Logging(string.Format("DEBUG : parma count : {0}", words.Length)); if (logThismany < 22) break; for(int i = 0; i < logThismany; i++) { #region DULL switch (i) { case 2: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 4: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 6: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 8: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 10: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 12: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 14: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 16: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 18: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 20: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 22: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; case 24: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 26: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 28: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 30: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 32: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 34: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 36: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 38: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 40: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 42: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 44: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; case 46: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 48: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 50: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 52: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 54: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 56: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 58: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 60: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 62: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 64: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 66: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; case 68: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 70: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 72: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 74: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 76: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 78: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 80: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 82: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 84: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 86: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 88: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; default: break; } #endregion } } else logging.Logging(string.Format("From Server {0}", msgReceived.strMessage)); break; case Command.List: lstChatters.Items.AddRange(msgReceived.strMessage.Split('*')); lstChatters.Items.RemoveAt(lstChatters.Items.Count - 1); txtChatBox.Text += strServerName + " connected\r\n"; break; } if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List && msgReceived.cmdCommand != Command.Command) txtChatBox.Text += msgReceived.strMessage + "\r\n"; byteData = new byte[1024]; clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } catch (ObjectDisposedException) { } catch { } }
private void Form1_Load(object sender, EventArgs e) { this.Text = "Test Client: " + strServerName; //The user has logged into the system so we now request the server to send //the names of all users who are in the chat room Data msgToSend = new Data (); msgToSend.cmdCommand = Command.List; msgToSend.strName = strServerName; msgToSend.strMessage = null; 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); }
private void OnReceive(IAsyncResult ar) { try { clientSocket.EndReceive(ar); Data msgReceived = new Data(byteData); //Accordingly process the message received switch (msgReceived.cmdCommand) { case Command.Login: lstChatters.Items.Add(msgReceived.strName); break; case Command.Logout: lstChatters.Items.Remove(msgReceived.strName); break; case Command.Message: break; case Command.Command: if (msgReceived.strMessage.Contains("@ERROR")) { string[] words = msgReceived.strMessage.Split('\t'); logging.Logging(string.Format("word 1 = {0} word 2 = {1}", words[0], words[1])); error.ERROR(Convert.ToInt32(words[1])); } else if (msgReceived.strMessage.Contains("@load_Char")) { logging.Logging(string.Format("From Server {0}", msgReceived.strMessage)); string[] words = msgReceived.strMessage.Split('=', ' '); int logThismany = words.Length; logging.Logging(string.Format("DEBUG : parma count : {0}", words.Length)); if (logThismany < 22) { break; } for (int i = 0; i < logThismany; i++) { #region DULL switch (i) { case 2: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 4: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 6: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 8: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 10: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 12: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 14: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 16: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 18: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 20: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 22: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; case 24: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 26: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 28: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 30: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 32: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 34: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 36: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 38: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 40: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 42: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 44: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; case 46: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 48: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 50: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 52: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 54: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 56: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 58: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 60: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 62: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 64: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 66: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; case 68: logging.Logging(string.Format("{0}{1}", char_Info[0], words[i])); break; case 70: logging.Logging(string.Format("{0}{1}", char_Info[1], words[i])); break; case 72: logging.Logging(string.Format("{0}{1}", char_Info[2], words[i])); break; case 74: logging.Logging(string.Format("{0}{1}", char_Info[3], words[i])); break; case 76: logging.Logging(string.Format("{0}{1}", char_Info[4], words[i])); break; case 78: logging.Logging(string.Format("{0}{1}", char_Info[5], words[i])); break; case 80: logging.Logging(string.Format("{0}{1}", char_Info[6], words[i])); break; case 82: logging.Logging(string.Format("{0}{1}", char_Info[7], words[i])); break; case 84: logging.Logging(string.Format("{0}{1}", char_Info[8], words[i])); break; case 86: logging.Logging(string.Format("{0}{1}", char_Info[9], words[i])); break; case 88: logging.Logging(string.Format("{0}{1}", char_Info[10], words[i])); break; default: break; } #endregion } } else { logging.Logging(string.Format("From Server {0}", msgReceived.strMessage)); } break; case Command.List: lstChatters.Items.AddRange(msgReceived.strMessage.Split('*')); lstChatters.Items.RemoveAt(lstChatters.Items.Count - 1); txtChatBox.Text += strServerName + " connected\r\n"; break; } if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List && msgReceived.cmdCommand != Command.Command) { txtChatBox.Text += msgReceived.strMessage + "\r\n"; } byteData = new byte[1024]; clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } catch (ObjectDisposedException) { } catch { } }
private void btnSend_Click_1(object sender, EventArgs e) { if (txtMessage.Text.Contains("@")) { try { Data msgToSend = new Data(); msgToSend.strName = "TEST_CLIENT"; msgToSend.strMessage = txtMessage.Text; msgToSend.cmdCommand = Command.Command; logging.Logging(string.Format("To Server : {0}\r\nPacket Type : Command\r\nServer : {1}", msgToSend.strMessage, msgToSend.strName)); byte[] byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); txtMessage.Text = null; } catch { } } else if (txtMessage.Text.Contains("/")) { try { string[] temp = new string[65535]; Data msgToSend = new Data(); msgToSend.strName = "TEST_CLIENT"; if (txtMessage.Text.Contains(" ")) { txtMessage.Text.Replace('/', ' '); temp = txtMessage.Text.Split(' '); } msgToSend.strToUser = temp[0]; for (int i = 1; i < temp.Length; i++) { msgToSend.strMessage += temp[i] + " "; } if (msgToSend.strMessage.Length <= 0) { return; } msgToSend.cmdCommand = Command.PrivateMessage; byte[] byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); txtMessage.Text = null; } catch { } } else { try { //Fill the info for the message to be send Data msgToSend = new Data(); msgToSend.strName = "TEST_CLIENT"; msgToSend.strMessage = txtMessage.Text; msgToSend.cmdCommand = Command.Message; logging.Logging(string.Format("To Server : {0}\r\nPacket Type : Message\r\nServer : {1}", msgToSend.strMessage, msgToSend.strName)); 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 { } } }
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 = "TEST_CLIENT"; 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); } catch { } }