Easy multiplayer chat rooms implemented in C #
Only a group chat function
Server
the System the using; the using the System.Collections.Generic; the using the System.ComponentModel; the using the System.Data; the using the System.Drawing; the using the System.Linq; the using the System.Net; the using the System.Net.Sockets; the using the System.Text; the using the System. threading; a using System.Threading.Tasks; a using System.Windows.Forms; namespace FinalChatRoomClient { public partial class client: Form { // client is responsible for receiving data sent by the server message thread the thread threadClient = null; // create customer end of the socket, the connection server responsible for the socket SocketClient = null; public Client () { the InitializeComponent (); // Close examination of the text box cross-thread operation TextBox.CheckForIllegalCrossThreadCalls = to false; } Private void start_Click (SENDER Object, EventArgs E) { // get the IP address of the object in the text box IPAddress address = IPAddress.Parse (txtIp.Text. the TRIM ()); // create a network node object that contains IP and port IPEndPoint endPoint = new new IPEndPoint (address, int.Parse (txtPort.Text.Trim ())); // create a client socket, connect to the server responsible for the Socket = new new SocketClient (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); the try { // the client connects to the server socketClient.Connect (endPoint); ShowMsg ( "client connection server successfully"); } the catch (a SocketException EX) { ShowMsg ( "client connection server exception occurs:" + ex.Message); } the catch (Exception EX) { ShowMsg ( "client connection server exception occurs:" + ex. the Message); } threadClient the Thread new new = (ReceiveMsg); threadClient.IsBackground = to true; threadClient.Start (); } Private void btnSend_Click (SENDER Object, EventArgs E) { String strMsg txtMsg.Text.Trim = (); // a easily converted into binary array string transmitted over the network byte [] arrMsg = Encoding.UTF8.GetBytes (strMsg ); byte [] = arrMsgSend new new byte [arrMsg.Length +. 1]; arrMsgSend [0] = 0; // set flag bit 0 represents the transmitted text Buffer.BlockCopy (arrMsg, 0, arrMsgSend,. 1, arrMsg.Length); the try { socketClient.Send (arrMsgSend); // Clear message sent message text box this.txtMsg.Text = ""; } the catch (a SocketException EX ) { ShowMsg ( "exception occurs when a client sends a message:" + ex.Message); } the catch (exception EX) { ShowMsg ( "exception occurs when a client sends a message:" + ex.Message); } } Private void ShowMsg (String MSG) { txtRecord.AppendText (MSG + "\ R & lt \ n-"); } Private void ReceiveMsg () { the while (to true) { // definition of a message received by the byte array buffer (2M size) byte [] = arrMsgRev new new byte [ 1024 * 1024 * 2]; // the received data into arrMsgRev, and returns a true length of the data received int length = -1; the try { length = socketClient.Receive (arrMsgRev); } the catch (a SocketException EX) { ShowMsg ( "exception occurs when a client receives a message:" + ex.Message); BREAK; } the catch (Exception EX) { MessageBox.Show ( "exception occurs when a client receives a message:" + ex.Message); BREAK; } // in this case, all elements of the array (each byte) are transformed into a string, but the real receiving sent to the server only a few characters String strMsgReceive = Encoding.UTF8.GetString (arrMsgRev, 0, length); Console.WriteLine (strMsgReceive); ShowMsg (strMsgReceive); } } } }
Client
the System the using; the using the System.Collections.Generic; the using the System.ComponentModel; the using the System.Data; the using the System.Drawing; the using the System.Linq; the using the System.Net; the using the System.Net.Sockets; the using the System.Text; the using the System. threading; a using System.Threading.Tasks; a using System.Windows.Forms; namespace FinalChatRoomClient { public partial class client: Form { // client is responsible for receiving data sent by the server message thread the thread threadClient = null; // create customer end of the socket, the connection server responsible for the socket SocketClient = null; public Client () { the InitializeComponent (); // check the text box closed cross-thread operation = To false TextBox.CheckForIllegalCrossThreadCalls; } Private void start_Click (SENDER Object, EventArgs E) { // get the IP address of the object in the text box the IPAddress = IPAddress.Parse address (txtIp.Text.Trim ()); // Create IP and comprising network node objects ports IPEndPoint endPoint = new new IPEndPoint (address, int.Parse (txtPort.Text.Trim ())); // create a client socket, connect to the server responsible for socketClient = new socket (AddressFamily.InterNetwork, SocketType . Stream, ProtocolType.Tcp); the try { // the client connects to the server } the catch (a SocketException EX) socketClient.Connect (endPoint); ShowMsg ( "client connection server successfully"); byte [] arrMsg = Encoding.UTF8.GetBytes (strMsg ); { ShowMsg ( "client connection server exception:" + ex.Message); } the catch (Exception EX) { ShowMsg ( "client connection server exception occurs:" + ex.Message); } threadClient the Thread new new = (ReceiveMsg) ; threadClient.IsBackground = to true; threadClient.Start (); } Private void btnSend_Click (SENDER Object, EventArgs E) { string strMsg txtMsg.Text.Trim = (); // string easily converted into a binary array of network transmission arrMsgSend [0] = 0; // set flag bit 0 represents the transmitted text byte [] = arrMsgSend new new byte [arrMsg.Length +. 1]; Buffer.BlockCopy (arrMsg, 0, arrMsgSend,. 1, arrMsg.Length); the try { socketClient.Send (arrMsgSend); // send a message text box empty message ; "this.txtMsg.Text =" } the catch (a SocketException EX) { ; ": (+ ex.Message ShowMsg" exception occurs when a client sends a message) } the catch (exception EX) { while ShowMsg (client sends a message " exception: "+ ex.Message); } } Private void ShowMsg (String MSG) { txtRecord.AppendText (MSG + "\ R & lt \ n-"); } void ReceiveMsg Private () { the while (to true) { // definition of a message received by the byte array buffer (2M size) byte [] = arrMsgRev new new byte [1024 * 1024 * 2]; // data received deposit arrMsgRev, and returns a true length of the data received int length = -1; the try { length = socketClient.Receive (arrMsgRev); } the catch (a SocketException EX) { ShowMsg ( "exception occurs when a client receives a message:" + ex .Message); BREAK; } the catch (Exception EX) { MessageBox.Show ( "exception occurs when a client receives a message:" + ex.Message); BREAK; } // in this case, all elements of the array (each byte) are transformed into a string, the only truly received the server sent a few characters String strMsgReceive = Encoding.UTF8.GetString (arrMsgRev, 0, length); Console.WriteLine (strMsgReceive); ShowMsg (strMsgReceive); } } } }