Socket programming Basics

Source: Internet
Author: User
Tags file transfer protocol
For socket, I don't want to know its history here. I just want to say that it is a process communication method at the time, in short, some API functions of the network library can be called to exchange data between processes distributed across different hosts.

In socket, we need to understand the following definition concepts:

First, IP Address: I think it is easy to understand that the network address assigned to the local host according to the TCP/IP protocol needs to communicate with the two processes, if any process needs to know the location of the Communication peer and how the location is determined, the IP address of the Communication peer should be used.

Second, the port number is used to identify the local communication process and facilitate the OS to submit data. that is to say, the process specifies the network IP address of the other process, but this IP address is only used to identify the host where the process is located. How to find the process running on this host, then use the port number.

3. connection: the communication link between two processes.

4. Semi-correlation: using a triple in the network can uniquely identify a process in the world:

(Protocol, local address, local port number)

Such a triple is called a semi-correlation, which specifies each half of the connection.

5. Full correlation

A complete inter-network process communication must be composed of two processes and can only use the same high-level protocol. That is to say, it is impossible for one end of the communication to use the TCP protocol, while the other end to use the UDP protocol. Therefore, a complete inter-network communication requires a quintuple:

(Protocol, local address, local port number, remote address, and remote port number)

Such a quintuple is called association, that is, two half-correlation protocols can be combined into an appropriate correlation, or completely specify to form a connection.

Customer/Server mode
In TCP/IP network applications, the main mode of interaction between two processes is the client/server model, that is, the customer sends a service request to the server, after receiving the request, the server provides the corresponding service. The customer/Server mode is based on the following two aspects: first, the network is created because the hardware and software resources, computing power, and information in the network are not equal and need to be shared, thus, a host with many resources is created to provide services, and the customer request service with few resources is not equivalent. Second, inter-network process communication is completely asynchronous, and there is no parent-child relationship between processes that communicate with each other without sharing the memory buffer. Therefore, a mechanism is required to establish a connection between processes that want to communicate with each other, provides synchronization for the data exchange between the two, which is the TCP/IP of the Ji households/Server mode.

The customer/Server mode takes the active Request Method in the key discipline process:

First, the server must start and provide the corresponding services according to the request:

1. Open a channel and inform the local host that it is willing to receive customer requests at a recognized address (port, for example, FTP is 21;

2. Wait for the customer's request to reach the port;

3. Receive a duplicate service request, process the request, and send a response signal. To receive a concurrent service request, activate a new process to process the request (such as fork and exec in UNIX ). The new process processes this customer request and does not need to respond to other requests. After the service is completed, close the communication link between the new process and the customer and terminate the process.

4. Return step 2, waiting for another customer's request.

5. Disable the server

Customer:

1. Open a channel and connect to the specific port of the host where the server is located;

2. Send a service request message to the server, wait for and receive the response; Continue to make the request ......

3. After the request ends, the communication channel is closed and terminated.

We can see from the process described above:

1. The role of the client and server processes is non-symmetric, so the encoding is different.

2. Service processes are generally initiated upon requests from Ji households. As long as the system is running, the service process persists until it is normal or forced to terminate.

After introducing the basic knowledge, the following describes some API functions:

Create socket -- socket ()
Before using a socket, an application must first have a socket. The system calls socket () to provide the application with the means to create a socket. The call format is as follows:

Socket Pascal far socket (int af, int type, int Protocol );

This call receives three parameters: AF, type, and protocol. The AF parameter specifies the region where communication occurs, such as af_unix, af_inet, and af_ns. in DOS and Windows, only af_inet is supported, which is the region of the Internet. Therefore, the address family is the same as the protocol family. The type parameter describes the type of the socket to be created. There are three types of data: TCP streaming socket (sock_stream) provides a connection-oriented and reliable data transmission service, which allows you to send data in an error-free and repeat-free manner and receive the data in the sending order. Internal traffic control to avoid data flow exceeding the limit. Data is considered as a byte stream with no length limit. The file transfer protocol (FTP) uses a streaming socket.
Second, the datagram socket (sock_dgram) provides a connectionless service. Data packets are sent in the form of independent packets. No error guarantee is provided. data may be lost or duplicated, and the receiving order is disordered. Network File System (NFS) uses a datagram socket. Third, the original socket (sock_raw) interface allows direct access to lower-layer protocols, such as IP and ICMP. It is often used to test new protocol implementations or access new devices configured in existing services. the Protocol Parameter indicates the specific protocol used by the socket. If the caller does not want to specify the protocol, the value is set to 0 and the default connection mode is used. Create a socket based on the three parameters, allocate the corresponding resources to it, and return an integer socket number. Therefore, the socket () System Call actually specifies the "protocol" dollar in the related quintuple.
Specify the local address -- BIND ()
After a socket is created with socket (), a namespace (address family) exists, but it is not named. BIND () associates the socket address (including the local host address and local port address) with the created socket number, that is, assign the name to the socket to specify the local semi-correlation. The call format is as follows:

Int Pascal far BIND (socket S, const struct sockaddr far * Name, int namelen );

The parameter S is the socket Descriptor (socket number) returned by the socket () call and not connected ). The parameter name is the local address (name) assigned to socket S. Its length is variable and the structure varies with the communication domain. Namelen indicates the name length. If no error occurs, BIND () returns 0. Otherwise, socket_error is returned.
Establish a socket connection -- connect () and accept ()
These two system calls are used to complete a complete related establishment, and connect () is used to establish a connection. A connectionless socket process can also call connect (), but there is no actual Packet Exchange Between processes. The call will be directly returned from the local operating system. The advantage of this is that the programmer does not need to specify the destination address for each data, and if a datagram is received, the destination port does not establish a "connection" with any socket ", this allows you to determine whether the client depends on discipline and discipline. And accept () is used to make the server wait for the actual connection from a customer process.

The call format of Connect () is as follows:

Int Pascal far connect (socket S, const struct sockaddr far * Name, int namelen );

The parameter S is the local socket descriptor to establish a connection. The parameter name indicates the pointer indicating the structure of the socket address of the other party. The length of the socket address of the other party is described by namelen.

If no error occurs, connect () returns 0. Otherwise, socket_error is returned. In the connection-oriented protocol, this call causes the actual connection between the local system and the external system.

Because the address family is always included in the first two bytes of the socket address structure, and is related to a protocol family through socket () calls. Therefore, the BIND () and connect () protocols are not required as parameters.

The call format of accept () is as follows:

Socket Pascal far accept (socket S, struct sockaddr far * ADDR, int far * addrlen );

The parameter S is the local socket descriptor. You must call listen () before using the parameter called by accept (). The ADDR pointer to the client's socket address structure to receive the address of the connected object. The exact ADDR format is determined by the address family created at the socket creation. Addrlen is the length (in bytes) of the client socket address ). If no error occurs, accept () returns a socket type value, indicating the descriptor of the received socket. Otherwise, the returned value is invalid_socket.

Accept () is used for connecting servers. The ADDR and addrlen parameters store the customer's address information. Before the call, the ADDR parameter points to an address structure with an empty initial value, while the initial value of addrlen is 0. After the accept () is called, the server waits for the customer connection request to be received from the socket number S, and the connection request is sent by the customer's connect () call. When a connection request arrives, the accept () call puts the first client socket address and length in the request connection queue into ADDR and addrlen, create a new socket number with the same features as S. The new socket can be used to process concurrent requests from the server.

Four socket system calls, socket (), BIND (), connect (), accept (), can complete a fully quintuple-related establishment. Socket () specifies the Protocol element in the quintuple. Its usage has nothing to do with whether it is a customer or server or connection-oriented. BIND () specifies the Local Binary In the quintuple, that is, the address and port number of the local host. Its usage is related to whether connection is oriented, BIND () must be called. If connection orientation is adopted, BIND () can be called instead of Connect. If no connection is used, the customer must use bind () to obtain a unique address.
Listener ── listen ()
This call is intended for connection servers, indicating that it is willing to receive connections. Listen () must be called before accept (). The call format is as follows:

Int Pascal far listen (socket S, int backlog );

The parameter S identifies a locally established, unconnected socket number, and the server is willing to receive requests from it. Backlog indicates the maximum length of the Request connection queue. It is used to limit the number of requests in the queue. Currently, the maximum value is 5. If no error occurs, listen () returns 0. Otherwise, it returns socket_error.

During the execution of the call, listen () can complete the required connection for the socket s without calling BIND () and establish a request connection queue with the length of backlog.

Calling listen () is the third step in the four steps in which the server receives a connection request. It allocates a stream socket when socket () is called, and calls bind () after giving s a name, and must be called before accept.
Data transmission-send () and Recv ()
After a connection is established, data can be transmitted. Common system calls include send () and Recv ().

The send () call is used to send output data on the connected datagram or stream socket specified by the key discipline S. The format is as follows:

Int Pascal far send (socket S, const char far * Buf, int Len, int flags );

The parameter S is the descriptor of the connected local socket. The Buf pointer to a buffer with sent data. Its length is specified by Len. Flags specifies the transmission control mode, such as whether to send out-of-band data. If no error occurs, send () returns the total number of bytes sent. Otherwise, it returns socket_error.
The Recv () call is used to receive input data on the connected datagram or stream socket specified by S. The format is as follows:

Int Pascal far Recv (socket S, char far * Buf, int Len, int flags );

The parameter S is the connected socket descriptor. The Buf pointer to the buffer for receiving input data. Its length is specified by Len. Flags specifies the transmission control mode, such as whether to receive out-of-band data. If no error occurs, Recv () returns the total number of bytes received. If the connection is closed, 0 is returned. Otherwise, it returns socket_error.
Multiplexing of input/output -- select ()
The Select () call is used to detect the status of one or more sockets. For each socket, this call can request read, write, or error information. The socket set in the given Request status is indicated by an fd_set structure. This structure is updated to reflect the subset of sockets that meet specific conditions. At the same time, the Select () call returns the number of sockets that meet the conditions. The call format is as follows:

Int Pascal far select (INT NFDs, fd_set far * readfds, fd_set far * writefds, fd_set far * limit TFDs, const struct timeval far * timeout );

The NFDs parameter indicates the value range of the socket descriptor to be checked. This variable is generally ignored.

The readfds parameter points to the pointer to the socket descriptor set for read detection. The caller wants to read data from it. The writefds parameter points to the pointer to the socket descriptor set for write detection. Exceptfds indicates the pointer to the socket descriptor set to detect for errors. Timeout points to the maximum waiting time of the select () function. If it is set to null, the operation is blocked. Select () returns the total number of socket descriptors that have been prepared in the fd_set structure, or returns socket_error if an error occurs.
Close socket-closesocket ()
Closesocket () closes socket S and releases the resources allocated to the socket. If s involves an open TCP connection, the connection is released. Closesocket () is called in the following format:

Bool Pascal far closesocket (socket S );

Socket descriptor to be disabled by parameter S. If no error occurs, closesocket () returns 0. Otherwise, socket_error is returned.

The above are some common API functions of socket APIs. Below I will introduce the C/S mode as the client/server communication mode. The server starts the service and listens on the corresponding port, and the client opens the connection, after the establishment of the communication link, both parties interact with each other, and then close the socket.

Now, we will introduce socket basics here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.