Communication basis, TCP/IP protocol

Simple server and client connection

  1. Understanding the TCP/IP protocol
    For the TCP/IP protocol, Baidu Encyclopedia defines it as a protocol cluster used to transfer data and information between multiple networks. Nowadays, almost all computers follow a protocol for communication.

So how to understand the (TCP/IP) protocol?

Personally, my understanding of the agreement is:
Consensus is a set of guidelines that we all follow together, which is similar to the secret code in spy war films. It also has a prescribed format and statement. People and people say that machines need to interact with each other. When communicating and cooperating, it is necessary to reach a consensus on communication ! For
example, Chinese people speak Mandarin to express their ideas and wishes. To achieve the purpose of collaboration, it can be seen in turn that mutual collaboration between machines also requires a consensus, and the current TCP/IP protocol is this consensus.

The general process is that
I have to get the machine I want to connect with the network IP address (you can check on the computer, search the network properties, IPv4 address) , establish a connection to it after a successful connection is equivalent to my machine to another A machine connects an invisible data transmission line. The other party's data can be sent to me in bytes through this line , and my data can also be sent to the other party in bytes through this line .

In one sentence: TCP/IP protocol is a consensus that computers follow for interaction and collaboration

2. Server and client

Server : ( The definition of Baidu Encyclopedia is ) A server is also a type of computer, which runs faster, has a higher load, and is more expensive than ordinary computers. The server provides computing or application services for other clients (such as PCs, smart phones, ATMs, and even large equipment such as train systems) in the network. The server has high-speed CPU computing power, long-term reliable operation, powerful I/O external data throughput and better scalability. According to the services provided by the server, in general, the server has the ability to respond to service requests, undertake services, and guarantee services. As an electronic device, a server has a very complicated internal structure, but it is not much different from the internal structure of an ordinary computer, such as: cpu, hard disk, memory, system, system bus, etc. (It is a computer that has very powerful computing power and data throughput capacity, can provide the required services for the client (ie, the client), and can guarantee the service for a long time )

Client : Client, or client, refers to a program that corresponds to a server and provides local services to customers. Except for some applications that only run locally, they are generally installed on ordinary clients and need to cooperate with the server to run. (It is a stuff that provides direct service to users, and cooperates with the server to feedback information )

With these basic knowledge: then let's use JAVA (implemented in other languages ​​and under study haha) to implement a simple server and client connection function

Implementation steps :
create a server (Java.net.SeverSocket) -> wait for client access -> after the client access ( that is, a connection is established between the client and the server ), this realizes a client and server connection!
As for the subsequent data transmission, you only need to obtain the input and output streams of the client after the connection is successful, and perform a data transmission between the client and the server through the input and output streams!

Code part:

public class Sever(){
    
    

   //创建服务器 
   /**
   *@parm port(这个port其实是一个标签的意思,就是在特定的IP地址下、创建的标示为port的服务器)
   */
   public void setSever(int port){
    
    
     try{
    
    
       //创建服务器  java.net.SeverSocket
       java.net.SeverSocket sever=new ServerSocket(port);
       //输出服务器创建成功
       System.out.println("服务器创建成功!");
       //等待客户机的接入
       java.net.Socket client=sever.accept();//Line1
       //如果没有客户机接入的的话,他会一直卡在Line1,不会执行下面的代码
       //如果有客户机连接上来了就输出有客户机连接成功了
       System.out.println("有客户端连接进来了!");
     }catch(Exception ed){
    
    
       System.out.println("服务器创建失败!");
       ed.printStackTrace();
     }
   }
   //主函数
   public static void main(String[] args){
    
    
      //创建服务器
      Sever ss=new Sever();
      ss.setSever(9999);
   }  
}

Then create a client to connect to the server

//客户端
public class Client(){
    
    

   //连接服务器
   public void connectSever(){
    
    
      try{
    
    
         //连接服务器 127.0.0.1是每台电脑固定的本机IP地址,9999是刚刚创建的服务器的标识
         java.net.Socket client=new Socket("127.0.0.1",9999);
         System.out.println("连接服务器成功!"); 
      }catch(Exception ed){
    
    
         System.out.println("连接服务器失败!");
         ed.printStackTrace();
      }     
   }
   //主函数
   public static void main(String[] args){
    
    
      Client cli=new Client();  
      cli.connectSever();
   }
}

In this way, a simple server connection is achieved! Next, you can exchange data between the servers. In the next blog, I will share a simple real-time drawing board of mine, and then further implement a video communication, and finally implement a small interface similar to Tencent conference. , Friends who are interested can do it together!

My QQ is: 2058084624
My email is: [email protected]
My WeChat is: 18975010038

Guess you like

Origin blog.csdn.net/qq_48201696/article/details/113104603