Chat room based on c#-socket
preface
Source code: https://gitee.com/TL0902/term/blob/master/C%23%E8%81%8A%E5%A4%A9%E5%AE%A4/Tchat.rar
You can download it directly. The development tool is vs2019;
I’ve seen that everyone likes dry goods, and there are ready-made codes. I don’t know you can’t live through Saturday and Sunday!
Recently, I wrote a small program to build a chat room through socket. The tool is vs2019 and the language is c#, which makes it easy to make a graphical interface;
In fact, I originally wanted to throw the code up and finish it, but some students don’t understand socket and its use methods, and may have various bugs first, so I’d like to explain it briefly to you. At the same time, I mainly understand the use of socket and some small knowledge points. I hope you can gain something;
The program can be better optimized. I’ll leave it to you~
Code explanation
socket
introduce
From Baidu Encyclopedia: socket is also called socket
In fact, it is a protocol between two hosts that need to communicate with each other. If you want to have an in-depth understanding, you can have an in-depth understanding of the protocol later;
In fact, it can be easily understood that if a Chinese and an American want to complete communication, they must either use Chinese at the same time or use English. They must reach an agreement. They can communicate only when they reach an agreement on using Chinese. Therefore, the two hosts need to communicate with each other and also need an agreement. Here, we choose socket protocol;
Socket usage
Socket is very simple to use. It’s the same reason to put the elephant into the refrigerator in several steps;
Both the server and the client have the same three parts:
public Socket socket; // Server socket
public Socket client; // Client socket
public byte[] buffer = new byte[1024*1024*2]; // Message buffer
string ip = textBox2.Text;
int port = Convert.ToInt32(textBox3.Text);
//1. Instantiate socket (IP4 search protocol, streaming protocol, TCP protocol)
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//2. Instantiation IP
IPAddress address = IPAddress.Parse(ip);
//3. Create network ports, including IP and port 127.0.0.1:30
IPEndPoint endPoint = new IPEndPoint(address, port);
Server, if we want to write to the server, we only need to enable the socket binding and listening function:
//Server side
//4. Bind socket
socket.Bind(endPoint);
//Output listening information
richTextBox1. Text + = "start listening" + socket LocalEndPoint. ToString() + "\r\n";
//The maximum number of connections that the server can receive
socket.Listen(int.MaxValue);
///Receive client connection
client = socket.Accept();
client, if you want to write to the client, you only need to turn on the socket connection client connection function:
//4.0 establishing connections
client.Connect(endPoint);
richTextBox1. Text + = "successfully connected to the server" + "\ R \ n";
Here you can practice to see whether a simple connection can be realized through the above;
Experience start
Here I’ll talk about the packaging of the project. After downloading, you can first experience the following tchat. Com in the bin directory Exe can directly double-click to run the program:
Program source code address: https://gitee.com/TL0902/term/blob/master/C%23%E8%81%8A%E5%A4%A9%E5%AE%A4/Tchat.rar **The code is here (gitee download directly)**
Here I have made a login interface, without setting the login account and password display, just click login directly;
Page after opening:
Here, take two programs as an example. To chat, you need to run two tchats, one click broadcast and the other click Connect. You can see that the connection is successful:
Now you can send messages to each other. At the same time, I also made a fun little function of ten combos:
Here we have experienced this small program. Let’s take a look at its implementation process;
Sign in
The login page here is actually very simple. It just creates a form at the beginning of the program, and then starts the mode window. Judge whether to open the program through a flag bit. There is no account and password. You can enter the account and password to realize login and registration by yourself. It is a very simple function;
Form1 form1 = new Form1();
form1. ShowDialog(); // Mode window, run the function of form1 first
if (form1.closeflag == false)
{
Application.Run(new main());
}
Multithread synchronization
If you implement the socket connection mentioned in socket, how can we synchronize the sending and receiving of messages with the sending of messages? Here we need to realize thread synchronization. Thread synchronization is not synchronous in theory, but it gives us the feeling that it is synchronous;
Almost every language has a thread to realize thread synchronization. The thread in c# can realize thread synchronization. You only need to create a thread and then start it. It doesn’t need to be explained here. If you don’t know about thread synchronization, you can learn a very important knowledge point. You can’t finish it in detail once or twice. Of course, if necessary, comment and leave a message, If I can, I will write another thread document for you;
Here you can see the applet source code, which is very simple;
C # in the implementation of the thread is through the delegate mechanism, that is, parameterization; You can create a method and start a thread
Only two steps are required:
//1. Create a thread
Thread recvierThread = new Thread(recvierMassage); // Through delegation, the recviermassage method is the thread body
//2. Start a thread
recvierThread.Start();
Here we found that the blogger didn’t explain the multi person chat function. Yes, it’s not perfect. Let’s realize it!
Summary
The blogger here deliberately left a lot of optimization operations for you, and you can continue to improve!
Be sure to do more
There is also a point of praise for the child. It’s OK to leave a note! Thank you very much!