The properties of a socket are determined by three properties: domain, type, protocol.
1. Domain
Af_unix UNIX Domain protocol (file system sockets)
Af_inet arpa Internet Protocol (Unix network sockets)
... Omitted
2. Type
1) Flow socket: Sock_stream
2) Datagram Socket: Sock_dgram
3. Agreement
It is generally determined by the socket type and socket field, and usually does not need to be selected. Setting this parameter to 0 means that the default protocol is used.
Socket address.
1) Af_unix
struct Sockaddr_un {
sa_family_t sun_family; /* Af_unix */
Char sun_path[]; /* Pathname */
};
2) Af_inet
struct SOCKADDR_IN {
short int sin_family; /* Af_inet */
unsigned short int sin_port; /* Port number */
struct IN_ADDR sin_addr; /* Internet Address */
}; The structure is defined in the header file Netinet/in.h
The IP address structure in_addr is defined as:
struct IN_ADDR {
unsigned long int s_addr;
};
1. Naming sockets
#include <sys/socket.h>
int bind (int socket, const struct SOCKADDR *address, size_t Address_len);
2. Create a socket queue
#include <sys/socket.h>
int listen (int socket, int backlog);
3. Accept the connection
#include <sys/socket.h>
int accept (int socket, struct sockaddr *address, size_t *address_len);
4. Request a connection
#include <sys/socket.h>
int connect (int socket, const struct SOCKADDR *address, size_t Address_len);
5. Close the socket
Close ();
Linux Sockets (1)