Socket function Prototype:
#include <sys/socket.h>int socket (int family, int type, int protocol);//Successful execution returns a non-negative socket descriptor; Error 1
The socket function is used to open an (active) socket. In this function you need to specify the protocol family (family) used, the socket type (type), the protocol used (protocol).
General values for the protocol family:
| Family |
Description of the Protocol family |
| Af_inet |
IPV4 protocol |
| Af_inet6 |
IPV6 protocol |
| Af_local |
UNIX Domain protocol |
| Af_route |
Routing Sockets Protocol |
| Af_key |
Key sockets |
Socket Type:
| Type |
Description |
| Sock_stream |
BYTE stream sockets |
| Sock_dgram |
Datagram sockets |
| Sock_seqpacket |
Ordered group sockets |
| Sock_ram |
Raw sockets |
Agreement:
| Protocol |
Description |
| Ipproto_cp |
TCP Transport Protocol |
| Ipproto_udp |
UDP Transport Protocol |
| Ipproto_sctp |
SCTP Transfer Protocol |
Combination of family and type parameters in the socket function
|
Af_inet |
Af_inet6 |
Af_local |
Af_route |
Af_key |
| Sock_stream |
tcp| Sctp |
tcp| Sctp |
Is |
|
|
| Sock_dgram |
Udp |
UPD |
Is |
|
|
| Sock_seqpacket |
Sctp |
Sctp |
Is |
|
|
| Sock_ram |
IPv4 |
IPv6 |
|
Is |
Is |
Connect function
The TCP client establishes a connection to the TCP server through the Connect function.
#include <sys/socket.h>int connect (int sockfd, const struct SOCKADDR *servaddr, socklen_t addrlen);//Success returns 0, Failure is returned-1
Parameter description:
sockfd--the active socket Pathbreaker by the socket.
servaddr--pointer to the socket address structure.
socklen--the size of the socket structure
The function is to establish a connection to the server side, so be sure to include the IP address and port number of the server in the socket. If you use a TCP socket (type select sock_stream), the function fires the three handshake of the TCP connection and, when called, blocks the process until the function succeeds or fails to continue down. When the function error returns, there are several possible scenarios:
The 1.TCP client did not receive the corresponding etimedout from the server-side of the SYN section and returned an error.
The response of the 2.TCP server to the SYN section of the TCP client is RST (for reset), indicating that the server host does not have a process waiting to connect to it on the port we specify (either the maximum thread is reached or the server process is not running). A econnrefused error is returned immediately after the client receives the RST.
3. If the client sends a SYN sub-section that throws a "Destination unreachable" (unreachable) ICMP error on the brokered route, the client will continue to send the SYN section until the specified time is exceeded.
Ps://rst is one of the TCP header 6 large flag bits that represents a reset connection. As for what is the reset connection, then give an explanation.
Bind function
The BIND function assigns a local protocol address to a socket. For Internet protocols, the protocol address is a combination of the IP address (ipv4/ipv6) and the port number (TCP/UDP).
#include <sys/socket.h>int bind (int sockfd, const sockaddr *myaddr, socklen_t addlen);//Execution returns 0 successfully, error 1.
SOCKFD: Socket descriptor opened via socket
MYADDR: Pointer to the Protocol-specific socket address structure
Addlen: The length of the address structure.
Once the function has been successfully executed, the socket contains one of the above protocol addresses.
Listen function
The listen function turns an disconnected socket into a passive socket, indicating that the kernel should accept connection requests to that socket.
#include <sys/socket.h>int Listen (int sockfd, int backlog);//execution successfully returns 0, failure returns-1;
SOCKFD: Represents an active socket; The backlog indicates the maximum number of connections
ps://the maximum number of connections includes both completed and incomplete connections. Some detailed discussion of this function is explained in detail in the UNIX network programming: Volume I, which is not described here.
Accept function
The Accept function is used to get the next completed connection from the team header that has completed the connection queue, and if the queue is empty, the function goes into sleep mode (blocking state).
#include <sys/socket.h>int Accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);//execution successfully returns a non-negative socket descriptor, Failed to return-1;
The special thing about this function is that the third argument is a pointer, which means that the value of the parameter can be changed in the function. This parameter is called the "value-result" parameter. Before the function executes, the parameter refers to the length of the socket address structure, and after execution, the value of the formal parameter represents the actual number of bytes stored in the socket address structure by the kernel.
Here's another concept that needs to be differentiated: listening sockets and connected sockets. The socket after execution of the listen is called a listening socket and is used as the first parameter of the function, and the socket after the accept function is called a connected socket. A listener socket is used by a server process and persists throughout the life of the server; The connected sockets are created by the kernel for the server, and the corresponding connected sockets are closed once the service is complete.
Close function
The close function is used to close a socket and the socket after it is closed will no longer be used.
#include <sys/socket.h>int close (int sockfd);//Successful return 0, failure return-1
This article is from the "Execute" blog, so be sure to keep this source http://executer.blog.51cto.com/10404661/1775267
Basic TCP Socket Programming