~/ learn/ comp-325/ cards/ Six fields, filled in by four calls
1 of 10

Create a TCP socket and bind it to port 9000 on every interface

Create a TCP socket and bind it to port 9000 on every interface

Answer

int s = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in me; memset(&me, 0, sizeof me); me.sin_family = AF_INET; me.sin_addr.s_addr = htonl(INADDR_ANY); me.sin_port = htons(9000); if (bind(s, (struct sockaddr *)&me, sizeof me) < 0) perror("bind");

Zero the address structure first — the unused `sin_zero` bytes must be clear for portability. `htonl`/`htons` convert to network byte order. After `bind()` succeeds the socket is half associated: it has a local end and no remote one.

S&K 3e ch20 §20.3.1, §20.6; call signatures from POSIX.1-2024 socket(), bind(), listen(), accept(), connect()

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/318c561c-5caf-4e3f-be99-dbe7dd3e6c12/flashcard utf-8 LF