Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #define MYPORT β€œ3490” // the port users will be connecting to
  6. #define BACKLOG 10 // how many pending connections queue will hold
  7. int main(void) {
  8. struct sockaddr_storage their_addr;
  9. socklen_t addr_size;
  10. struct addrinfo hints, *res;
  11. int sockfd, new_fd;
  12. // !! don’t forget your error checking for these calls!!!
  13. // first load up address structs with getaddrinfo()
  14. memset(&hints, 0, sizeof hints);
  15. hints.ai_family = AF_UNSPEC;
  16. hints.ai_socktype = SOCK_STREAM;
  17. hints.ai_flags = AI_PASSIVE;
  18. gettaddrinfo(NULL, MYPORT, &hints, &res);
  19. // make a socket, bind it, and listen on it:
  20. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  21. bind(sockfd, res->ai_addr, res->ai_addrlen);
  22. listen(sockfd, BACKLOG);
  23. // now accept an incoming connection:
  24. addr_size = sizeof their_addr;
  25. new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
  26. // ready to communicate on socket descriptor new_fd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement