Created
November 10, 2024 10:07
-
-
Save 7etsuo/3d3c243297f850b1f12c03931c665dec to your computer and use it in GitHub Desktop.
socket cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. . | |
|\/\/| | |
|____| | |
.-----------. .-----------. .------. .-----. | |
/ \.-------./ \.-------*-. | | |----------. | |
| | .--. \ | / '-' | \ | |
'---. .---' '--' |-. .-:-' /____/_ | .-. | | |
| | .---.___-' | | '.____ \ | | | | | |
| | '-___-''\ | | / | / '-' | | |
| |\ \ | |/ |-_______-' / | |
'-_____-' '--_______--' '-_____-'-.__________.' '-__________.' | |
2024 | |
▗▖ ▗▄▄▄▖▗▖ ▗▖▗▖ ▗▖▗▖ ▗▖ ▗▄▄▖ ▗▄▖ ▗▄▄▖▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖ | |
▐▌ █ ▐▛▚▖▐▌▐▌ ▐▌ ▝▚▞▘ ▐▌ ▐▌ ▐▌▐▌ ▐▌▗▞▘▐▌ █ | |
▐▌ █ ▐▌ ▝▜▌▐▌ ▐▌ ▐▌ ▝▀▚▖▐▌ ▐▌▐▌ ▐▛▚▖ ▐▛▀▀▘ █ | |
▐▙▄▄▖▗▄█▄▖▐▌ ▐▌▝▚▄▞▘▗▞▘▝▚▖ ▗▄▄▞▘▝▚▄▞▘▝▚▄▄▖▐▌ ▐▌▐▙▄▄▖ █ | |
▗▄▄▖▗▖ ▗▖▗▄▄▄▖ ▗▄▖▗▄▄▄▖▗▄▄▖▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖▗▄▄▄▖ | |
▐▌ ▐▌ ▐▌▐▌ ▐▌ ▐▌ █ ▐▌ ▐▌ ▐▌▐▌ ▐▌ █ | |
▐▌ ▐▛▀▜▌▐▛▀▀▘▐▛▀▜▌ █ ▝▀▚▖▐▛▀▜▌▐▛▀▀▘▐▛▀▀▘ █ | |
▝▚▄▄▖▐▌ ▐▌▐▙▄▄▖▐▌ ▐▌ █ ▗▄▄▞▘▐▌ ▐▌▐▙▄▄▖▐▙▄▄▖ █ | |
1. Interaction | |
2. Port and Service Functions | |
TCP Server getservbyname(char *name, char *proto) // Fetch port by service name and protocol | |
┌───────────────────┐ getservbyport(int port, char *proto) // Fetch service name by port and protocol | |
│ socket() │ | |
└─────────┬─────────┘ 3. Byte Ordering Functions | |
│ htons (unsigned short hostshort) // Convert 16-bit host to network byte order | |
┌─────────▼─────────┐ htonl (unsigned long hostlong) // Convert 32-bit host to network byte order | |
known port │ bind() │ ntohs (unsigned short netshort) // Convert 16-bit network to host byte order | |
└─────────┬─────────┘ ntohl (unsigned long netlong) // Convert 32-bit network to host byte order | |
│ | |
┌─────────▼─────────┐ 4. IP Address Functions | |
│ listen() │ inet_aton (const char *strptr, s ruct in_addr *addrptr) // Convert string to network address | |
└─────────┬─────────┘ inet_addr (const char *strptr) // Convert string to IPv4 address (32-bit network order) | |
│ inet_ntoa (struct in_addr inaddr) // Convert IPv4 address to string | |
┌─────────▼─────────┐ | |
│ accept() │ 5. Socket Core Functions | |
└─────────┬─────────┘ socket (int family, int type, int protocol) // Get socket descriptor | |
│ connect (int sockfd, struct sockaddr *serv_addr, int addrlen) // Connect to server | |
TCP Client ▼ bind (int sockfd, struct sockaddr *my_addr, int addrlen) // Bind socket to local address | |
┌───────────────────┐ blocks until connection listen (int sockfd, int backlog) // Listen for incoming connections | |
│ socket() │ from client accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) // Accept connection | |
└─────────┬─────────┘ │ send (int sockfd, const void *msg, int len, int flags) // Send data | |
│ │ recv (int sockfd, void *buf, int len, unsigned int flags) // Receive data | |
┌─────────▼─────────┐ connection │ sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen) // Send to UNCONNECTED socket | |
│ connect() ◄─────────────────────► recvfrom (int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen) // Receive from UNCONNECTED socket | |
└─────────┬─────────┘(TCP 3-way handshake)│ close (int sockfd) // Close socket | |
│ │ shutdown (int sockfd, int how) // Gracefully close socket | |
┌─────────▼─────────┐ │ select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) // Monitor multiple sockets | |
┌─────► write() ┼────┐ ┌─────────▼─────────┐ | |
│ └─────────┬─────────┘ └──────► read() │ 6. Socket Helper Functions | |
│ │ └─────────┬─────────┘ write (int fildes, const void *buf, int nbyte) // Write to file descriptor | |
│ │ │ read (int fildes, const void *buf, int nbyte) // Read from file descriptor | |
│ │ ▼ fork (void) // Create new process | |
│ │ process request bzero (void *s, int nbyte) // Set memory to 0 | |
│ │ │ bcmp (const void *s1, const void *s2, int nbyte) // Compare byte strings | |
│ │ │ bcopy (const void *s1, void *s2, int nbyte) // Copy byte strings | |
│ │ ┌─────────▼─────────┐ memset (void *s, int c, int nbyte) // Set memory to specific value | |
│ │ ┌──────┼ write() │ | |
│ ┌──────────▼────────┐ data(reply)└─────────┬─────────┘ 7. Linux Socket Structures | |
└────│ read() ◄─────┘ │ sockaddr | |
└──────────┬────────┘ │ struct sockaddr { unsigned short sa_family; char sa_data[14]; } // Generic socket address | |
│ ┌─────────▼─────────┐ sockaddr_in | |
│ │ read() │ struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; } // IPv4 address | |
│ └─────────┬─────────┘ in_addr | |
┌──────────▼────────┐ │ struct in_addr { unsigned long s_addr; } // 32-bit IPv4 address | |
│ close() ├────┐ │ hostent | |
└───────────────────┘ EOF ┌─────────▼─────────┐ struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } // Host information | |
└───────► close() │ servent | |
└───────────────────┘ struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; } // Service and port information |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment