The communication process and schematic diagram from the client to the server are very good

To learn anything, as long as we understand the principle, we will bypass it. Now that I have learned what I have learned, I would like to summarize the communication process from the client to the server. Only when we understand the principle, we will understand that when our program development process error problems will appear there, we will better solve the problem.

    We must first understand a conceptual vocabulary: Socket

    The English meaning of socket is "hole" or "socket". As the process communication mechanism, take the latter meaning. Usually also called "socket", it is used to describe the IP address and port, and is the handle of a communication chain. ( In fact, it is used for communication between two programs .) The socket is very similar to the socket of the phone. Take a telephone network as an example. The two sides of the phone are equivalent to two programs that communicate with each other, and the phone number can be regarded as an IP address. Before any user talks, they must first own a telephone, which is equivalent to applying for a socket; at the same time, they must know the other party's number (IP address), which is equivalent to having a fixed socket. Then dialing the call to the other party is equivalent to sending a connection request. If the other party is present and idle, pick up the phone microphone, the two parties can formally talk, which is equivalent to a successful connection. The process of two parties talking is the process of sending a signal to the phone and receiving the signal from the phone, which is equivalent to sending data to the socket and receiving data from the socket. After the call is over, one party hangs up the phone to close the socket, cancel the connection, and the communication is complete.

    The above communication is based on two people talking as an example to explain the general communication, but now if one of the people in the communication is a foreigner (speaks English) and one is a Chinese (speaks Mandarin), they communicate with each other , Ca n’t understand what the other party is saying, so their communication ca n’t be completed. But if we give a rule that both parties can only speak Putonghua, there is no barrier to communication between the two parties. This leads to the communication protocol.

There are two types: (Tcp protocol and Udp protocol):

The Tcp protocol and Udp protocol is a data syntax for communication transmission between two hardware devices .

– Streaming Socket (STREAM):

    It is a connection-oriented Socket, aimed at connection-oriented TCP service applications, safe, but inefficient; Tcp: transmitted in the form of a stream.

– Datagram Socket (DATAGRAM):

    It is a connectionless Socket, which corresponds to the connectionless UDP service application. It is insecure (lost, disordered, it needs to analyze the rearrangement and request for retransmission at the receiving end), but it is efficient. Udp: The data packet is split into Several numbers were transmitted later. It is easy to lose data during the transmission process. But the transmission speed is faster than TCP.

Socket communication process

  • Demo:
  • Service-Terminal:

– Apply for a socket  (socketWatch) to monitor

– Bind to an IP address and a port

– Start listening and wait for the client to connect

– When there is a connection, create a socket (socketConnection) to communicate with the client

– Continuous monitoring, waiting for the next client's connection

code show as below:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Net.Sockets;

using System.Net;

using System.Threading;

namespace MyChatRoomClient

{

    public partial class FChatClient : Form

    {

        public FChatClient()

        {

            InitializeComponent();

            TextBox.CheckForIllegalCrossThreadCalls = false;

        }

        Thread threadClient = null; //客户端 负责 接收 服务端发来的数据消息的线程

        Socket socketClient = null;//客户端套接字

//客户端发送连接请求到服务器

        private void btnConnect_Click(object sender, EventArgs e)

        {

            IPAddress address = IPAddress.Parse(txtIP.Text.Trim());//获得IP

            IPEndPoint endpoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim()));//网络节点

//创建客户端套接字

            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //向 指定的IP和端口 发送连接请求

            socketClient.Connect(endpoint);

            //客户端 创建线程 监听服务端 发来的消息

            threadClient = new Thread(RecMsg);

            threadClient.IsBackground = true;

            threadClient.Start();

        }

        /// <summary>

/// 监听服务端 发来的消息

/// </summary>

        void RecMsg()

        {

            while (true)

            {

                //定义一个 接收用的 缓存区(2M字节数组)

                byte[] arrMsgRec = new byte[1024 * 1024 * 2];

                //将接收到的数据 存入 arrMsgRec 数组,并返回 真正接收到的数据 的长度

               int length=  socketClient.Receive(arrMsgRec);

                //此时 是将 数组 所有的元素 都转成字符串,而真正接收到的 只有服务端发来的几个字符

               string strMsgRec = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);

                ShowMsg(strMsgRec);

            }

        }

        void ShowMsg(string msg)

        {

            txtMsg.AppendText(msg + "\r\n");

        }

    }

}

Communication process diagram

Through the above flow chart, we can see that a basic communication process between the client and the server, summarize the role of the general application mode of Socket (client and server):

Server: There are at least two sockets. One is the server that listens to the client for connection requests, but is not responsible for communicating with the requested client. The other is that whenever the server successfully receives the client, the server Create a socket to communicate with the requesting client.

Client: Specify the server address and port to connect to, and create a socket object to initialize a TCP connection to the server.

Reprinted from: http://www.codeceo.com/article/client-to-server.html

 

Published 24 original articles · praised 46 · 50,000+ views

Guess you like

Origin blog.csdn.net/m0_37777700/article/details/104041376