Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define MYPORT “3490” // the port users will be connecting to
- #define BACKLOG 10 // how many pending connections queue will hold
- int main(void) {
- struct sockaddr_storage their_addr;
- socklen_t addr_size;
- struct addrinfo hints, *res;
- int sockfd, new_fd;
- // !! don’t forget your error checking for these calls!!!
- // first load up address structs with getaddrinfo()
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_PASSIVE;
- gettaddrinfo(NULL, MYPORT, &hints, &res);
- // make a socket, bind it, and listen on it:
- sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- bind(sockfd, res->ai_addr, res->ai_addrlen);
- listen(sockfd, BACKLOG);
- // now accept an incoming connection:
- addr_size = sizeof their_addr;
- new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
- // ready to communicate on socket descriptor new_fd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement