Network programming interview written test questions

1. OSI 7-layer model, TCP/IP 4-layer model and 5-layer model. And the function of each layer (emphasis: the third layer and the fourth layer)

answer:

7-layer model (①physical layer: binary bit stream transmission, ②data link layer: reliable transmission of adjacent nodes, ③network layer: addressing and routing selection, ④transport layer: end-to-end reliable transmission, ⑤session layer : session management between hosts, ⑥ presentation layer: data representation, encryption and compression, ⑦ application layer: provide various network application interfaces)

5-layer model (①physical layer, ②data link layer, ③network layer, ④transport layer, ⑦application layer)

4-layer model (①Network interface and physical layer or network access layer: mapping between ip address and physical address (MAC), and encapsulating the last ip message into a frame, converting it into a binary bit stream for transmission, ③Network layer, ④ transport layer, ⑦ application layer)

Network Interface and Physical Layer: Network Access Layer

2. MAC address: 48bit unique in the world. After changing the network environment, the MAC address does not change, and the IP address does not change

Answer: MAC remains unchanged, IP changes

2.1 ARP: Obtain the corresponding mac address through the ip address

2.2 RARP: Obtain the corresponding ip address through the mac address

2.3 MTU MSS, how big are they, and what are their functions

Answer: MTU (Maximum Transmission Unit): The size of the largest data transmission provided by the physical interface to the upper layer, which specifies the maximum data length that the data link layer can transmit, with a maximum of 1500 bytes. MSS (maximum message length): The maximum segment size submitted by TCP to the IP layer, which refers to the maximum length of the data part that is allowed to be transmitted in a TCP message, excluding the TCP header. MSS is TCP to limit the maximum number of bytes sent by the application layer. If MTU =1500, the maximum MSS is 1460bytes

3. Network layer:

  1. IP protocol: Which layer does the router work at: the network layer
  2. ICMP: Internet Control Management Protocol, used to transfer control messages between IP hosts and routers
  3. IGMP: Internet Group Management Protocol, multicast, broadcast

Fourth, the transport layer:

4.1 TCP UDP

Answer: TCP (Transmission Control Protocol): Provides a connection-oriented, one-to-one, reliable data transmission protocol. UDP (User Datagram Protocol): Provides a connectionless, unreliable, best-effort transport protocol (high efficiency)

4.2 What are the similarities and differences between TCP and UDP

Answer: The same point: it is a protocol that belongs to the transport layer. difference:

TCP:

  1. TCP is a connection-oriented, reliable data transmission protocol
  2. TCP provides data error-free, data out-of-sequence, data loss, and data without repeated arrival communication
  3. Low transmission efficiency and high resource consumption
  4. The sending and receiving of data is asynchronous, or the situation of sticky packets occurs

UDP:

  1. UDP is a connectionless, unreliable, best-effort data transfer protocol
  2. Data may be lost, out of order, or duplicated during transmission
  3. High transmission efficiency
  4. The sending and receiving of data is synchronous, and there is no sticky packet phenomenon
  5. Limit the size of data transferred each time, and the excess will be deleted directly

5. IP address:

5.1 IP address classification: Class 2

Answer: IPv4: Use 4 bytes of unsigned integers, a total of 32 bits to store IP addresses. IPv6: Use 16-byte unsigned integers, a total of 128 bits to store IP addresses

5.2 Secondary IP address division: Secondary IP address = network number + host number which types can be assigned to the host.

Answer: Class A: 0.0.0.0~127.255.255.255

Class B: 128.0.0.0~191.255.255.255

Class C: 192.0.0.0~223.255.255.255

Class D: 224.0.0.0~239.255.255.255

Class E: 240.0.0.0~255.255.255.255

Class ABC is the basic class, and IP addresses are assigned to hosts. Class D does not represent the network and is used for special purposes, such as multicast. Class E is not intended for web, reservation, or laboratory use.

5.3 Network address and broadcast address:

Answer: Network address = effective network number + host number with all 0s, for example, 192.168.122.92 network address is 192.168.122.0

Broadcast address = effective network number + host number with all 1s, for example, 192.168.122.92 broadcast address is 192.168.122.255

5.4 Subnet mask: default subnet mask, number of subnet segments, host ID, available host ID, all available host IDs. See Notes for topic details.

Answer: Three-level IP address division: the host number can be divided again by using the subnet mask, which can be divided into 2^n, IP = network number + subnet number + host number

Subnet mask format: The length of the subnet mask is the same as the length of the IP address, which is a 32-bit unsigned integer, consisting of a series of consecutive 1s followed by consecutive 0s

IP address & subnet mask = subnet segment

The number of subnet segments = 2^(the number of 1s added in the subnet mask)

The number of host numbers in each subnet segment = 2^(the number of remaining 0s in the subnet mask)

6. Byte order: Please briefly describe the concept of endian order, and judge the number of bytes in this machine by way of union (union)

Answer: Byte order refers to the way different types of CPU host memory store multi-byte integer sequences, for example: short, int, long. The byte order is divided into big endian byte order and little endian byte order, little endian byte order: low address stores low byte, high address stores high byte. Big-endian: Low addresses store high bytes, high addresses store low bytes. Use unions to determine native byte order:

#include <stdio.h>
union t
{
    int  a;
    char b;
};
int main(int argc,const char argv[])
{
    union t test;
    test.a = 1;
    if(1 == test.b)
    printf("这是小端存储\n");
    else if(0 == test.b)
    printf("这是大端存储\n");
}

7. TCP

7.1 TCP flow chart

7.2 Can the send function be replaced by other functions

