1. Introduction

Linux is a free UNIX class multi-user, multi task operating system, which can run on Intel 80386 and higher grade PC, arms, MIPs and PowerPC and other computer platforms. It has become a computer operating system with wide application, high reliability and powerful functions With the advantages of small kernel, high efficiency, open source code and so on, it also contains TCP / IP network protocol, which is very suitable for use in the server field. One of the main uses of the server is network communication. With the application and promotion of computer office automation processing technology and the continuous popularization of network, the traditional paper file transmission mode is no longer suitable for the development needs We are looking forward to a convenient, efficient, environment-friendly and safe network transmission mode.

This paper is to consider this situation, combined with the principle of TCP / IP network communication based on Linux operating system, gives an example of file transmission based on TCP / IP programming. Therefore, the research of TCP / IP network communication is of great significance.

2. Overview of TCP / IP protocol

TCP / IP, namely transmission control protocol / Internet protocol, is a protocol family composed of multiple protocols. It defines the specifications for computers to communicate with each other through the network and among different levels of protocol family. Figure 1 describes the implementation mechanism of IP protocol family by Linux.

Case analysis of file transmission based on TCP / IP programming

Linux supports BSD socket and all TCP / IP protocols. It is realized by network protocol as a group of connected software layers Socket) is supported by the general socket management software. The software is INET socket layer, which is used to manage the interconnection between TCP and UDP ports based on IP. From the perspective of protocol layering, IP is a network layer protocol, and TCP is a reliable port to port transport layer protocol. It uses IP layer to transmit and receive messages, and is also connection oriented. Through the establishment of a virtual electricity The route transmits messages between different networks to ensure no loss and no repetition. User datagram protocol (UDP) also uses IP layer to transmit messages, but it is a non connection oriented transport layer protocol. When using IP layer to transmit messages, when the destination internet protocol layer receives the IP message, it must identify the upper layer protocol (i.e. transport layer protocol) used by the message. Therefore, there is a “Protocol” field in the IP header. Through the value of this field, the upper layer protocol type can be identified. The biggest difference between transport layer and network layer in function theory is that the former provides process communication capability, while the latter cannot. In the sense of process communication, the final address of network communication is not only the host address, but also some kind of identifier that can describe the process. Therefore, TCP / UDP puts forward the protocol port (Protocol) For example, web server processes usually use port 80. These registered port addresses are found in the / etc / services file.

For TCP transmission, the transmission nodes should establish a connection first, and then transmit the ordered messages through the connection to ensure the correctness of transmission. The codes in IP layer are used to implement the internet protocol. These codes add the IP header to the transmission data, and transmit the received IP messages to the TCP layer or UDP layer correctly. TCP is a connection oriented protocol, while UDP is a non connection oriented protocol. When a UDP message is sent out, Linux does not know whether it has successfully reached the destination host. Under the IP layer, there are network device layers supporting all Linux network applications, such as point to point protocol (PPP) and Ethernet layer. Network devices do not always represent physical devices. Some of them (such as loopback devices) are pure software devices. Unlike standard Linux devices, network devices are not created by mknod command. They must be found and initialized by the underlying software before they are created and available. Therefore, the / dev / eth0 file will not be available until the kernel of the Ethernet device driver is set correctly. ARP protocol is between the IP layer and the protocol layer that supports address resolution.

3. Principle of network communication

All network communication can be divided into two types: line switching and packet switching. Computer networks generally adopt packet switching, TCP uses packet switching technology. All data transmitted in computer networks are sent in the unit of packet. Packets are composed of “header” and “message”. The structure is shown in Figure 2. The address of sending host is recorded in the header, The receiving host address and information related to the message content are recorded in the “message”. Each host and router in the network has a routing address table. According to this routing table, packets can be transmitted to the corresponding destination host through the network.

Case analysis of file transmission based on TCP / IP programming

Socket is a very important concept in network communication. Simply speaking, socket is the ID of network process. In the final analysis, network communication is the communication of process. In the network, each node has a network address (i.e. IP address). When two processes communicate, the network address of each network node should be determined first, but the network address can only be determined The computer where the program is located. There may be multiple network processes on a computer at the same time, and it is not sure which process it is. Therefore, there must be other information in the socket, that is, the port number. In a computer, a port can only be assigned to one process at a time, that is, the port number and the process are one-to-one corresponding Address can uniquely identify a network process in the Internet. It can be thought that: socket = network address + port number

The system calls a socket () to get a socket descriptor, and then it can communicate through the network.

