当前位置:网站首页>How does wechat and QQ chat work? So simple!!!
How does wechat and QQ chat work? So simple!!!
2022-06-24 11:16:00 【Domineering ocean】
demand
I believe all of us have used or are using buckle and wechat .QQ Is an Internet-based instant messaging software . When we enjoy the convenience and intelligence of instant messaging , Have you ever thought about ,QQ、 How to realize the chat function of wechat ?
function
We first need to know the network addresses of us and each other , Now the mainstream ip The address is IPV4 and IPV6. Then we can communicate two-way through these addresses , Realize the function of chat room .
Realization
View this machine IP
- open windows Control panel ( Press win +R )
- Input cmd
- Input ipconfig View this machine IP
The sender
The program in this location is mainly to let you understand how to make the sender program . The specific and complete project engineering documents will have all the procedures below for you to download .
// Get current IPv4 Address private string GetIpAddress() { string hostName = Dns.GetHostName(); // Get the local name IPHostEntry localhost = Dns.GetHostByName(hostName); // Method expired , Can get IPv4 The address of //IPHostEntry localhost = Dns.GetHostEntry(hostName); // obtain IPv6 Address IPAddress localaddr = localhost.AddressList[0]; return localaddr.ToString(); } public void UdpSend() { //Parse() Decimal system iP Turn it into IPAddress class , binding IP Address and port remoteIPEp = new IPEndPoint(IPAddress.Parse("IP Address "), CLIENT_UDP_PORT); // Create and send data Socket Object and data buffer udpSendDataBuf = new byte[1024]; skUdpSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // by false, Waiting for execution mreUdpShutDown = new ManualResetEvent(false); mreUdpSend = new ManualResetEvent(false); whUdp = new WaitHandle[2]; whUdp[0] = mreUdpShutDown;//UDP Closing order whUdp[1] = mreUdpSend; //UDP Service end command int iWaitRetCode; iWaitRetCode = WaitHandle.WaitAny(whUdp, 1000); // Wait for any element in the specified array to receive the signal , Use at the same time TimeSpan Specify the time interval byte[] b_txt; int iByteLen; // Meet the conditions while (iWaitRetCode != 0) { switch (iWaitRetCode) { case 1:// send data b_txt = Encoding.UTF8.GetBytes(strSendTxt); iByteLen = b_txt.Length; // Initialize cache data Array.Clear(udpSendDataBuf, iWaitRetCode, iByteLen); // Copy the array Array.Copy(b_txt, udpSendDataBuf, iByteLen); // Send to specified IP skUdpSend.SendTo(udpSendDataBuf, remoteIPEp); // Consume an event mreUdpSend.Reset(); break; case WaitHandle.WaitTimeout:// Overtime break; } // Continue with the next event detection iWaitRetCode = WaitHandle.WaitAny(whUdp, 1000); } skUdpSend.Close(); skUdpSend = null; } #endregion UDP Send thread thread public static string strSendTxt; private void button1_Click(object sender, EventArgs e) { mreUdpShutDown.Set(); } private void button2_Click(object sender, EventArgs e) { strSendTxt = GetIpAddress() +":"+ textBox1.Text + "\r\n"; mreUdpSend.Set(); } private void liaotian_Load(object sender, EventArgs e) { // Thread start ThreadStart theStart = new ThreadStart(UdpSend); Thread theThr = new Thread(theStart); theThr.Start(); } private void liaotian_FormClosing(object sender, FormClosingEventArgs e) { mreUdpShutDown.Set(); }
The receiver
The program in this location is mainly to let you understand how to make the sender program . The specific and complete project engineering documents will have all the procedures below for you to download .
namespace FormUdpSend { public partial class send : Form { public send() { InitializeComponent(); } #region UDP Send thread public static int iUdpRecvPkgLen; public static Socket skUdpSend; public static byte[] udpSendDataBuf; public static IPEndPoint remoteIPEp; //IP Binding of address and port public static int CLIENT_UDP_PORT = 0x5555; // Assigned to Port attribute , For example ipv4 public static ManualResetEvent mreUdpShutDown;//UDP Closing order public static ManualResetEvent mreUdpSend;//UDP dispatch orders public static WaitHandle[] whUdp; //UDP Correlation handle // Get current IPv4 Address private string GetIpAddress() { string hostName = Dns.GetHostName(); // Get the local name IPHostEntry localhost = Dns.GetHostByName(hostName); // Method expired , Can get IPv4 The address of //IPHostEntry localhost = Dns.GetHostEntry(hostName); // obtain IPv6 Address IPAddress localaddr = localhost.AddressList[0]; return localaddr.ToString(); } public void UdpSend() { //Parse() Decimal system iP Turn it into IPAddress class , binding IP Address and port remoteIPEp = new IPEndPoint(IPAddress.Parse("169.254.7.216"), CLIENT_UDP_PORT); // Create and send data Socket Object and data buffer udpSendDataBuf = new byte[1024]; skUdpSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // by false, Waiting for execution mreUdpShutDown = new ManualResetEvent(false); mreUdpSend = new ManualResetEvent(false); whUdp = new WaitHandle[2]; whUdp[0] = mreUdpShutDown;//UDP Closing order whUdp[1] = mreUdpSend; //UDP Service end command int iWaitRetCode; iWaitRetCode = WaitHandle.WaitAny(whUdp, 1000); // Wait for any element in the specified array to receive the signal , Use at the same time TimeSpan Specify the time interval byte[] b_txt; int iByteLen; // Meet the conditions while (iWaitRetCode != 0) { switch (iWaitRetCode) { case 1:// send data b_txt = Encoding.UTF8.GetBytes(strSendTxt); iByteLen = b_txt.Length; // Initialize cache data Array.Clear(udpSendDataBuf, iWaitRetCode, iByteLen); // Copy the array Array.Copy(b_txt, udpSendDataBuf, iByteLen); // Send to specified IP skUdpSend.SendTo(udpSendDataBuf, remoteIPEp); // Consume an event mreUdpSend.Reset(); break; case WaitHandle.WaitTimeout:// Overtime break; } // Continue with the next event detection iWaitRetCode = WaitHandle.WaitAny(whUdp, 1000); } skUdpSend.Close(); skUdpSend = null; } #endregion UDP Send thread thread public static string strSendTxt; private void button1_Click(object sender, EventArgs e) { mreUdpShutDown.Set(); } private void button2_Click(object sender, EventArgs e) { strSendTxt = GetIpAddress() +":"+ textBox1.Text + "\r\n"; mreUdpSend.Set(); } private void Form1_Load(object sender, EventArgs e) { // Thread start ThreadStart theStart = new ThreadStart(UdpSend); Thread theThr = new Thread(theStart); theThr.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { mreUdpShutDown.Set(); } } }
Usage flow
- Enter... In the sender program IP Address
- Run the sender program , Enter the message to be sent in the input box
- Run the receiver information
- Input IP Address
- View the message box
Running results
follow-up
If you want to learn more about the Internet of things 、 Smart home project knowledge , You can pay attention to my official account. .
It's not easy to write , Thank you for your support .
边栏推荐
- H5 video conference, camera monitoring, web streaming and live broadcast integration scheme
- 把騰訊搬到雲上,治愈了他們的技術焦慮
- “一次编写,运行各端”,高通重磅发布 AI 软件栈!
- 2008R2 precautions for configuring L2TP pre shared key VPN
- Clickhouse deployment and basic usage 1
- Nacos source code - configure automatic update
- Which map navigation is easy to use and accurate?
- 喜欢就去行动
- 腾讯开源项目「应龙」成Apache顶级项目:前身长期服务微信支付,能hold住百万亿级数据流处理...
- [activities this Saturday] NET Day in China
猜你喜欢
MYSQL_精讲数据库数据类型
PHP SMS notification + voice broadcast automatic double call
脚本之美│VBS 入门交互实战
程序员大部分时间不是写代码,而是。。。
Today's sleep quality record 76 points
"Write once, run at all ends", Qualcomm released AI software stack!
齐次坐标的理解
Group counting_ Structure and workflow of CPU
Maui's way of learning -- Opening
Any and typevar make the automatic completion of IDE better
随机推荐
Group policy export import
Why use a firewall? What is the function of firewall?
Many of my friends asked me what books and online classes I recommended. This time, I contributed all the materials that I had been hiding for a long time (Part 1)
工具及方法 - 在Source Insight中使用代码格式化工具
Audio knowledge (III) -- MFCCs code implementation
喜歡就去行動
Canvas pipe animation JS special effect
[Qianfan 618 countdown!] IAAs operation and maintenance special preferential activities
Suddenly I thought of the wooden house in my hometown
Any 与 TypeVar,让 IDE 的自动补全更好用
Maui的学习之路 -- 开篇
Google Earth Engine(GEE)—如何新增一个图例在Map面板
Fais ce que tu veux.
Smart energy: scenario application of intelligent security monitoring technology easycvr in the petroleum energy industry
Déplacer Tencent sur le cloud a guéri leur anxiété technologique
Qt: 判断字符串是否为数字格式
Go basic series | 4 Environment construction (Supplement) - gomod doubts
Maui's way of learning -- Opening
What are the means of network promotion?
Apple's legendary design team disbanded after jobs refused to obey cook