Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

123

2,461 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

123

  1. 1. Berkeley Socket Network Programming
  2. 2. TCP Socket : Server <ul><li>Server </li></ul>socket() bind() listen() accept()
  3. 3. TCP Socket : Server <ul><li>socket() </li></ul><ul><li>bind() </li></ul>listenfd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET: IPv4, AF_INET6: IPv6 // SOCK_STREAM: TCP, SOCK_DGRAM: UDP bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(23); // htonl: host to network (long) // htons: host to network (short) bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  4. 4. TCP Socket : Server <ul><li>listen() </li></ul><ul><li>accept() </li></ul>listen(listenfd, LISTENQ); // LISTENQ: the maximum length the queue of pending // connections connfd = accept(listenfd, (struct sockaddr *) NULL, NULL); // or cli_len = sizeof(cli_addr); connfd = accept(listenfd, (struct sockaddr *) &cli_addr, cli_len);
  5. 5. TCP Socket : Client <ul><li>Client </li></ul>socket() connect()
  6. 6. TCP Socket : Client <ul><li>socket() </li></ul><ul><li>connect() </li></ul>sockfd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET: IPv4, AF_INET6: IPv6 // SOCK_STREAM: TCP, SOCK_DGRAM: UDP char *server_ip = “140.113.210.143”; bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(server_ip); serv_addr.sin_port = htons(23); // inet_addr: ascii dotted-decimal addr to network addr // htons: host to network (short) connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  7. 7. TCP Socket : functions <ul><li>convert values between host and network </li></ul><ul><li>Internet address manipulation routines </li></ul>#include <netinet/in.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> in_addr_t inet_addr(const char *cp); char * inet_ntoa(struct in_addr in); int inet_pton(int af, const char * restrict src, void * restrict dst);
  8. 8. TCP Socket : functions <ul><li>inet_ntoa() </li></ul><ul><li>inet_pton() </li></ul>inet_ntoa(cli_addr.sin_addr); // return ascii ip address of client char *server_ip = “140.113.210.143”; inet_pton(AF_INET, server_ip, &serv_addr.sin_addr); // return 1: address valid // return 0: address not parseable // return -1: error occurred // inet_pton is valid for AF_INET and AF_INET6
  9. 9. Socket Options <ul><li>SO_REUSEADDR </li></ul><ul><ul><li>bind to the ip/port already in use </li></ul></ul><ul><ul><li>bind to the same port at different ip </li></ul></ul><ul><li>SO_REUSEPORT </li></ul><ul><ul><li>bind to the same port at the same ip </li></ul></ul>int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
  10. 10. Socket Options #include <sys/types.h> #include <sys/socket.h> sockfd = socket(. . . .); int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); bind(sockfd, . . . .);
  11. 11. TCP : TIME_WAIT Client Server close Active Close FIN_WAIT_1 Passive Close CLOSE_WAIT FIN M ACK M+1 FIN N ACK N+1 FIN_WAIT_2 TIME_WAIT close LAST_ACK CLOSED
  12. 12. TCP : TIME_WAIT <ul><li>TIME_WAIT </li></ul><ul><ul><li>Wait 2MSL (Maximum Segment Lifetime) </li></ul></ul><ul><ul><li>For 2 Reasons: </li></ul></ul><ul><ul><ul><li>If last ACK (ACK N+1) lost ... </li></ul></ul></ul><ul><ul><ul><li>Let lost duplicate packets timeout </li></ul></ul></ul>

×