There are many kinds of sockets, and there are two most commonly used ones: streaming socket and datagram socket. In Linux, they are called “sock”_ Stream “and” sock “_ Dgram “they use different protocols, TCP protocol is used for streaming socket, UDP protocol is used for datagram socket, and streaming socket protocol is used in this paper.

4. Application of network communication principle in file transmission program design

The vast majority of communication on the network adopts the client / server mechanism, that is, the server provides services, and the client is the user of these services. The server first creates a socket, then binds the socket with the local address / port number (bind()), and then listens on the corresponding socket (listen()). When the accept() function captures a connection service (connect()) request, it accepts and generates a new socket, And communicate with the client through the new socket. The client also needs to create a socket, bind the socket with the local address / port number, specify the server address and port number, and then send connect () to the server. After the request is received by the server, it can communicate with the server through the socket.

TCP is a kind of connection oriented, reliable, two-way communication data flow. It is said that it is reliable because it uses three-phase handshake protocol to transmit data, and uses “retransmission positive confirmation” mechanism to ensure the correct transmission of data: after receiving the data, the receiving end must send a positive confirmation, and the sending end must be able to receive this positive signal, otherwise, the data will be retransmitted Hair. On the basis of this principle, an example of file transfer based on TCP / IP programming under Linux operating system is designed. When we use the client / server mode for communication, the workflow of sending / receiving data is shown in Figure 3.

Case analysis of file transmission based on TCP / IP programming

File transfer is designed based on the client / server model. TCP is used to establish continuity between the client and the server. Because file transfer is an interactive session system, the client needs to establish control connection and data connection with the server every time it performs file transfer. The control connection is responsible for transmitting control information, using control commands, and the client can send to the server The server establishes a data connection with the client for actual data transmission. After the data transmission is completed, the corresponding data connection is cleared, and the control connection is still maintained, waiting for the client to issue a new transmission request until the client cancels the control connection and ends the session.

When the file is transferred, the server first sends a connection request to the server. After the server verifies the identity, it establishes a connection with the client, and the two parties enter into the session state. At this time, as long as the client sends a data connection request to the server and establishes the data connection, both parties enter the data transmission state. After the data transmission is completed, the data connection is revoked, and the cycle is repeated until The end of the session enables the transfer of files from the server to the client.

5. Program design flow of file transfer

5.1 client TCP application process

(1) First, the socket () is used to create the local socket and assign a value to the server socket address structure.

(2) The connect() function is used to make the local socket send a connection request to the server socket interface, and the TCP connection is established through three handshakes.

(3) Use the read() function to read the name of the file to be received and the contents of the file stored in the memory.

(4) Use the open() function to open the new target file created by the client. If it is not created, the function will automatically generate the target file and wait for the contents of the file to be stored.

(5) Finally, write() function is used to store the contents of the read file in the new target file to realize the file transfer from the server to the client.

(6) When the communication ends, close the socket with close() and stop receiving files.

5.2 server side TCP application process

(1) First, open the read file waiting to be transferred with open() function;

(2) Socket () is used to create socket and assign a value to socket address structure;

(3) Bind socket interface with bind() function;

(4) Use the listen() function to listen to the request on the interface;

(5) Accept the request with accept() function, generate new socket interface and description word, and connect with the client;

(6) The lseek() function is used to move the pointer of the source file used for reading to the file header every time the client connection is accepted;

(7) Read a certain length of source file data with read() function;

(8) Finally, the read source file data is stored in memory by write() function, so that the client can read it;

(9) When the transfer is finished, close all processes with close() to end the file transfer.

In the process of file transmission, it is very important that when the server starts to send data, the client should receive the file data at the same time. If the client is not running, the server will always wait for the client to send the request. When the server source file is sent, the client will receive the source file data completely and generate a new target file, so as to realize the file network communication.

6. Conclusion

Linux operating system has a strong development potential in network application. At the same time, Linux is also a system with high reliability and security. Therefore, in the research and development of network communication based on TCP / IP, Linux operating system is usually selected as the development platform.

This paper introduces the practical application of TCP / IP network communication based on Linux operating system, which is mainly used for the network transmission of files, and solves the problem of file transmission efficiency. As a further improvement, security mechanisms such as authentication, permission allocation and file encryption can be added in the process of file transmission to ensure that some important files will not be leaked in the process of transmission The design can be widely used in enterprise office area network.

Editor in charge

Leave a Reply

Your email address will not be published. Required fields are marked *