Socket programming mode: 1. Define parameters, sockaddr_in, sockaddr is the structure of the description Socket information provided, requiring users to assign initial values. SockAddr_in and sockadd are the type of Socket information, but the SOCKADDR_IN structure is more convenient, sin_zero Fill 0 to maintain the same size as Struct SockAddr to transition to each other to use Bzero () or MEMSET () to settle all zero.) The above structure is as SOCKADDR_IN, and SockAddr has been defined in the system header file Sys / socket.h. 2 Assign a value for the above structural instance 3. The specific process:
You need to use the function introduction: INET_ADDR ("132.241.5.10"): Convert the IP address into an unsigned long integer.
Bzero (& (My_ADDR.SIN_ZERO) or MEMSET (& (MY_ADDR.SIN_ZERO): Seats SIN_ZERO in Struct SockAddr_IN.
Htons (): "Host to NetWork Short" transitions this unit byte order into network byte order.
FCNTL (SOCKFD, F_SETFL, O_NONBLOCK), setting blocking.
GethostByname ("www.google.com") gets address information from the remote host.
Select (int Numfds, fd_set * readfds, fd_set * writefds, fd_set * eXcePTFDS, Struct TimeVal * Timeout): Multi-channel synchronization function call, parameter int numfds Maximum number of Sockets running, fd_set * readfds Read synchronous with storage multiplexing Socket Descriptor Collection Pointer. Similar FD_SET * WRITEFDS is a pointer to the Socket Descriptor Collection to the Storage Multi-Synchronous Synchronous. You must call the macro fd_set (int FD, fd_set * set) descriptor before calling Select (). .
Connected server: You must get the native IP, this machine socket descriptor, this unit port number .a.socket () Established a socket name protocol, type (data stream or duplicate). Return Value: int (Socket Description符) B.BIND () Bind native port number, native IP address. Return value: intc.listen () Listening to the client access, set the connection waiting upper limit. Return value: intd.accept () agreed to accept The client's access, and stores the Struct SockAddr_in type structure of the accessible client (including the port number to which the client is connected, the client's IP address, etc.). This function returns another Socket descriptor, name new_fd = accept ( For the new Socket descriptor. Used for send () RECV () transfer data, the original Socket descriptor is still used to listen to the client's connection. Return Value: int (New Socket Descriptor) E.Recv () in * BUF is a buffer pointer to read information, that is, the tentative area of the other party, uses the new socket descriptor .f.send () where * msg is a buffer to send information, use the new socket description The returning value mainly plays a function of judging the error.
Connecting the client: There must be a server IP or URL, the port number of the server connection, the machine socket descriptor .a.socket () establishes a socket name to use the protocol, type. Return value: intb.bind () can be available No .c.gethostByname () Get the server's IP address (this function is a domain name resolution function, the memory address of the input server can return to the server information). Return Value: Hostent type structure pointer. This structure instance stores the host name of the returned server, the IP address (in the h_addr field) .d.connect () is connected to the server, the connection specifies the socket descriptor of the unit, will The address structure and length of the connected server. Return Value: int.E.send () F.Recv () g.close () review: 1. Socket is the connection point of the two hosts, one or more hosts Socket is used to connect to transfer data (create itself). The user can select the Socket connection point as needed. Any connection is built on the pipe established in the socket. So the function listen () accept () connect () send () Recv () Wait to bind the socket descriptor. Socket descriptor can be seen as the ID of the socket of each machine, which is unique on the machine, so it can be used to indicate the socket.2. Two machines use socket Need a full correlation (protocol, local address, local port number, remote address, remote port number) when connecting to transfer. The protocol is specified by the user when creating the socket, the local address and port number can be filled and assigned by the service. Or call bind () self-fill and assign by the user. The remote address and the remote port number are also created by remote hosts, transmitted or acquired by connect (), Accept (), gethostByname ().
Socket (), accept () function is a blocking function, that is, if the data is not accepted, it hinders the execution of the entire program unless an exception occurs. Using the FCNTL () function can set the socket () to non-blocking Int newsocket; newsocket = Socket (PF_INET, SOCK_STREAM, 0); FCNTL (NewSocket, F_SETEL, O_NONBLOCK), will not block only functions such as binding newSocket's accept (). But querying the socket connection information, a lot of resources are ware, such as binding Blocked Socket's non-blocking accept () function, the server will not stop the accept () when using RECV (), and the system will also call accept (), thereby wasting a large number of CPU resources. If ACCEPT () setting For blocking, when the RECV () is queried, the recv () is also suspended until accept () is connected to the information of the client connect ().