Answer: Yes, when the flag in send == 0, the send function can be replaced by write or sendto. At this time, the last two parameters of sendto are NULL and 0 respectively.

7.3 Can the recv function be replaced by other functions

Answer: Yes, when the flag in recv == 0, the recv function can be replaced by read or recvfrom. At this time, the last two parameters of recvfrom are NULL

8. UDP

8.1 Flowchart of UDP

8.1 Can the recvfrom function be replaced by other functions

Answer: Yes, when flag == 0 and the last two parameters are NULL, it can be replaced with recv and read functions

8.2 Can the sendto function be replaced by other functions ----> connect

Answer: Yes, when flag == 0, it can be replaced with send and write functions, but the premise is that the connect function must be called first

8.3 Can the connect function be called in udp. (comparison with TCP, advantages)

Answer: In UDP, the connect function can be called.

  1. The connect function in TCP will generate a three-way handshake to connect the client to the server. The connect function in UDP will not generate a connection. It only records the IP and port number of the peer in the kernel socket. At this time, UDP can only communicate with Recorded peers communicate
  2. The connect function in TCP can only be called once, and the connect function in UDP can be called multiple times, but it will refresh the IP and port number of the peer in the kernel. If you want to clear the address information of the peer in the kernel, you need to set the address information structure Set sin_family to AF_UNSPEC, and then call connect
  3. When UDP adopts the connect function, the sendto function can be replaced by send and write
  4. UDP calling the connect function can improve transmission efficiency
  5. UDP calls the connect function to increase transmission stability

Nine, broadcast IP and multicast IP (process)

9.1.1 Broadcast sender

  1. socket Create a newspaper socket
  2. setsockopt set to allow broadcast level: SOL_SOCKET optname: SO_BROADCAST
  3. bind is not required to bind
  4. Fill in the receiver's address information structure for use by the sendto function, IP: Broadcast IP is consistent with the one bound to the receiver (ps: 0.0.0.0 cannot be filled in) PORT: 1024~49151, consistent with the one bound to the receiver
  5. sendto send

9.1.2 Broadcast Receiver

  1. socket Create a newspaper socket
  2. Fill in the receiver's own address information structure and use it for the bind function
    1. IP: Bind broadcast IP (192.168.123.255 or 255.255.255.255 or 0.0.0.0)
      1. 0.0.0.0: Once bound to the socket, all available IP addresses of the machine will be bound to the socket. For example:
        1. The local IP from ifconfig is 192.168.122.120
        2. Local loopback IP: 127.0.0.1
        3. Broadcast IP
        4. Multicast IP
    2. PORT:1024~49151。
  3. bind must bind
  4. recvfrom receive data

9.2.1 Multicast sender

  1. socket Create a newspaper socket
  2. bind is not required to bind
  3. Fill in the receiver's address information structure for use by the sendto function, IP: multicast is consistent with the one bound to the receiver (ps: 0.0.0.0 cannot be filled), PORT: 1024~49151, consistent with the one bound to the receiver
  4. sendto send

9.2.2 Multicast receiver

  1. socket Create a newspaper socket
  2. setsockopt join multicast group level: IPPROTO_IP optname: IP_ADD_MEMBERSHIP
  3. Fill in the receiver's own address information structure and use it for the bind function
    1. IP: Bind multicast IP, consistent with the joined multicast (224.0.0.0 - 239.255.255.255 or 0.0.0.0)
      1. 0.0.0.0: Once bound to the socket, all available IP addresses of the machine will be bound to the socket. For example:
        1. The local IP from ifconfig is 192.168.122.120
        2. Local loopback IP: 127.0.0.1
        3. Broadcast IP
        4. Multicast IP
    2. PORT:1024~49151。
  4. bind must bind
  5. recvfrom receive data

10. Multi-process concurrent server and multi-thread concurrent server model

10.1 Multi-process concurrent server

void handler(int sig){
    while(waitpid(-1, NULL, WNOHANG) > 0);
}

signal(17, handler);
sfd = socket();
bind();
listen();
while(1){
    newfd = accept();
    if(fork() == 0){
        close(sfd);
        while(1){
            recv();
            send();        
        }    
        close(newfd);
        exit(0);
    }
    close(newfd);
}
close(sfd);

10.2 Multi-threaded concurrent server

sfd = socket();
bind();
listen();
while(1){
    newfd = accept();
    pthread_create(&tid, NULL, deal_cli_msg, &info);
    pthread_detach(tid);
}
close(sfd);

void* deal_cli_msg(void* arg){
    newfd = arg->newfd;
    cin = arg->cin;
    
    while(1){
        recv();
        send();    
    }
    close(newfd);
    pthread_exit();
}

11. Classification of IO multiplexing? Please briefly describe the process (principle) of one of them?

Answer: IO multiplexing is divided into select, poll, epoll, select TCP server model

sfd = socket();
bind();
listen();
while(1)
{
    tempfds = readfds
    select(maxfd+1,&tempfds,NULL,NULL,NULL);
    for(int i=0;i<maxfd;i++)
    {
        if(FD_ISSET(i,&tempfds) == 0) continue;
        if(0 == i)
        {
            fgets();        
        }    
        else if(sfd == i)
        {
            newfd = accept();
            FD_SET(new,&readfds);
            maxfd = maxfd > newfd ? maxfd : newfd;        
        }
        else
        {
            res = recv();
            if(0 == res)
            {
                close(i);
                FD_CLR(i,&readfds);
                while(FD_ISSET(maxfd,&readfds) == 0 && maxfd-- > 0)
                continue;            
            }      
            send();                                       
        }
    }
}
close(sfd);

Guess you like

Origin blog.csdn.net/weixin_53478812/article/details/132394252