Follow our Telegram channel to get notified instantly whenever new books are published.
Beejs Guide To Network Programming – Brian Hall

Slightly Advanced Techniques int get_listener_socket(void) { struct addrinfo hints, *ai, *p; int yes=1; // for setsockopt() SO_REUSEADDR, below int rv; int listener; // get us a socket and bind it memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) { fprintf(stderr, “selectserver: %s\n”, gai_strerror(rv)); exit(1); } for(p = ai; p != NULL; p = p->ai_next) { listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (listener < 0) { continue; } // lose the pesky "address already in use" error message setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) { close(listener); continue; } break; } // if we got here, it means we didn't get bound if (p == NULL) { fprintf(stderr, "selectserver: failed to bind\n"); exit(2); } freeaddrinfo(ai); // all done with this // listen if (listen(listener, 10) == -1) { perror("listen"); exit(3); } return listener; } Chapter 7.
Slightly Advanced Techniques /* * Add new incoming connections to the proper sets */ void handle_new_connection(int listener, fd_set *master, int *fdmax) { socklen_t addrlen; int newfd; // newly accept()ed socket descriptor struct sockaddr_storage remoteaddr; // client address char remoteIP[INET6_ADDRSTRLEN]; addrlen = sizeof remoteaddr; newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen); if (newfd == -1) { perror(“accept”); } else { FD_SET(newfd, master); // add to master set if (newfd > *fdmax) { // keep track of the max *fdmax = newfd; } printf(“selectserver: new connection from %s on ” “socket %d\n”, inet_ntop2(&remoteaddr, remoteIP, sizeof remoteIP), newfd); } } /* * Broadcast a message to all clients */ void broadcast(char *buf, int nbytes, int listener, int s, fd_set *master, int fdmax) { for(int j = 0; j <= fdmax; j++) { // send to everyone!
if (FD_ISSET(j, master)) { // except the listener and ourselves if (j != listener && j != s) { if (send(j, buf, nbytes, 0) == -1) { perror(“send”); } } } } } /* * Handle client data and hangups */ void handle_client_data(int s, int listener, fd_set *master, int fdmax) Chapter 7.
Book Information
- Unique ID: 12a121295aa1da3b
- File Extension: .pdf
- File Size: 825,753 bytes (0.787 MB)
- Title: –
- Author: Unknown
- ISBN: 0000000000
- Pages: 133
- Language: English (en)
Reading & Word Statistics
- Estimated Reading Time: 228.26 minutes
- Total Words: 45,653
- Total Characters: 260,953
- Average Words per Page: 343.26
- Average Characters per Page: 1962.05
Most Frequent Words
int (378), socket (362), struct (349), addr (291), inet (237), data (208), ipv (199), sockaddr (198), address (194), set (191), include (189), buf (174), use (168), hints (147), send (138), return (133), port (130), get (129), network (129), chapter (126), function (124), char (122), call (121), one (116), long (115), sockets (106), sockfd (105), want (104), unsigned (104), see (102), like (101), family (98), recv (97), it’s (97), getaddrinfo (95), host (93), error (92), https (89), sin (88), number (87), sizeof (86), descriptor (85), server (84), you’re (84), res (84), connect (81), client (81), stream (80), bind (78), listener (77), man (76), protocol (75), packet (75), system (73), size (73), byte (72), bytes (71), don’t (70), addresses (69), also (69), connection (69), sock (69), example (68), sys (68), select (66), errno (66), bit (64), returns (64), null (64), file (61), read (61), name (61), guide (60), rfc (60), printf (60), first (59), information (58), need (58), accept (57), org (57), string (57), pages (56), len (56), case (54), order (52), code (52), type (52), perror (51), well (51), value (51), now (49), types (49), you’ll (49), that’s (49), time (48), new (48), ready (48), void (48), using (47), close (47).
