pure C socket

this is a review from GNU C lib. socket is a two-way communication channel, both read and write can be performed at either end.

sys/socket.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int socket(AF_INET, SOCK_STREAM, protocol);
/* return fd for this new socket, or -1 as error */
int close(int socket_fd);
/* return 0 when close successfuly, else return -1 */
int connect(int socket, struct sockaddr *addr, socketlen_t len);
/* initiate a connection from (client) socket to (server) address; by default, the connection is blocking untill server respond */
int listen(int socket, int n);
/* n specifies the length of queue for pending connections; when the queue fills up, new clients attempting to connect will fail */
int accept(int socket, struct sockaddr *addr, socketlen_t *len_ptr);
/* if successfully, accept return a new socket fd, the original (server) socket remains open and unconnected; the address and len_ptr are used to return information about the name of client socket that initiated this connection */
size_t send(int socket, const void *buffer, size_t size, int flags);
/* param socket is the fd of current sending socket. no receiver socket explicitly defined, since connection-oriented(TCP)protocol will connect first prior to send/receive */
size_t recv(int socket, const void *buffer, size_t size, int flags);
/* param socket is the fd of current receiving socket */
```
## network socket
I didn't realize GNU C socket I/O is so related to network socket. the missing part when reading muduo, libuv is here.
The server listens the connection requests on the special server socket, then accept each incoming connection. select() blocks/sleeps the program until input is available/ready on the special server socket/fd.
```c
void FD_ZERO(fd_set *set)
/* initialized the file descriptor set to empty set */
void FD_SET(int fd, fd_set *set) /* add fd to set */
void FD_CLR(int fd, fd_set *set) /*remove fd from set */
void FD_ISSET(int fd, const fd_set * set)
/* return true(non-zero) if fd is in set; else return 0 */
STDIN_FILENO ; /* file descriptor for std input “1” */

how to debug server/client code? chatRoom