Summary of network programming (socket programming) knowledge points

What exactly does TCP/IP refer to?

Answer: The collection of protocols associated with the Internet is collectively called TCP/IP . There is also a saying that TCP/IP refers to the two protocols TCP and IP . There is also a saying that TCP/IP is the collective name of the protocol family used in the communication process of the IP protocol.

 

How many layers can the TCP/IP protocol suite be divided into? Which layers are they?

Answer: 4 layers. Application layer, transport layer, network layer, data link layer.

 

Introduce the application layer

Answer: The application layer determines the activities of communication when providing application services to users.

Various general application services are pre-stored in the TCP/IP protocol suite. For example , FTP ( File Transfer Protocol ) and DNS ( Domain Name System ) services are two of them.

The HTTP protocol also belongs to the application layer.

 

Introduce the transport layer

Answer: The transport layer provides data transmission between two computers in a network connection to the upper application layer.

The transport layer consists of two different protocols: TCP (Transmission Control) and UDP (User Datagram Protocol).

 

Introduce the network layer

Answer: The network layer is used to process the data packets flowing on the network. A packet is the smallest unit of data transmitted over a network. This layer specifies the path (the so-called transmission route) to reach the computer of the other party and transmit the data packet to the other party.

When transmitting to and from the other computer through multiple computers or network devices, the role of the network layer is to rotate a transmission route among numerous options.

IP ( Internet Protocol ) The Internet Protocol is located at the network layer. The role of the IP protocol is to transmit various data packets to each other. To ensure that it is indeed transmitted to the other party, various conditions need to be met. Two of the important conditions are IP address and MAC address.

The IP address indicates the address to which the node is assigned, and the MAC address indicates the fixed address to which the network card belongs. IP addresses can be paired with MAC addresses. The IP address can be changed, but the MAC address basically does not change.

Note: Do not confuse IP protocol and IP address.

 

Introduce the data link layer

A: The part of the hardware that handles the connection to the network. Including the control operating system, hardware device drivers, network cards, optical fibers and other physically visible parts. The scope of hardware is within the scope of the link layer.

 

Which layer does the HTTP protocol belong to in the TCP/IP protocol suite?

Answer: Application layer.

 

Which layer of the TCP/IP protocol family belongs to the TCP protocol and the UDP protocol?

Answer: The network layer.

 

Which layer does the IP protocol belong to in the TCP/IP protocol suite?

Answer: Transport layer.

 

Which layer of the TCP/IP protocol suite does the hardware parts such as network cards and optical fibers belong to ?

Answer: Data link layer.

 

Combined with the TCP/IP protocol suite, what steps are required to send an HTTP request?

Answer: 1. The application layer of the sender sends an HTTP request

2. At the transport layer ( TCP protocol), the data ( HTTP request message) received from the application layer is divided, and each message is marked with a sequence number and port number and forwarded to the network layer.

3. In the network layer ( IP protocol), add the IP address as the communication destination and forward it to the link layer.

4. The link layer sends the request to the receiving server.

5. The link layer of the receiving end server receives the data and sends it to the upper layer in sequence until it reaches the application layer. When it is transmitted to the application layer, the HTTP request sent by the sender can be really received .

 

When the sender transmits data between layers, each time it passes through a layer, it must be marked with a header information to which the layer belongs. Conversely, when the receiver transmits data between layers. The corresponding header will be eliminated each time it passes through a layer.

 

The DNS service is located at the application layer like the HTTP protocol. What is its role?

A: Provides resolution services between domain names and IP addresses. (DNS)

 

Describe the three-way handshake of the TCP protocol

Answer: 1. The sender first sends a data packet with the SYN (sychronize) flag to the receiver.

2. The receiving end transmits a data packet with the SYN/ACK flag to the transmitting end after receiving it to convey confirmation information.

3. Finally, the sender sends back a data packet with an ACK flag to the receiver, indicating the end of the "handshake".

 

Both UDP and TCP/IP protocols are transport layer communication protocols. What is the difference between them?

