TCPIP Programming Remote File Transfer

xiaoxiao2021-03-06  35

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 #include int socket (int name, int type, int protocol): (1) Family Indicates the set fifth family, its value includes: AF_UNIX (UNIX internal protocol) AF_INET (ITERNET protocol) AF_NS (Xeroxns protocol, TCP / IP program) AF_IMPLINK (IMP link layer) (2) TYPE specifies a sleeve type, with: SOCK_STREAM (Flow socket ) SOCK_DGRAM (Data Supply Set) SOCK_RAW (Original Socket) SOCK_SEQPACKET in general, the combination of the first two parameters can determine the protocol used, at this time, the third parameter is Set to 0, if the first parameter is an AF_INET, the second parameter selection SOCK_STREAM, the protocol used is TCP; the second parameter selection SOCK_DGRAM, the protocol used is UDP; when the second parameter is selected SOCK_RAW, use The protocol is IP. It is worth pointing out that it is not a combination of all the families and types. Please refer to the relevant information. If the system call returns a similar file descriptor, it is a set of file descriptors, which can be performed using READ and WRITE as a file descriptor. When a process is used in the soft socket, you need to use Close ( Close (see the following content). 2. Server-side BIND system calls the soft outlet to create, not with any address, must use bind () The system calls to establish an address contact. The format is: #include #include int bind (int socketfd, struct sockaddr_in * localaddr, sizeof (localaddr)); where :( 1) The first parameter socketfd is the first step SOCKET () system call returns a set of graphical descriptors.

(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 1112 to the client on the server side, two clients must Add the router 2221 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 #include #include #include #include #include #include main () {char C, BUF [1024], File [30]; int fromlen, source; register int k, s , ns; struct sockaddr_in sin; struct hostent * hp; system ("clear"); Printf ("/ n"); Printf ("/ N / N / T / T input to transfer file name:"); scanf "% S", file); if (Source = Open (file, o_rdonly)) <0) {PERROR ("Source File Opening error"); exit (1);} printf ("/ n / t / t Transfer file, wait ... "); hp = gethostbyname (" server "); if (hp == null) {PERROR (" Return Host address information error !!! "); exit (2);} s = socket AF_INET, SOCK_STREAM, 0); if (S <0) {PERROR ("Get socket number failed !!!"); exit (3);} sin.sin_family = AF_INET; SIN.SIN_PORT = HTONS (1500); / * Use port 1500 * / bcopy (hp-> h_addr, & sin.sin_addr, hp-> h_length); if (bind (s, & sin) <0) {PERROR ("cannot bundle the server address to the socket number On !!! "); colse (s); exit (4);} IF (Listen (S, 5) <0 {Perror (" sever: listen "); exit (5);} while (1) {ix ((ns = accept (s, & sin, & fromlen)) <0) {Perror ("Sever: Accept"); exit (6);} Lseek (Source, OL, 0); / * Each time you accept the client connection, you should move the source file pointer used to read to the file header. * / Write (ns, file, sizeof (file)); / * Send file name * / while ((k = read (Source, BUF, SIZEOF (BUF)))> 0) Write (NS, BUF, K); Printf ("/ n / n / t / t transferred !!! / n"); Close (ns);} close (Source); exit (0); / * Client source program khj.c * / #include #include #

转载请注明原文地址:https://www.9cbs.com/read-75863.html

New Post(0)