Unix network programming entry basic tutorial

  
 

Unix is ​​one of the mainstream operating systems used by computers. TCP/IP is a widely used Internet protocol. Unix provides a network process communication mechanism for TCP/IP network programming: sockets. Interface (Sockets Interface). This article will introduce the basic concepts and programming techniques of sockets in the Un ix environment, and use examples to illustrate how to implement client/server communication with sockets under Unix. 1. Socket Introduction Socket (Socket) is the basic operating unit of network communication. It provides endpoints for two-way communication between different hosts. These processes establish a Socket before communication and read/write to Socket. The operation implements the network communication function. Sockets are divided into the following three types. 1. Byte Stream Socket This is the most commonly used socket type. The TCP (Transport Control Protocol) protocol in the TCP/IP protocol suite uses this type of interface, which provides connection-oriented (establishing virtual circuits) and error-free. Network packet transmission with consistent transmission order, unlimited packet length and non-repeating. 2. Datagram Socket The UDP (User Datagram Protocol) protocol in the TCP/IP protocol suite uses such an interface. It is a connectionless service that performs network transmission with independent envelopes. The maximum length of the envelope is 32 KB. Sequence, reliability, and no repeatability are not guaranteed, and it is typically used for single message transmission or where reliability is not important. 3. The original datagram socket provides direct access to the underlying communication protocol (such as the IP protocol) of the network. It is generally not provided to ordinary users, and is mainly used to develop new protocols or to extract features that are more concealed. Second, the socket system call schedule is a simple description of the socket system call in Unix. Schedule 3, socket programming method Here we will introduce the programming method of the byte-flow socket and the non-connection protocol datagram socket for the connection protocol, because the original datagram socket is used in the actual work. Less, no discussion here. Regardless of the socket programming, the client/server collaboration mode is employed, that is, the client process issues a request to the server process, and the server process executes the requested task and returns the result to the client process. The service process of the byte stream socket and the client process must establish a connection before communicating. The steps to establish a connection and communication are shown in Figure 1. 1. The service process first calls Socket() to create a byte stream socket, and calls bind() to bind the server address to the socket, and then calls listen() to listen. The connection request, then call accept() to prepare for establishing a connection with the client process. When there is no connection request, the service process is blocked. 2. The client process calls Socket() to create a byte stream socket, and then calls connect() to The service process issues a connection request; 3. When the connection request arrives, the service process is woken up, a new byte stream socket is generated, and the new socket is used to establish a connection with the client process's socket, and the service process is first generated. The socket continues to be used to listen for service requests on the network. 4. The service process and the client process exchange data by calling read() and write(); 5. the service process and the client process undo the socket by calling close() and Interrupt the connection. Figure 1 Byte-stream socket system call for connection protocol Figure 2 The service socket process of the data connection socket of the connectionless system of the non-connection protocol. The client process does not need to establish a connection before communication. The communication steps are shown in Figure 2. 1. The service process first calls Socket() to create a data socket, and calls bind() to bind the server address to the socket, and then calls recvfrom() to wait for the request sent by the client process; 2. The client process is calling the Socket. () After creating a datagram socket, call bind() to bind the client address to the socket, then call sendto() to send the request to the service process, and then call recvfrom() to wait for the service process to return the request. Processing the result; 3. After executing the task requested by the client process, the service process calls sendto() to return the processing result to the client process. 4. The service process and the client process revoke the socket by calling close(). IV. Socket Programming Example An example of implementing a client/server mode process communication over a TCP/IP network using a byte stream socket is given below. In this example, the service process runs before the client process. When the two parties establish a connection, the service process continuously sends a continuously increasing number of sequences to the client process through the connection. The client process displays on the screen every time 50 sequences are received. An & lsquo;.', after 20 points are displayed, and the line is changed until either process is interrupted. /******server.c******/# include <sys/types.h># include <sys/socket.h># include <netinet/in.h># include < ;netdb.h># include <stdio.h>main(){int sock,namelen,seq,netint;struct sockaddr-in server;//the server's internet address char msgsock;char buf[1024];//Create the TCP protocol byte stream socket sock=socket (AF-INET, SOCK-STREAM, IPPROTO-TCP); if(sock<0){perror("socket");exit(1); }//Bundle the address of the local host (server) onto the created socket server.sinfamily=AF-INET; //internet domain sevrer.sinaddr.s-addr=INADDR-ANY; //Use any legal address sevrer .sinport=htons(1032);//Accepted service port number if(bind(sock,&server,sizeof(server))<0){perror("bind");exit(2);}//Create a listen queue of length 5, listen to the connection request from the socket if(listen(sock,5)<0){perror("listen");exit(3)}//block to the client The connection request arrives, creating a new socket for communication namelen=sizeof(server);if((msgsock=accept(sock,&server,&namelen) )<0){perror("accept");exit(4);}//At this point the connection has been established, communication seq=0; for(;;){netint=htonl(seq);//The host byte order is converted to the network byte order write(msgsock,&netint,4);//write the sequence number to the client seq++;}}/******client.c******/#include <sys/types.h># include <sys/socket.h># include <netinet/in.h># include <netdb.h># include <stdio.h>main(argc, Argv)int argc;char *argv[];{int sock,myseq,recvseq;struct sockaddr-in server;//Save server's internet address struct hostcnt *h;//Save host information //command line must be followed by parameters: The host name of the server, which must be defined in the /etc/hosts file, for example: _ 192.7.100.31 hp486if(argc!=2){printf("Usage:%s servername\ ",argv[0 ];exit(1);}//Create the byte stream socket of the internet domain TCP protocol sock=socket(AF-INET,SOCK-STREAM,IPPROTO-TCP);if(sock<0){perror(" Socket");exit(2);}//According to the server host name provided by the command line argument, get the address of the server if(!(h=gethostbyname(argv[1]))){perror(argv[1];ex It(3);}bzero(&server,sizeof(server)); //Clear the server address first 0server.sinfamily=AF-INET; //internet domain //fill the address of the host into the address of the server Bcopy(h->h-addr,&server.sinaddr,h->length);server.sinport=htons(1032); //fill in the recognized service port number and establish a connection with the service process if(connect( Sock, &server,sizeof(server))<0){perror("connect");exit(4);}//At this point, the connection has been established, which can be implemented by reading/writing the socket. Communication myseq=0; while(read(sock,&recvseq,4)==4){//read sequence number recvseq=ntohl(recvseq);//network byte order is converted to host byte order if(myseq! =recvseq){printf("sented=%d wanted=%d\ ",recvseq,myseq);myseq=recvseq;}elsemyseq++;if(!(recvseq%50))printf("."); If(!(recvseq%1000))printf("\ ");}} V. Conclusion Although Sockets was first proposed as a BSD specification, it has become a TCP/IP under Unix operating system
Network programming standards, but with the continuous advancement of network technology, the scope of application of Sockets is no longer limited to Unix operating system
and TCP/I P network. Currently, Windows
, Windows
NT, Windows
95, OS/2, Sun OS, Netware and many other operating systems
are available Socket interfaces, which are based on 4.3BSD Unix Sockets and add some extensions to their own operating system
features. These new versions of Sockets are built-in or plug-in to OS
The form is provided to the programmer. W insock (Windows
Sockets) is a version of Sockets for the Windows
series of operating systems. At the same time, the types of network protocols supported by sockets are increasing. For example, Winsock supports not only TCP/UDP protocols, but also network protocols such as IPX/SP X, AppleTalk, Decnet, and NetBEUI. Netware sockets support TCP/UDP. And IPX/SPX protocol. In addition, sockets have added non-C language support: C++, BASIC, Pascal, etc. It can be seen that the open performance of Sockets is gradually improving, and it can be said that it has become a universal interface for network programming. With this powerful tool, we can construct any distributed processing system across operating systems and cross-network protocols.

Copyright © Windows knowledge All Rights Reserved