This article shares the specific code of the prototype of C# simple chat room for your reference. The specific content is as follows
The black window simulation program of the console used by the program involves network programming for the first time. It is easy to write and read later. The code is very simple.
First, the server-side code:
public class ServerControl { private Socket serverSocket; public ServerControl() { serverSocket = new Socket(,,); } public void Start() { (new IPEndPoint(, 12312)); //Set to receive any ip (10); //Maximum number of suspended ("Server startup successfully"); } }
Main program:
static void Main(string[] args) { ServerControl server = new ServerControl(); (); (); }
Here is the client code:
public class ClientControl { Socket clientSocket; public ClientControl() { clientSocket = new Socket(, , ); } public void Connect(string ip, int port) { (ip, port); ("Connecting to the server successfully"); } }
Client calling code:
static void Main(string[] args) { ClientControl client = new ClientControl(); ("127.0.0.1",12312); (); }
At this time, the client can connect to the server smoothly. The next step is to show who is connected to the server on the server. It’s very simple. Just add a method to the server to detect:
private void Accept() { //Receive client method, the current thread will be suspended Socket client = (); IPEndPoint point = as IPEndPoint; ( + "[" + + "]Connected successfully"); Accept(); //Tail recursion }
Because you need to check whether there is an IP connected to the server when the server is started, you need to open a new thread in the start method to call the Accept method
Thread threadAccept = new Thread(Accept); //Receive a delegate method = true; //Set as background thread ();
The next step is to send messages to the server
ClientControl class code:
public void Send(string msg) { (Encoding.(msg)); }
Client main program call code:
("Please enter the content you want to send, enter exit to exit:"); string msg = (); while (msg != "exit") { (msg); msg = (); }
The next step is to receive the client input on the server
private void Receive(object obj) { Socket client = obj as Socket; IPEndPoint point = as IPEndPoint; try //Prevent client exceptions from being disconnected and thrown out exceptions { byte[] msg = new byte[1024]; int msglen = (msg); ( + "[" + + "]:" + Encoding.(msg, 0, msglen)); (Encoding.(Encoding.(msg, 0, msglen))); //A small function has been added in this place, which means that the client sends a message and the server sends it back the same thing, and it can be removed Receive(client); //Tail recursion } catch { ( + "[" + + "]:" + "Still disconnected"); }
This method execution will also suspend the thread, so a new thread must be opened. Because this is receiving the client's message, the new thread must be written in the Accept method I just wrote.
Thread threadReceive = new Thread(Receive); = true; (client);
Attached:
Just now, I didn't write that a server received a message and would return the same message to the client, so I also had to receive the server's message on the client. The method was similar to receiving it on the server.
private void Receive() { try { byte[] msg = new byte[1024]; int msglen = (msg); ("The server said:" + Encoding.(msg)); Receive(); } catch { ("The server has been disconnected"); } }
I thought I had to check if there were any messages on the server, so I opened a thread and wrote it in the client's connect method.
Thread threadReceive = new Thread(Receive); = true; ();
OK, the simplest chat room is finished
If you want to log in for multiple people, just find the client's debug folder in vs. Find the exe file to run
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.