Programming TCP/IP with Sockets

7,137 views

Published on

Published in: Technology, Education
  • Good show, neatly presented, nice flow chart on starting a socket
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Programming TCP/IP with Sockets

  1. 1. Session NM056 Programming TCP/IP with Sockets Geoff Bryant Process software
  2. 2. Course Roadmap NM055 (11:00-12:00) Important Terms and Concepts TCP/IP and Client/Server Model Sockets and TLI Client/Server in TCP/IP NM056 (1:00-2:00) Socket Routines NM057 (2:00-3:00) Library Routines NM058 (3:00-4:00) Sample Client/Server NM059 (4:00-5:00) VMS specifics (QIOs) NM067 (6:00-7:00) Clinic - Q&A Slide 57 Portions Copyright © 1996, Opus1, Process Software, TGV
  3. 3. TCP/IP Programming Slides and Source Code available via anonymous FTP: Host:: ftp.process.com Directory: [pub.decus] Slides: DECUS_F96_PROG.PS Examples: DECUS_F96_PROG_EXAMPLES.TXT Host: ftp.opus1.com Slides: DECUS_F96_PROG.PS Examples: DECUS_F96_PROG_EXAMPLES.TXT Slide 58 Portions Copyright © 1996, Opus1, Process Software, TGV
  4. 4. Programming with Sockets Roadmap Berkeley Socket History Overview of the Socket Paradigm Socket addresses Other programming models Slide 59 Portions Copyright © 1996, Opus1, Process Software, TGV
  5. 5. Berkeley Socket History Documented in such books as Stevens’s Unix Network Programming Comer’s Internetworking with TCP/IP, vol III First provided with Berkeley Software Distribution (BSD 4.1c) Unix for the VAX Popularized in the 1986 4.3BSD release Slide 60 Portions Copyright © 1996, Opus1, Process Software, TGV
  6. 6. Other Programming Models Socket model is widely used mainly due to wide implementation of BSD networking kernel Avaible for Unix, Windows, VMS, ... Other models may be used in different environments STREAMS model (UNIX System V) Others Slide 61 Portions Copyright © 1996, Opus1, Process Software, TGV
  7. 7. Socket Paradigm Overview A socket is a communications endpoint In BSD networking, it is a data structure within the kernel A socket is “named” by its socket address A connection is represented by two communicating sockets Slide 62 Portions Copyright © 1996, Opus1, Process Software, TGV
  8. 8. Using sockets is like using the file system Server socket() Client socket() bind() listen() accept() block until connection connect() connection from establishment client read() write() write() read() close() close() Slide 63 Portions Copyright © 1996, Opus1, Process Software, TGV
  9. 9. Servers sit in a tight loop socket() bind() listen() accept() read() write() close() Slide 64 Portions Copyright © 1996, Opus1, Process Software, TGV
  10. 10. Connectionless Client/Server is simpler Server socket() Client socket() bind() bind() recvfrom() sendto() block until data from client sendto() recvfrom() Slide 65 Portions Copyright © 1996, Opus1, Process Software, TGV
  11. 11. A socket is just a data structure socket Operating System Protocol Family Channel List Service Local Address Remote Address Slide 66 Portions Copyright © 1996, Opus1, Process Software, TGV
  12. 12. In C, a socket doesn’t have much integer: always set to socket be PF_INET for Protocol Family TCP/IP programming integer: set to be Service SOCK_STREAM (TCP), SOCK_DGRAM (UDP), sockaddr: a 2-octet Local Address or SOCK_RAW (raw IP) address family Remote Address identifier and then 14 octets of address- specific information Slide 67 Portions Copyright © 1996, Opus1, Process Software, TGV
  13. 13. Families and Types Address Families (or Protocol Families) AF_UNIX: Unix domain sockets AF_INET: Internet Protocols AF_NS: Xerox NS Protocols AF_IMPLINK: IMP link layer (obsolete) Types: SOCK_STREAM: Stream socket (TCP) SOCK_DGRAM: Datagram socket (UDP) SOCK_RAW: Raw socket (IP) Slide 68 Portions Copyright © 1996, Opus1, Process Software, TGV
  14. 14. TCP/IP addresses are special cases of sockaddr socket Protocol Family struct sockaddr { u_short sa_family; Service char sa_data[16]; Local Address } Remote Address struct sockaddr_in { u_short sin_family; u_short sin_port; sockaddr: a 2-octet struct in_addr sin_addr; address family char sin_zero[8]; identifier and then 14 } octets of address- specific information Slide 69 Portions Copyright © 1996, Opus1, Process Software, TGV
  15. 15. sockaddr unveiled struct sockaddr_in { sin_family: AF_INET for all TCP/IP u_short sin_family; addresses u_short sin_port; struct in_addr sin_addr; sin_port: 16-bit port number (as char sin_zero[8]; used in UDP & TCP headers) } sin_addr: 32-bit IP address /* * Internet address (a structure for historical reasons) */ struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; #define s_addr S_un.S_addr Slide 70 Portions Copyright © 1996, Opus1, Process Software, TGV
  16. 16. Remember our goal: open() TCP/IP socket programming is mostly read() and write() subroutine calls All of the socket routines are there to do the equivalent of an open() in the file system. Slide 71 Portions Copyright © 1996, Opus1, Process Software, TGV
  17. 17. Five routines are used to replace the open() call socket socket() : allocate socket in memory, fill in Protocol Family protocol family and service fields Service bind() : fill in the local address part (local sockaddr). OPTIONAL for most clients Local Address Remote Address listen() : tell network kernel that you want to receive connects (“passive open”) accept() : ask network kernel to hand you the next incoming connect (“passive open”) connect() : tell network kernel to connect to the other side (“active open”) Slide 72 Portions Copyright © 1996, Opus1, Process Software, TGV
  18. 18. Overview of Sockets (one more time) Get the socket open somehow socket(), bind(), listen(), accept(), connect() Read and write from it read(), write() When done, close the socket close() Slide 73 Portions Copyright © 1996, Opus1, Process Software, TGV
  19. 19. Programming with Sockets Key Concepts A socket is a data structure representing a connection To open a connection Fill in the data structure Link it to the network kernel Link it to the operating system Reading and writing uses the same calls as the file system Slide 74 Portions Copyright © 1996, Opus1, Process Software, TGV
  20. 20. Coding with Sockets
  21. 21. General Flow for Servers/Clients is different Server socket() socket() Client bind() listen() accept() block until connection connect() connection from establishment client read() write() write() read() Let’s start with clients close() because close() they’re easier Slide 76 Portions Copyright © 1996, Opus1, Process Software, TGV
  22. 22. Clients
  23. 23. Step 1: Get Socket socket() int socket(int domain, int type, int protocol); connect() int s, domain, type, protocol; write() domain = AF_INET; /* always the same */ read() type = SOCK_STREAM; /* STREAM for TCP */ close() protocol = 0; s = socket(domain, type, protocol); if (s < 0) ERROR(“Cannot create socket”); Slide 78 Portions Copyright © 1996, Opus1, Process Software, TGV
  24. 24. Step 2: Fill in remote address fields socket() #define SMTP_PORT 25 connect() #define IP_ADDRESS 0xC0F50C02 /*192.245.12.2*/ write() struct sockaddr_in sin; read() sin.sin_family = AF_INET; close() sin.sin_port = htons(SMTP_PORT); sin.sa_addr = htonl(IP_ADDRESS); Slide 79 Portions Copyright © 1996, Opus1, Process Software, TGV
  25. 25. Step 3: Connect to other side socket() int connect(int s, struct sockaddr*name, int connect() namelen); int status; write() read() status = connect(s, (struct sockaddr*)sin, sizeof(sin)); close() if (status != 0) ERROR(“Cannot connect to other side”); Slide 80 Portions Copyright © 1996, Opus1, Process Software, TGV
  26. 26. Step 4: Use the socket socket() char buf[LINELEN+1]; connect() /* * Now go into a loop, reading data from the network, writing write() * it to the terminal and reading data from the terminal, * writing it to the network... */ read() while ( (n = read(s, buf, LINELEN) ) > 0) { close() fwrite(buf, n, 1, stdout); if (!fgets(buf, LINELEN, stdin)) break; write(s, buf, strlen(buf)); } if (n < 0) { ERROR(“Cannot read from socket!”); } Slide 81 Portions Copyright © 1996, Opus1, Process Software, TGV
  27. 27. Step 5: Shut it down when done socket() status = close(s); connect() if (status != 0) ERROR(“Can not close socket”); write() read() close() Slide 82 Portions Copyright © 1996, Opus1, Process Software, TGV
  28. 28. Servers
  29. 29. Server Review and Overview socket() socket() : allocate socket in memory, fill in protocol family and service fields bind() bind() : fill in the local address part (local listen() sockaddr). OPTIONAL for most clients accept() read() listen() : tell network kernel that you want write() to receive connects (“passive open”) accept() : ask network kernel to hand you close() the next incoming connect (“passive open”) Slide 84 Portions Copyright © 1996, Opus1, Process Software, TGV
  30. 30. Step 1: Get Socket socket() int socket(int domain, int type, int protocol); bind() int s, domain, type, protocol; listen() accept() domain = PF_INET; /* always the same */ type = SOCK_STREAM; /* STREAM for TCP */ read() protocol = 0; write() s = socket(domain, type, protocol); close() if (s < 0) ERROR(“Cannot create socket”); This should look vaguely familiar... Slide 85 Portions Copyright © 1996, Opus1, Process Software, TGV
  31. 31. Step 2: Fill in local address fields socket() #define SMTP_PORT 25 bind() #define IP_ADDRESS 0xC0F50C02 /*192.245.12.2*/ listen() struct sockaddr_in sin; accept() sin.sin_family = AF_INET; read() sin.sin_port = htons(SMTP_PORT); write() sin.sa_addr = INADDR_ANY; close() INADDR_ANY is a shorthand way of saying “I don’t care what the local address is”... Slide 86 Portions Copyright © 1996, Opus1, Process Software, TGV
  32. 32. Step 3: “Bind” address to the socket socket() int bind(int s, struct sockaddr*name, int bind() namelen); int status; listen() accept() status = bind(s, (struct sockaddr*)sin, read() sizeof(sin)); write() if (status != 0) ERROR(“Cannot bind to local address”); close() INADDR_ANY is a shorthand way of saying “I don’t care what the local address is”... Slide 87 Portions Copyright © 1996, Opus1, Process Software, TGV
  33. 33. Step 4: Tell the kernel to listen socket() #define MAX_BACKLOG 5 bind() int listen(int s, int backlog); listen() int status; accept() status = listen(s, MAX_BACKLOG); read() if (status != 0) write() ERROR(“Cannot ask kernel to listen”); close() Slide 88 Portions Copyright © 1996, Opus1, Process Software, TGV
  34. 34. Step 5: Block waiting for connect socket() int accept(int s, struct sockaddr *addr, int bind() *addrlen); int status, addrlen, vs; listen() struct sockaddr_in sin2; accept() read() addrlen = sizeof(sin2); write() vs = accept(s, (struct sockaddr*)&sin2, &addrlen); close() if (vs < 0) ERROR(“Cannot accept a connection.”); Slide 89 Portions Copyright © 1996, Opus1, Process Software, TGV
  35. 35. Step 6: Read and Write socket() /* Get the line from the client. */ read ( vs, buf, 256 ); bind() /* Translate the logical name. */ listen() log_name = getenv ( buf ); accept() /* Get the definition string and add a new line to it. */ sprintf ( buf, quot;%snquot;, log_name ); read() /* Write the translation to the client. */ write ( vs, buf, strlen ( buf ) ); write() close() Yes, this code has lots of holes in it. But you get the picture of what we’re trying to do. Slide 90 Portions Copyright © 1996, Opus1, Process Software, TGV
  36. 36. Step 7: Tear down when done socket() status = close(s); bind() if (status != 0) ERROR(“Can not close socket”); listen() accept() read() write() close() Slide 91 Portions Copyright © 1996, Opus1, Process Software, TGV
  37. 37. Of course, there are more routines than those socket Create a descriptor for use in network communication connect Connect to a remote peer (client) write Send outgoing data across a connection read Acquire incoming data from a connection close Terminate communication and deallocate a descriptor bind Bind a local IP address and protocol port to a socket listen Place the socket in passive mode and set backlog accept Accept the next incoming connection (server) recv, recvmsg Receive the next incoming datagram recvfrom Receive the next incoming datagram and record source addr. send, sendmsg Send an outgoing datagram sendto Send an outgoing datagram to a particular dest. addr. shutdown Terminate a TCP connection in one or both directions getpeername After a connection arrives, obtain remote machine’s address getsockopt Obtain the current options for a socket setsockopt Change the options for a socket Slide 92 Portions Copyright © 1996, Opus1, Process Software, TGV
  38. 38. Coding with Sockets Key Concepts All clients and servers basically look the same Follow a template, but make sure you understand what you’re doing Lots of templates are wrong Lots of templates don’t do what you think they do Slide 93 Portions Copyright © 1996, Opus1, Process Software, TGV

×