当前位置:网站首页>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

  1. open windows Control panel ( Press win +R )
  2. Input cmd
  1. 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

  1. Enter... In the sender program IP Address
  2. Run the sender program , Enter the message to be sent in the input box
  3. Run the receiver information
  4. Input IP Address
  5. View the message box

Running results

Program

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 .

原网站

版权声明
本文为[Domineering ocean]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210604151112679i.html