dreamworker

Untitled

Aug 8th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. pid_t pid;
  2. int listenfd, connfd;
  3.  
  4. listenfd = Socket(...);
  5.  
  6. /* fill in sockaddr_in{} with server's well-known port */
  7. Bind(listenfd, ...);
  8.  
  9. Listen(listenfd, LISTENQ);
  10.  
  11. for (;;) {
  12.   connfd = Accept(listenfd, ...); /* probably blocks */
  13.   if ((pid = Fork()) == 0) {
  14.     Close(listenfd); /* child closes listening socket */
  15.     doit(connfd); /* process the request */
  16.     Close(connfd); /* done with this client */
  17.     exit(0); /* child terminates */
  18.   }
  19.   Close(connfd);  /* parent closes connected socket */
  20. }
Advertisement
Add Comment
Please, Sign In to add comment