Socket-first exploration
tags: - The-Ccategories: - Codingdate:# created: 2025-02-26# updated: 2025-02-26—
Socket-first exploration
Section titled “Socket-first exploration”1. Socket() function### (1). domain protocol family: 1. AF_INET 2. AF_INET6 3. AF_LOCAL(AF_UNIX) 4. AF_ROUTE### (2). Type socket type 1. SOCK_STREAM 2. SOCK_DGRAM 3. SOCK_RAW 4. SOCK_PACKET 5. SOCK_SEQPACKET### (3). Protocol 1. IPPROTO_TCP 2. IPPROTO_UDP 3. IPPROTO_STCP 4. IPPROTO_TIPC## 2. Bind() function```
Section titled “1. Socket() function### (1). domain protocol family: 1. AF_INET 2. AF_INET6 3. AF_LOCAL(AF_UNIX) 4. AF_ROUTE### (2). Type socket type 1. SOCK_STREAM 2. SOCK_DGRAM 3. SOCK_RAW 4. SOCK_PACKET 5. SOCK_SEQPACKET### (3). Protocol 1. IPPROTO_TCP 2. IPPROTO_UDP 3. IPPROTO_STCP 4. IPPROTO_TIPC## 2. Bind() function```”Int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)- Sockfd: socket descriptor, uniquely identifies a socket- addrlen: address length- addr: an address pointer pointing to the protocol address to be bound to sockfdFor example, ipv4 corresponds to:``` struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; }; struct in_addr { uint32_t s_addr; };Corresponding to ipv6 is:```
struct sockaddr_in6 {
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
uint32_t sin6_scope_id;
};
struct in6_addr {unsigned char s6_addr[16];};The Unix domain corresponds to:``` #define UNIX_PATH_MAX 108
struct sockaddr_un { sa_family_t sun_family; char sun_path[UNIX_PATH_MAX]; };3. Listen() connect() function
Section titled “3. Listen() connect() function”Int listen(int sockfd, int backlog)Int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
- The listen function changes the active type created by the socket into a passive type and waits for the client to connect.
4. accept function```
Section titled “4. accept function```”Int socket(int domain, int type, int protocol)
## 5. setsockopt function```Int setsockopt(SOCKET s, int level, int optname, const char FAR *optval, int optlen)- s:socket descriptor- Level: option level, SOL_SOCKET/IPPROTO_TCP- Optname: Options that need to be set- optval: just want to store the pointer of the option- Optlen: buffer length
[2]. OSI model