• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
123
 

123

on

  • 2,367 views

 

Statistics

Views

Total Views
2,367
Views on SlideShare
2,367
Embed Views
0

Actions

Likes
0
Downloads
28
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

123 123 Presentation Transcript

  • Berkeley Socket Network Programming
  • TCP Socket : Server
    • Server
    socket() bind() listen() accept()
  • TCP Socket : Server
    • socket()
    • bind()
    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));
  • TCP Socket : Server
    • listen()
    • accept()
    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);
  • TCP Socket : Client
    • Client
    socket() connect()
  • TCP Socket : Client
    • socket()
    • connect()
    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));
  • TCP Socket : functions
    • convert values between host and network
    • Internet address manipulation routines
    #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);
  • TCP Socket : functions
    • inet_ntoa()
    • inet_pton()
    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
  • Socket Options
    • SO_REUSEADDR
      • bind to the ip/port already in use
      • bind to the same port at different ip
    • SO_REUSEPORT
      • bind to the same port at the same ip
    int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
  • 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, . . . .);
  • 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
  • TCP : TIME_WAIT
    • TIME_WAIT
      • Wait 2MSL (Maximum Segment Lifetime)
      • For 2 Reasons:
        • If last ACK (ACK N+1) lost ...
        • Let lost duplicate packets timeout