%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% THE MIDGARD MAGICPOINT TEMPLATE %% %% Copyright(c) 1999 Henri Bergius %% %% A template for making MagicPoint presentations for %% Midgard. Originally written for MagicPoint 1.06a %% and the Midgard Workshop in October 1999. %% %% This version is for print usage, with white background. %% %% See the Midgard CVS repository for usage examples. %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %deffont "standard" tfont "arial.ttf", xfont "helvetica-medium-r" %deffont "thick" tfont "arialbd.ttf", xfont "helvetica-bold-r" %deffont "code" tfont "courbd.ttf", xfont "courier-medium-r" %% %% Default settings per each line numbers. %% %% The page settings: %default 1 leftfill, size 2, fore "black", back "white", font "thick", bimage "background.jpg" 1024x768 %% %% Format the header: %default 2 size 7, vgap 70, prefix " " %% %% Have a bar: %default 3 size 2, bar "brown" 5, vgap 50 %% %% The standard text settings: %default 4 size 5, fore "black", vgap 50, prefix " ", font "standard" %% %% Default settings that are applied to TAB-indented lines. %% %tab 1 size 4, vgap 40, prefix " ", icon box "brown" 30 %tab 2 size 3, vgap 30, prefix " ", icon arc "gray30" 30 %tab 3 size 2, vgap 30, prefix " ", icon delta3 "black" 40 %% %% Enable page caching (immediate, no effect, high speed) %% #%pcache 1 1 1 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page %center, size 4 %size 7 Introduction to Socket Programming %right %size 4 by Bart Trojanowski bart@jukie.net %cont, center %image "linux-logo.png" %size 2 December OCLUG meeting 2001-12-06 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page Overview The Phone Analogy The Sockets API Network Byte Order Naming Services Client & Server Data Transfer Socket I/O Control References %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page The Phone Analogy "Phone number" == "IP address" must be predefined and known by both parties google.com == 216.239.33.100 as \ 555-1212 == some guy in the movie "Extension" == "Port number" also predefined and known by both parties port 80 == HTTP as extension 0 == operator "411 number directory" == "DNS" accessing the directory is optional; i.e. connection can be \ made by already resolved number "Caller" == "client" client initiates the connection knowing the IP address \ and port number "Recipient" == "server" server listens on the specific IP address and port number \ for new connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page ...The Phone Analogy Placing a call: %mark get a phone %again, font "code" socket() %mark, font "standard" select outgoing line %again, font "code" bind() %mark, font "standard" dial the number %again, font "code" connect() %mark, font "standard" talk %again, font "code" read()/write() %mark, font "standard" hang up %again, font "code" close() %font "standard" Accepting a call: %mark get a phone %again, font "code" socket() %mark, font "standard" select incoming line %again, font "code" bind() %mark, font "standard" turn on the ringer %again, font "code" listen() %mark, font "standard" pickup a ringing phone %again, font "code" accept() %mark, font "standard" talk %again, font "code" read()/write() %mark, font "standard" hang up %again, font "code" close() %font "standard" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page The Sockets API %right ... continuation of the Phone Analogy %left use " %cont, font "code" socket %cont, font "standard" " to create a phone (socket) must specify domain and type for new socket: PF_INET: IPv4 %cont, font "thick" domain %cont, font "standard", icon delta3 "gray30" 30 SOCK_DGRAM: datagram oriented (UDP %cont, font "thick" type %cont, font "standard" ) PF_INET6: IPv6 %cont, font "thick" domain %cont, font "standard", icon delta3 "gray30" 30 SOCK_STREAM: connection oriented (TCP %cont, font "thick" type %cont, font "standard" ) use " %cont, font "code" bind %cont, font "standard" " to set your extension (port) number this is a local setup step (plug in the phone and configure it) client may decide to use a dynamically allocated number server should set this number; there are exceptions (ftp data) use " %cont, font "code" listen %cont, font "standard" " to turn on the ringer server must start listening before any connection can be made use " %cont, font "code" accept %cont, font "standard" " to pick up a ringing phone server accepts a connection via this function call the IP address of the client is revealed by this call use " %cont, font "code" connect %cont, font "standard" " to dial the server the client calls this function to issue the "call" step is optional for datagram sockets as there is no connection %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page Network Byte Order Internet protocols use big-endian byte ordering for multi-byte fields It is up to the network programmer to deal with ordering changes good style dictates calling byte-ordering change functions \ even on big-endian only systems %font "code", right, size 2 #include %font "standard", left " %cont, font "code" ntohs, ntohl %cont, font "standard" " - convert network to host byte order %font "code", right, size 2 #include %font "standard", left " %cont, font "code" htons, htonl %cont, font "standard" " - convert host to network byte order Internet data is composed of 8-bit entities called octets today an octet is equivalent to a byte past systems, like the DEC-10, did not use 8-bit bytes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page Naming Services %font "code", right, size 2 #include %font "standard", left " %cont %font "code" gethostbyname %cont %font "standard" " - lookup hostname in DNS, NIS, /etc/hosts, etc. query can be made on an dotted-decimal or host name strings returns all matches for query supports IPv4 or IPv6 (not both at the same time) %font "code", right, size 2 #include %font "standard", left " %cont %font "code" gethostbyname2 %cont %font "standard" " - better support for IPv6 allows caller to specify address family to return %font "code", right, size 2 #include %font "standard", left " %cont %font "code" gethostbyaddr %cont %font "standard" " - reverse lookup takes an address and returns the same structure as the other \ look-up functions %font "code", right, size 2 #include %font "standard", left " %cont %font "code" getservbyname, getservbyport %cont %font "standard" " - search /etc/services allows to locate port numbers from service names \ and the reverse works for UDP and TCP protocol types " %cont, font "code" gethostbyname_r, gethostbyaddr_r %cont, font "standard" " are reenterant %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page ...Naming Services %font "code", right, size 2 #include %font "standard", left " %cont %font "code" inet_aton, inet_ntoa, inet_addr %cont %font "standard" " - IPv4 only convert between dotted-decimal string and its 32-bit network \ byte ordered binary example: "1.2.3.4" <==> 0x04030201 (on a little-endian host) %font "code", right, size 2 #include %font "standard", left " %cont %font "code" inet_pton, inet_ntop %cont %font "standard" " - IPv4 and IPv6 convert between presentation format and numeric network \ byte ordered binary %font "code", right, size 2 #include %font "standard", left " %cont %font "code" getaddrinfo %cont %font "standard" " - more informative hostname lookup provides a common interface to all of the services just covered returns an array of structures which can be used by %cont, font "code" bind %cont, font "standard" and %cont, font "code" connect %font "standard" %font "code", right, size 2 #include %font "standard", left " %cont %font "code" getnameinfo %cont %font "standard" " - reverse of above takes an initialized socket address structure and uses \ it for DNS lookup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page The Client The simple client: %font "code", size 2, vgap 5 int main (void) { int sock; struct sockaddr_in serv_addr; sock = socket (AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons (110); /* pop3 client */ inet_aton ("127.0.0.1", &serv_addr.sin_addr); connect (sock, &serv_addr, sizeof (serv_addr)); ... close (sock); } %font "standard" Common client tasks: create a socket optionally bind to a local port create a server socket structure connect to server communicate close the server socket %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page The Server The simple server: %font "code", size 2, vgap 5 int main (void) { int serv, cli, cli_len; struct sockaddr_in serv_addr, cli_addr; serv = socket (AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons (110); /* pop3 server */ serv_addr.sin_addr.s_addr = htonl (INADDR_ANY); bind (serv, &serv_addr, sizeof (serv_addr)); listen (serv, 5); for (;;) { cli_len = sizeof (cli_addr); cli = accept (serv, &cli_addr, &cli_len); ... close (cli); } close (serv); } %font "standard" Common server tasks: create a socket create a server socket structure and bind on it start listening for connections accept client connections communicate and close client socket close the server socket %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page ...The Server (concurrent clients) Multi-threaded one server thread handles all accepts the server thread creates a new agent thread for each client accepted agents handle requests for their specific clients Select / Poll one process solution using the select or poll system calls process alternates between the server and client sockets %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page ...The Server (select) %font "code", size 2, vgap 5 int main (void) { int sock_fd[MAX_TO_HANDLE], sock_num=0, fd_max=0, i; fd_set rfds; fd_max = sock_fd[sock_num++] = socket (AF_INET, SOCK_STREAM, 0); /* setup the socket */ ... FD_ZERO (&rfds); FD_SET (sock_fd[0], &rfds); for (;;) { select (fd_max + 1, &rfds, NULL, NULL, NULL); if (FD_ISSET (sock_fd[0], &rfds)) { sock_fd[sock_num] = accept (sock_fd[0], NULL, NULL); if (fd_max < sock_fd[sock_num]) fd_max = sock_fd[sock_num]; sock_num++; } FD_SET (sock_fd[0], &rfds); for (i=1; i #include %font "standard", left " %cont, font "code" getsockopt %cont, font "standard" " and " %cont, font "code" setsockopt %cont, font "standard" " manipulate options associated with a socket each function takes a protocol level and option to access as \ parameters for example this call allows the port of a bound socket \ to be reused quickly: %font "code", size 3 getsockopt (s, SOL_SOCKET, SO_REUSEPORT, &value); %font "standard" some other useful values include: SOL_SOCKET, SO_SNDBUF - set send buffer SOL_SOCKET, SO_RCVBUF - set receive buffer IPPROTO_IP, IP_TOS - set TOS field in IP header IPPROTO_IP, IP_TTL - set TTL field in IP header IPPROTO_TCP, TCP_MAXRT - set maximum retransmit time IPPROTO_TCP, TCP_MAXSEG - set maximum segment size IPPROTO_TCP, TCP_KEEPALIVE - set number of seconds \ between TCP keep-alive probes Useful " %cont, font "code" ioctl %cont, font "standard" " calls for sockets " %cont, font "code" ioctl %cont, font "standard" " calls can be made on sockets just in the same way as on file descriptors some interesting ioctls include: FIONREAD - get the amount of unread data in \ receive buffer TIOCOUTQ - get the amount of unsent data in \ send buffer SIOCGSTAMP - get timestamp of last packet \ received FIOASYNC - change to asynchronous I/O mode \ (non blocking event driven interaction) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page References Books: UNIX Network Programming Vol I, by W. Richard Stevens 2nd Ed, 1998, Prentice Hall this is a fantastic book by an amazing author; \ never hesitate to purchase any book by this man; \ sadly he is no longer with us Man pages: Socket specific system calls: socket(2) bind(2) listen(2) accept(2) connect(2) getsockopt(2) recv(2) sned(2) Relevant system calls: read(2) write(2) ioctl(2) Information about socket configuration: socket(7) ip(7) tcp(7) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page ...References (on the web) BSD Sockets: A Quick And Dirty Primer http://ftp.std.com/homepages/jimf/sockets.html An Introduction to Socket Programming http://www.uwo.ca/its/doc/courses/notes/socket/index.html UNIX Network Programming http://www-net.cs.umass.edu/ntu_socket/ Beej's Guide to Network Programming Beej's Guide to Network Programming http://www.ecst.csuchico.edu/~beej/guide/net/ Spencer's Socket Site http://www.lowtek.com/sockets/ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page ...References (even more on the web) Introduction to Socket Programming http://www.linuxgazette.com/issue47/bueno.html Linux Network Programming (a magazine column) http://www2.linuxjournal.com/lj-issues/issue46/2333.html http://www2.linuxjournal.com/lj-issues/issue47/2335.html http://www2.linuxjournal.com/lj-issues/issue48/2336.html BSD Sockets Programming Tutorial http://www.ecst.csuchico.edu/~chafey/prog/sockets/sinfo1.html UNIX Socket FAQ http://www.lcg.org/sock-faq/ Socket Programming in C (examples) http://pont.net/socket/