Answer: UDP : The data source and destination are encapsulated in data packets, no connection needs to be established; the size of each data packet is limited to 64k ; because there is no connection, it is an unreliable protocol; no connection needs to be established, and the speed is fast.

TCP : Establish a connection to form a channel for data transmission; perform large data transmission in the connection; complete the connection through a three-way handshake, which is a reliable protocol; a connection must be established, and the efficiency will be slightly lower.

Example:

UDP : send text messages

TCP : make a call

 

What is a URL ? What is a URI ? What is the relationship between the two?

Answer: URL , Uniform Resource Locator. URI , Uniform Resource Identifier. URL is a subset of URI .

 

 

Which 7 layers does the 7 -layer network model refer to ?

Answer: Application layer, presentation layer, session layer, network layer, transport layer, link layer, physical layer.

 

We usually say a switch, which layer of equipment does it belong to in the 7th layer of the network?

Answer: It belongs to the link layer.

 

Which layer of the 7 -layer model converts computer-readable data into human-readable data?

A: The presentation layer.

 

Three elements of network programming: IP address, port, protocol ( TCP/IP and UDP ).

Among them: The IP address belongs to the network layer, and the port and protocol belong to the transport layer.

 

What is an IP address? what is it composed of? How many categories?

Answer: An IP address is a unique identification of a computer on a network. An IP address consists of a network number segment and a host number segment . There are 5 categories: A , B , C , D , and E.

 

In the Windows operating system, to query the IP address of the machine, if you use the Doc command, what command can you use to query it?

Answer: ipconfig

 

What is a port? What is the range of valid ports?

A: The port is the identity of the running application. The range of valid ports is 0~65535 .

 

network programming

Data can be exchanged between programs running on different computers connected by a network .

 

Scoket is a mechanism provided for network communication services. There are Sockets at both ends of the communication . Network communication is actually communication between sockets , and data is transmitted between two sockets through IO .

 

Both UDP and TCP/IP protocols are transport layer communication protocols. What is the difference between them?

Answer: UDP : The data source and destination are encapsulated in data packets, no connection needs to be established; the size of each data packet is limited to 64k ; because there is no connection, it is an unreliable protocol; no connection needs to be established, and the speed is fast.

TCP : Establish a connection to form a channel for data transmission; perform large data transmission in the connection; complete the connection through a three-way handshake, which is a reliable protocol; a connection must be established, and the efficiency will be slightly lower.

Example:

UDP : send text messages

TCP : make a call

 

How does the UDP protocol send and receive data? (programming steps)

answer:

send data:

Create the sender Socket object

Create data and package

send data

release resources

 

Receive data:

Create the receiver Socket object

Create received packets

Receive data

Parse the data and display it in the console

release resources

 

How does the TCP/IP protocol send and receive data?

answer:

Send data :

Create a Socket object for the TCP client

Get output stream, write data

release resources

// Create the Socket object of the sender

Socket s = new Socket("192.168.12.92", 8888);

 

// Get the output stream, write data

OutputStream os = s.getOutputStream();

os.write("hello,tcp, here I am ".getBytes());

 

// release resources

s.close();

 

Receive data :

Create a Socket object on the TCP server side

Listen for client connections

Get input stream, read data

release resources

// Create the Socket object of the receiver

ServerSocket ss = new ServerSocket(8888);

 

// Listen for client connections. Returns a corresponding Socket object

Socket s = ss.accept(); // Listen and accept connections to this socket. This method blocks until a connection comes in.

 

// Get the input stream, read the data and display it in the console

InputStream is = s.getInputStream();

 

byte[] bys = new byte[1024];

int len ​​= is.read(bys);  // blocking method

String str = new String (bys, 0, len);

 

String ip = s.getInetAddress().getHostAddress();

 

System.out.println(ip + "---" + str);

 

// 释放资源

s.close();

// ss.close(); //这个不应该关闭

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326050113&siteId=291194637