TCP/IP ProgrammingTCP/IP Programming
Joel Snyder, Opus1Geoff Bryant, Process Software
Portions Copyright © 1996 TGV Software, Inc., Copyright © 1996 Process Software Corp. Copyright © 1996 Opus1
Slide 2 Portions Copyright © 1996, Opus1, Process Software, TGV
Course Roadmap
NM055 (11:00-12:00) Important Terms andConcepts
TCP/IP and Client/Server ModelSockets and TLIClient/Server in TCP/IP
NM056 (1:00-2:00) Socket RoutinesNM057 (2:00-3:00) Library RoutinesNM058 (3:00-4:00) Sample Client/ServerNM059 (4:00-5:00) VMS specifics (QIOs)NM067 (6:00-7:00) Clinic - Q&A
Slide 3 Portions Copyright © 1996, Opus1, Process Software, TGV
TCP/IP Programming
Slides and Source Code available via anonymous FTP:
Host:: ftp.process.comDirectory: [pub.decus]Slides: DECUS_F96_PROG.PSExamples: DECUS_F96_PROG_EXAMPLES.TXT
Host: ftp.opus1.comSlides: DECUS_F96_PROG.PSExamples: DECUS_F96_PROG_EXAMPLES.TXT
Session NM055Session NM055
TCP/IP ProgrammingTCP/IP ProgrammingTerms and ConceptsTerms and Concepts
Joel SnyderJoel SnyderOpus1Opus1
Slide 5 Portions Copyright © 1996, Opus1, Process Software, TGV
Terms and ConceptsOverview
What is the TCP/IP model?What is the client/server model?What are sockets and TLI?What is network byte order?What is encapsulation? Multiplexing?Demultiplexing? Fragmentation?What are addresses?
Slide 6 Portions Copyright © 1996, Opus1, Process Software, TGV
Networks are layered tosimplify construction
Data Link
Physical
Network
Transport
Session
Presentation
Application
Data Link
Physical
Network
Data Link
Physical
Network
Transport
Session
Presentation
Application
The FamousOSI 7-Story
ApartmentBuilding with
attachedParking Garage
Slide 7 Portions Copyright © 1996, Opus1, Process Software, TGV
The OSI Model(similar to the Holy Grail)
Physical
Data Link
Network
Transport
Session
Presentation
Application
Transmission of binary data of a medium
Transfer of units of information, framing, and error checking
Delivery of packets of information, which includes routing
Provision for end-to-end reliable and unreliable delivery
Establishment and maintenance of sessions
Data formatting and encryption
Network applications such as file transfer and terminalemulation
OSI layer Function provided
Slide 8 Portions Copyright © 1996, Opus1, Process Software, TGV
TCP/IP is (usually) slicedup into four layers
DataLink/Physi
cal
Network
Transport
Application
Network Network
Transport
Application
There are actuallymore layers to
TCP/IP than this setof four, but from theprogramming pointof view, this will do
just fine.Data
Link/Physical
DataLink/Physi
cal
Slide 9 Portions Copyright © 1996, Opus1, Process Software, TGV
We care most about thetwo center layers
Application
TCP
(Transmission Control Protocol)
UDP
(User Datagram Protocol)
IP
(Internet Protocol)
hardwareinterface
Application ApplicationApplication
Transport
Network
Physical/Data Link
Slide 10 Portions Copyright © 1996, Opus1, Process Software, TGV
TCP
Transmission Control Protocol is defined byRFC-793TCP provides connection-oriented transportserviceEnd-to-end transparent byte-stream
Slide 11 Portions Copyright © 1996, Opus1, Process Software, TGV
UDP
User Datagram Protocol is defined by RFC-768UDP provides datagram serviceConnectionless
Slide 12 Portions Copyright © 1996, Opus1, Process Software, TGV
Client/Server isapplication-to-application
TCP/IP and DECnet are client/servernetworksA client/server application has two parts
One which runs on one side of the networkOne which runs on the other side of the network
Differentiate this from a terminal-basednetwork or most Netware applications
Slide 13 Portions Copyright © 1996, Opus1, Process Software, TGV
Clients and Servers useIPC to talk to each other
Inter-Process Communication mechanismslet two cooperating applicationscommunicateThere are LOTS of IPC mechanisms forlocal communicationsThe two popular TCP/IP based IPCs areSockets (Berkeley Unix) and TLI (AT&T UnixSystem V)
Slide 14 Portions Copyright © 1996, Opus1, Process Software, TGV
Sockets is the standard APIfor TCP/IP IPC
Normally, you’d have an operating systemspecific routine set to talk to the networkIn the world of standardized APIs, you wouldhave an operating system independent setof routinesSockets takes it one step further: it makesthe network look much like a file system
Slide 15 Portions Copyright © 1996, Opus1, Process Software, TGV
TCP/IP is a file system?
Routines to openup a file
Routines to closean open file
Routines to openup a connection
Routines to close anopen connection
Read
Write
Yes, sockets treatsTCP/IP as if it were a
file system(with some added complexity
in the open and closeroutines)
Slide 16 Portions Copyright © 1996, Opus1, Process Software, TGV
It looks like a file, but it’sreally a network interface
ApplicationApplication
write()
read()
write()
read()
Slide 17 Portions Copyright © 1996, Opus1, Process Software, TGV
Life was easy whenmachines had 8-bit words
Welcome to the concept of “network byteorder”Remember that “network byte order” is notthe same as “host byte order”You have to convert to network byte order!
Slide 18 Portions Copyright © 1996, Opus1, Process Software, TGV
There are two ways to storemulti-byte integers
high order byte
high order byte
low order byte
low order byte
Address n
Address n
Address n+1
Address n+1
big endian
little endian
Slide 19 Portions Copyright © 1996, Opus1, Process Software, TGV
VMS systems are littleendian architectures
Big-endian architectures Little-endian architectures
Motorola 68xxx
IBM 370
Intel 80x86
VAX & Alpha (VMS)
PDP-11(sort of)
0 1 2 3 3 2 1 0
Slide 20 Portions Copyright © 1996, Opus1, Process Software, TGV
Relax: you could be doingEthernet and Token Ring
. . . .
Byte 0 Byte 1 Byte 5
Byte 0 Byte 1 Byte 5Source address
Bit 0
Bit 0
Bit 7Transmitted first
Bit 7Transmitted first
Destination address
Type field
6 bytes
6 bytes
2 bytes
46 - 1500 bytes
4 bytes
Multicast bit
LSB MSB
M
. . . .
TransmissionsequenceDestination
address
Sourceaddress
Datafield
FrameCheck
Sequence
Bits within bytes are transmitted left to right
Slide 21 Portions Copyright © 1996, Opus1, Process Software, TGV
Encapsulation adds controlinformation to data
Trailers HeadersNow is the time for all good
File serverClient workstation
Headers TrailersRequest block of data
Request
Response
Data field
Data from user program such as an applicationprogram or session control information
Networkheaders
Networktrailers
men to come to the aid of
Response
of their country
Response
Headers
Headers
Trailers
Trailers
Now is the timefor all good mento come to theaid of theircountry
Slide 22 Portions Copyright © 1996, Opus1, Process Software, TGV
Each layer encapsulatesthe layer below it
Physical
Data Link
Network
Transport
Session
Presentation
Application
Data linkheader
Network layerheader
Transport layer header
Session layer header
Presentation and Application layer
header
OSI model
Network packet
Data linktrailers
Slide 23 Portions Copyright © 1996, Opus1, Process Software, TGV
Things can be added onand peeled off at the ends
ApplicationLayer Data
ApplicationLayer Data
ApplicationLayer Data
ApplicationLayer Data
ApplicationLayer Data
Look at your averageWWW query
HTTPheader
HTTPheader
HTTPheader
HTTPheader
TCPheader
TCPheader
TCPheader
IPheader
IPheader
Ethernetheader
Ethernettrailer
14 20 20 192 4
Slide 24 Portions Copyright © 1996, Opus1, Process Software, TGV
Make sure you memorizeall of this encapsulation info
Destinationaddress
Source address
Type field Data field CRC
Destinationaddress
Source address
Length field Data field CRC
Destinationaddress
Source address
Length field DSAP SSAP CTRL Data field CRC
SNAP header
Destinationaddress
Source address
Length field Data field CRCFFFF
Destinationaddress
Source address
Length field DSAP SSAP CTRL Data field CRCOUI
Ethernet V2.0
IEEE 802.3
IEEE 802.3 with IEEE 802.2
IEEE 802.3 SNAP
Novell proprietary
6 bytes 2 bytes Up to 1500 bytes 4 bytes
Up to 1496 bytes
6 bytes
6 bytes 6 bytes 4 bytes
4 bytes
2 bytes 4 bytes6 bytes 6 bytes 1 byte 1 byte *
4 bytes2 bytes6 bytes 6 bytes Up to 1500 bytes
6 bytes 6 bytes 2 bytes 1 byte 1 byte 3 bytes 2 bytes 1492 bytes
FDDI
Token Ring
Destinationaddress
Source address
Length field DSAP SSAP CTRL Data field
4 bytes6 bytes 6 bytes 2 bytes 1 byte 1 byte 4472 bytes
EtherType
Preamble SD FC FCS FSED
1 byte 1 byte
Destinationaddress
Source address DSAP SSAP CTRL Data field CRC
4 bytes6 bytes 6 bytes 1 byte 1 byte 4472 (4 Mbps or 17800 (16 Mbps) bytes
RIFSD AC FC FS
Slide 25 Portions Copyright © 1996, Opus1, Process Software, TGV
Multiplexing andDemultiplexing
ApplicationProcess
ApplicationProcess
ApplicationProcess
TCP UDP
IP
Ethernetinterface
Ethernetinterface
IPX DECnet
Slide 26 Portions Copyright © 1996, Opus1, Process Software, TGV
Segmentation andReassembly
Application DataMessage
TCP
IP
TCP
IP
TCP Segments
IP Packets
Slide 27 Portions Copyright © 1996, Opus1, Process Software, TGV
Addressing identifiesentities in the network
Each layer in a TCP/IP stack has an addressmade up of two parts
The address of everything below itThe address of itself
In some cases, lower-level addresses areimplied and do not have to be stated
For example, Ethernet MAC addresses are tieddirectly to IP addresses, so they can be omited
Slide 28 Portions Copyright © 1996, Opus1, Process Software, TGV
Three addresses areimportant: IP, Protocol, Port
Application-leveladdresses
Transport-leveladdresses
Network-leveladdresses
Slide 29 Portions Copyright © 1996, Opus1, Process Software, TGV
IP addresses are 32-bitintegers
Application-leveladdresses
Transport-leveladdresses
Network-leveladdresses
IP Address: 192.245.12.2
Slide 30 Portions Copyright © 1996, Opus1, Process Software, TGV
Transport addresses are8-bit protocol numbers
Application-leveladdresses
Transport-leveladdresses
Network-leveladdresses
ProtocolNumber:
TCP = 6
UDP=17
Slide 31 Portions Copyright © 1996, Opus1, Process Software, TGV
Port Numbers are16-bit integers
Application-leveladdresses
Transport-leveladdresses
Network-leveladdresses
PortNumber:
SMTP = 25
Slide 32 Portions Copyright © 1996, Opus1, Process Software, TGV
The triplet (IP,Proto,Port)identifies a process
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
versionheader length total length (in 8-bit bytes)
protocol id
source IP address
destination IP address
IP number(network address)
Protocol number(transport address)
Port number(application address)
Slide 33 Portions Copyright © 1996, Opus1, Process Software, TGV
Two triplets identify aconnection
Now we have a socket!(sometimes called a “socket pair”)
Application number 1117
on protocol number 6 (TCP)
at IP address 192.245.12.2
Application number 25
on protocol number 6 (TCP)
at IP address 128.196.128.233
Slide 34 Portions Copyright © 1996, Opus1, Process Software, TGV
A socket maps oneapplication to another
ApplicationProcess
ApplicationProcess
TCP UDP
IP
Ethernetinterface
ApplicationProcess
TCP UDP
IP
Ethernetinterface
1117 25
6 6
192.245.12.2 128.196.128.233
Slide 35 Portions Copyright © 1996, Opus1, Process Software, TGV
Addresses are assigned bydifferent entities
IP addresses are assigned by the InterNICand are managed by the local networkmanagerProtocol numbers are assigned by the IANA(Internet Assigned Number Authority) andare fixed across the Internet (RFC 1700)Application port numbers come from twosources
Servers are assigned by IANA (RFC 1700)Clients are handled by the network kernel
Slide 36 Portions Copyright © 1996, Opus1, Process Software, TGV
IP Addressing
IP is the Internet Protocol, currently version4IPNG is the Next Generation (also known asIPv6)IP is defined by RFC-791IP uses four octet (8-bit byte) addressesIP takes care of getting packets todestination
Slide 37 Portions Copyright © 1996, Opus1, Process Software, TGV
Client and Server PortNumbers are coordinated
NORMALLY, Server port numbers are lownumbers in the range 1-1023
Defined in RFC 1700NORMALLY, Client port numbers are highnumbers starting at 1024
These are called “ephemeral ports”
Slide 38 Portions Copyright © 1996, Opus1, Process Software, TGV
You only pick server ports
A server running on a “well-known port” letsthe operating system know what port itwants to listen onA client simply lets the operating system picka new port (ephemeral) that isn’t already inuse
Slide 39 Portions Copyright © 1996, Opus1, Process Software, TGV
Well-Known-Servers
Public services (e.g. and email server) areassigned a particular number by IANAThese numbers are stored in the InternetAssigned Numbers RFC (changing number,latest is 1797)These are called Well-Known-ServersExamples of these include TELNET (23),SMTP (25), FINGER (79), HTTP (80),RLOGIN (512) and others
Important TermsKey Concepts
TCP/IP networks are layeredTCP/IP uses a client/server paradigmAddressing triples (IP, protocol, port) identifyapplications in TCP/IPA pair of triples beats a full house
Client/Server in TCP/IPClient/Server in TCP/IP
Slide 42 Portions Copyright © 1996, Opus1, Process Software, TGV
Client/Server in TCP/IPOverview
What is the server process (INETD)?What are ephemeral ports?How can you have multiple connections?How are processes identified?What is Connection-oriented? Connection-less?
Slide 43 Portions Copyright © 1996, Opus1, Process Software, TGV
Making the initialconnection to a server
When a connection enters a TCP/IP system,someone has to handle it
Either a running daemon is waiting (yourapplication)or a running daemon is waiting (InetD)
Trade off efficiency for performanceChoose whichever model you want based onindividual application characteristicsSeldom used? InetdConstantly used? True Daemon
Slide 44 Portions Copyright © 1996, Opus1, Process Software, TGV
Your application can waitfor an incoming connection
This is called the “Daemon” (or Demon)approach
YourApplication
Network Kernel
If anything shouldcome in for Port 25 onTCP, give it to me. I willbe waiting right here.
OK.
Slide 45 Portions Copyright © 1996, Opus1, Process Software, TGV
Or you can have the InetDdo it for you
YourApplication
Network Kernel
Here’s anincoming call.
Port 25? I will start anew process runningYour Application andhand it this incoming call.
InetD
In Multinet, InetD is known as the Master ServerIn TCPware, InetD is part of NETCPIn UCX, InetD is the Auxilliary Server
Slide 46 Portions Copyright © 1996, Opus1, Process Software, TGV
Servers low, Clients highServers:
ports1-1023
Clients:
“ephemeralports”above1023
Slide 47 Portions Copyright © 1996, Opus1, Process Software, TGV
A Server may connect tomultiple clients
Each connection is a pair of triples (IP,protocol, port)The server side may remain the sameacross multiple clientsOf course, the server programmer has tokeep it all straight
Slide 48 Portions Copyright © 1996, Opus1, Process Software, TGV
Socket-pairs leave noambiguity between 2 clients
Client
TCP
IP
Ethernetinterface
SERVER
TCP
IP
Ethernetinterface
1117 25
6 6
192.245.12.2 128.196.128.233
Client
TCP
IP
Ethernetinterface
5555
6
195.240.3.1
Slide 49 Portions Copyright © 1996, Opus1, Process Software, TGV
Applications are known bytheir port numbers
A process is identified by its 16-bit portnumberIf a server uses both TCP and UDP, it willoften use the same port number for bothprotocols
Protocol number means no ambiguityThe concept of low-numbered ports as“privileged” disappeared with PCs
Don’t base your security on port numbers!Some services use a meet-me approach
Slide 50 Portions Copyright © 1996, Opus1, Process Software, TGV
TCP/IP supports CL andCO at transport layer
CO = Connection OrientedTCP“Stream”
CL = ConnectionlessUDP“Datagram”
CL can be provided by pure IPUnusual“Raw”
Slide 51 Portions Copyright © 1996, Opus1, Process Software, TGV
Connection oriented is likea phone call
Connection oriented data communicationsarrived before connectionless
Used primarily over noisy serial lines in originalintention.
not necessarily an issue with TCPTwo stations must establish a connectionbefore data is transmitted.Connection is strictly maintained usingsequence numbers, acknowledgments, retriesand so on.
Slide 52 Portions Copyright © 1996, Opus1, Process Software, TGV
Connect, Transfer,Disconnect
Connect Request
Connect Confirm
Data
Disconnect Request
Disconnect ConfirmNote: This is almostlike how it’s done in
TCP. Take theinternals course for
more info.
Slide 53 Portions Copyright © 1996, Opus1, Process Software, TGV
Connectionless is like apostcard
Connectionless allows data to be transmittedwithout a pre-established connectionbetween two stations.
This type of service flourished with theproliferation of LANs.
LANs tend to have a very low error rate and aconnection need not be established to ensure theintegrity of the data.
This type of service does not provide errorrecovery, flow or congestion control.
upper layer network protocols can accomplish this.It requires less overhead and is implicitly faster.
Slide 54 Portions Copyright © 1996, Opus1, Process Software, TGV
Nike style datacommunications
Data
Note: This is exactlylike how it’s done in
UDP. Take theinternals course for
more info.
Data
Data
Data
Slide 55 Portions Copyright © 1996, Opus1, Process Software, TGV
Client/Server in TCP/IPKey Concepts
Servers are handled in one of two ways:resident daemons or InetDServer ports are low-numbers; client portsare high-numbered ephemeral portsA server can talk to many clients if theprogrammer can keep them straightTCP is CO; UDP is CL