23
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010

Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010

Embed Size (px)

Citation preview

  • Slide 1
  • Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010
  • Slide 2
  • Socket Interface (API) application transport Socket Interface 1982 UNIX TCP/IP Socket Berkeley Socket Interface Berkeley Software Distribution(BSD) Windows Sockets Winsock UNIX Berkeley Sockets 2
  • Slide 3
  • Socket Interface (con.) 3
  • Slide 4
  • Connection-oriented socket (TCP) 4
  • Slide 5
  • socket() Create an endpoint for communication int socket( int domain, int type, int protocol ); domain PF_INET for IPv4 PF_INET6 for IPv6 type SOCK_STREAM for reliable TCP sockets SOCK_DGRAM for unreliable fast UDP sockets protocol default: 0 5
  • Slide 6
  • bind() Bind a name to a socket int bind( int s, const struct sockaddr * name, socklen_t namelen ); s The file descriptor to be bound. name A pointer to the sockaddr structure that holds the address to be bound to the socket. namelen The length of the sockaddr structure pointed to by name. 6
  • Slide 7
  • bind() (con.) struct sockaddr { sa_family_t sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; sa_family represents address family sa_data contains data about address 7
  • Slide 8
  • bind() (con.) struct in_addr { uint32_t s_addr; // 32bit IPv4 address (4 bytes) // network byte ordered }; struct sockaddr_in { sa_family_t sin_family; // Address family (2 bytes) in_port_t sin_port; // Port number (2 bytes) struct in_addr sin_addr; // Internet address (4 bytes) char sin_zero[8]; // Empty (for padding) (8 bytes) } 8
  • Slide 9
  • bind() (con.) Example: struct sockaddr_in servaddr; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY);htonl servaddr.sin_port = htons(13); Bind (sockfd, (struct sockaddr *) & servaddr, sizeof(servaddr) ); 9
  • Slide 10
  • listen() Listen for connections on a socket int listen( int s, int backlog ); s The descriptor for the socket that you want to listen on. backlog The maximum length that the queue of pending connections may grow to. 10
  • Slide 11
  • connect() Initiate a connection on a socket int connect( int s, const struct sockaddr * name, socklen_t namelen ); s The descriptor of the socket on which to initiate the connection. name The name of the socket to connect to for a SOCK_STREAM connection. namelen The length of the name, in bytes. 11
  • Slide 12
  • accept() Accept a connection on a socket int accept( int s, struct sockaddr * addr, socklen_t * addrlen ); s The listen()ing socket descriptor. addr This is filled in with the address of the site that's connecting to you. addrlen This is filled in with the sizeof() the structure returned in the addr parameter. You can safely ignore it if you assume you're getting a struct sockaddr_in back, which you know you are, because that's the type you passed in for addr. 12
  • Slide 13
  • send() Send a message to a connected socket ssize_t send( int s, const void * msg, size_t len, int flags ); msg A pointer to the message that you want to send. len The length of the message. flags Set flags to zero if you want it to be "normal" data. 13
  • Slide 14
  • recv() Receive a message from a socket ssize_t recv( int s, void * buf, size_t len, int flags ); buf A pointer to a buffer where the function can store the message. len The size of the buffer. flags Set flags to 0 if you want it to be a regular vanilla recv(). 14
  • Slide 15
  • close() Close a socket descriptor int close(int s); Windows users: the function you need to use is called closesocket(), not close(). 15
  • Slide 16
  • Connectionless socket (UDP) 16
  • Slide 17
  • sendto() Send a message to a socket at a specific address ssize_t sendto( int s, const void * msg, size_t len, int flags, const struct sockaddr * to, socklen_t tolen ); to A pointer to a sockaddr object that specifies the address of the target. tolen A socklen_t object that specifies the size of the to address. 17
  • Slide 18
  • recvfrom() Receive a message from the socket at a specified address ssize_t recvfrom( int s, void * buff, size_t len, int flags, struct sockaddr * from, socklen_t * fromlen ); from NULL, or a pointer to a sockaddr object where the function can store the source address of the message. fromlen A pointer to a socklen_t object that specifies the size of the from buffer. The function stores the actual size of the address in this object. 18
  • Slide 19
  • inet_ntop() inet_pton() Convert IP addresses to human-readable form and back const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); int inet_pton(int af, const char *src, void *dst); The "n" stands for "network", and "p" for "presentation". These functions are for dealing with human-readable IP addresses and converting them to their binary representation for use with various functions and system calls. 19
  • Slide 20
  • IPv4 v.s IPv6 Socket 20
  • Slide 21
  • Demo Environment OS: Fedora 14 Client(homer): 10.21.10.154 2001:e10:6840:21:4a5b:39ff:fed6:1b73 Server(sinbad): 10.10.21.70 2001:e10:6840:21:21a:92ff:fe02:3495 Socket TCP (IPv4): tcp4_client.c, tcp4_server.ctcp4_client.ctcp4_server.c Socket UDP (IPv4): udp4_client.c, udp4_server.cudp4_client.cudp4_server.c Socket TCP (IPv6): tcp6_client.c, tcp6_server.ctcp6_client.ctcp6_server.c 21
  • Slide 22
  • Reference 1.http://beej.us/guide/bgnet/output/html/multipage/index.htmlhttp://beej.us/guide/bgnet/output/html/multipage/index.html 2.http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/sys_arc h/tcpip.htmlhttp://www.qnx.com/developers/docs/6.3.0SP3/neutrino/sys_arc h/tcpip.html 3.http://ms11.voip.edu.tw/~jryan/ref/20090326-chenglin- socket_programming.ppthttp://ms11.voip.edu.tw/~jryan/ref/20090326-chenglin- socket_programming.ppt 4.http://ms11.voip.edu.tw/~jryan/ref/SocketIntro.ppthttp://ms11.voip.edu.tw/~jryan/ref/SocketIntro.ppt 5.http://ms11.voip.edu.tw/~jryan/ref/Introduction_to_IPv6_progra mming.pdfhttp://ms11.voip.edu.tw/~jryan/ref/Introduction_to_IPv6_progra mming.pdf 6.http://ms11.voip.edu.tw/~jryan/ref/Windows_Socket Programming_&_IPv6_Translation Middleware.pdfhttp://ms11.voip.edu.tw/~jryan/ref/Windows_Socket Programming_&_IPv6_Translation Middleware.pdf 7.http://zh.wikipedia.org/zh- tw/%E4%BC%AF%E5%85%8B%E5%88%A9%E5%A5%97%E6% 8E%A5%E5%AD%97http://zh.wikipedia.org/zh- tw/%E4%BC%AF%E5%85%8B%E5%88%A9%E5%A5%97%E6% 8E%A5%E5%AD%97 8.http://ms11.voip.edu.tw/~jryan/ref/csb051-02-netprog.pdfhttp://ms11.voip.edu.tw/~jryan/ref/csb051-02-netprog.pdf 9.http://ms11.voip.edu.tw/~jryan/ref/ipv6_socket_programming.pp thttp://ms11.voip.edu.tw/~jryan/ref/ipv6_socket_programming.pp t 22
  • Slide 23
  • Thanks for listening~ 23
  • Slide 24
  • htons() htonl() ntohs() ntohl() Convert multi-byte integer types from host byte order to network byte order uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); htons()host to network short htonl()host to network long ntohs()network to host short ntohl()network to host long 24