TCP / IP programming implements remote file transfer in TCP / IP network structure, in order to ensure network security, network people often need to add a firewall on the router, prohibiting the TCP / IP protocol of secure harmful TCP / IP protocols such as FTP. Sometimes system maintenance personnel need to use FTP to pass some files from the central computer room host to the front-end network, such as the application's replacement upgrade. If you turn on the firewall every time you transfer files, you will not be a bit cumbersome. If you add a special file transfer module in your own application, it will be very pleasant. UNIX network programming generally uses a socket system call. For currently very popular client / server mode, the program is written as follows: 1. Socket system call To perform network I / O, the first thing to do at both ends of the server, the server, and the client is to call the socket () system call. To establish a soft socket, specify the appropriate communication protocol. Format is: #include
(2) The second parameter is bundled to the local address, the structure defines in Sys / Netinet / In.H: struct sockaddr_in {short sin_family; / * socket () system calling protocol, such as AF_INET * / U_SHORT SIN_PORT; / * Network Byte Order Form Port Number * / Struct In_Addr SIN_ADDR; / * Network Byte Ordered Network Address * / CHAR SIN_ZERO [8];} Each network program on a machine uses a certain Independent port numbers, for example: Telnet programs use port number 23, and the FTP file transfer program uses port number 21. When we design an application, the port number can be obtained from the GetServbyName () function from the / etc / service library file, or the Htons (int portnum) function can be converted to the network byte order in the form of network byte order. Some versions The UNIX operating system specifies that the port number below 1024 can only be used by the superuser, and the port number used by the ordinary user program is limited between 1025 and 32767. The network address can be obtained by the gethostbyname function (this function and getServByname () are in the network byte order in the network byte order, and the parameter hostname is a network address in the / etc / hosts file. The corresponding machine name. This function returns a type of HOSTENT-based structural pointer, the Hostent structure is defined in Netdb.h: struct hostent {char * h_name; char ** h_aliases; int h_addrtype; int h_length; / * address length * / char ** h_addr_list; # Define h_addr h_addr_list [0]; / * Address * /} (3) The third parameter is the length of the second structural parameter. If the call is successful, BIND returns 0, otherwise, -1 will return -1 and set Errno. 3. Server-end system calls Listen to enable the server to accept connection format: int Listen (int socketfd, int background) it usually executes before the Accept call call after the socket and bind calls. The second parameter indicates how many connection requirements can be queued when the system is waiting for the ACCEPT call. This parameter is often specified as 5 and is also the maximum allowed currently. 4. The server calls accept to wait for the client to call Connect to connect. The format is as follows: int newSocket = (int socketfd, structure sockaddr_in * peer, int * addrlen); This call acquires the first connection request on the queue and establishes a jacket word having the same characteristics as SOCKFD. If there is no waiting connection request, this call blocks the caller until a connection request arrives. After the connection is successful, the call will fill the parameter peer and addlen with the peer address structure and address length. If you are not interested in the address information of the client, these two parameters are replaced by 0. 5. The client calls Connect () to establish a connection with the server.
After: Connect (int socketfd, struct sockaddr_in * servsddr, int address) client acquires the socket descriptor, use this call to establish a connection with the server, the parameter socketfd is a socket () system call returned to the map character descriptor The second and third parameters are the structure of the destination address and the length of the destination address of byte metering (here the destination address should be server address). The call successfully returns 0, otherwise the -1 will be returned and Errno is set. 6. Once the connection is sent, once the connection is established, you can send and accept data to the network with the system to call Read and Write as normal files. READ accepts three parameters: one is a set of word descriptors; a buffer that will be filled in the data, and an integer indicates the number of bytes to read, it returns the number of bytes actually read, return time -1, when you encounter a file, return 0. Write also accepts three parameters: one is a set of word descriptors; one is a buffer pointing to sending data, and an integer indicates byte number of bytes to write files, it returns the number of bytes actually written, Returns -1 when an error occurs. Of course, it is also possible to call Send and RECV to read and write the set, which calls to the basic READ and WRITE system calls, but only one transmission mode parameter. 7. When exiting the program, you should close the jacket word in normal way. The format is as follows: int Close (socketfd) introduces the basic ideas and steps of UNIX Customer / Server Mode Network programming. It is worth noting that the system calls involved in the socket program are not belonging to the basic system call range, and its function is in the libsocket.a file, so it is necessary to compile the original program with the CC command. Now, we can program with questions starting with the beginning of the article. In the icon network structure, in order to communicate the server of the central computer room and the client on the outlet, you must add the route through the router 1112 to the client on the server side, two clients must Add the router 2221 to the server. In the server / etc / hosts file, you should include the following: 1.1.1.1 Server 2.2.2.2 CLI1 2.2.2.3 CLI2 Client / etc / hosts file should have local address information and server address information, such as CLI1 customers / Etc / hosts file: 2.2.2.2 CLI1 1.1.1.1 After the server network environment is built, we can write FWQ.c programs on the server side, responsible for accepting client's connection requests and read from the source file. Data is sent to the client. The client program khj.c sends a connection request to the server, receives data from the server side, and writes the received data into the target file.
The source program is as follows: / * Server source program fwq.c * / #include