setting up a network server


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. int status;
  2. struct addrinfo hints;
  3. struct addrinfo *servinfo; // will point to the results
  4.  
  5. memset(&hints, 0, sizeof hints); // make sure the struct is empty
  6. hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
  7. hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
  8. hints.ai_flags = AI_PASSIVE; // fill in my IP for me
  9.  
  10. if ((status = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) {
  11. fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  12. exit(1);
  13. }
  14.  
  15. // servinfo now points to a linked list of 1 or more struct addrinfos
  16.  
  17. // ... do everything until you don't need servinfo anymore ....
  18.  
  19. freeaddrinfo(servinfo); // free the linked-list

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.