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()