Application Development with
TCP/IP
Brian S. Mitchell
Drexel University
Agenda
l TCP/IP Application Development Environment
l Client/Server Computing with TCP/IP
l Sockets
l Port Numbers
l The TCP/IP Application Programming Interface (API)
l Example: A Distributed File Viewer
l Java Socket Implementation
TCP/IP Application Development
Environment
l TCP/IP applications are constructed using the sockets interface
l Open standardlExtensions to support the Windows (3.1, 95, NT) environment have been developed by an open committee (Winsock)
Network Media (Wire)
Network Interface Card
Control Header
Data Area IEEE 802.2 LLC FrameDatagram Header Data Area IP Datagram
Datagram
Header Data Area TCP Datagram
TCP/IP Application Development Interface (Sockets)
TCP/IP Applications TCP /IP
Protocol Suite
Client/Server Model with TCP/IP
l TCP/IP provides peer to peer communication between two application processes
l The peer applications can execute on the same or different machinesl Client application initiates the conversation
l Server application accepts the conversation
l Client applications generally request services whereas server applications
generally provide services
l Example: CETS Client
Application
TCP/IP
Interface Network TCP/IP
Interface
Server Application
Application Responsibilities
l Client Applications
l Locate and request a service from a server
l Can run on a single (DOS, Windows 3.x) or a multi-process
(Windows 95, NT, OS2, UNIX, MVS) operating system
l Server Applications
l Server has additional responsibilities beyond providing a service to
a client
lAuthentication - verifying the identity of the client
lAuthorization - determining if a given client is permitted to
access the service that the server supplies
lData Security - guaranteeing that the data that is exchanged
between the client and the server is not revealed or
compromised
Application Responsibilities
(con’t)
lPrivacy - keeping information about an individual (e.g. user
ID and password) from unauthorized access
lProtection - guaranteeing that network applications cannot
abuse system resources
l Requires a multiprocessing operating system to concurrently
support more than one client.
Application Development with
TCP/IP
lChecklist
4IP Address
4Socket
4Port Number
4Socket Verbs (Functions)
Sockets
l A socket is a 16-bit number that is used as an index into a socket
descriptor table
l Each entry in this table references a socket data structure.
l The socket data structure contains all of the information that is
required to setup and exchange data over an IP conversation
l The socket model has many advantages
l Support for different communication protocols (Raw IP, UDP/IP, TCP/IP) l Makes receiving and sending data over a network synonymous with
performing read and write operations on a local file.
37 80 9654 3156 9512
Socket Socket Details
Socket Data Structure Family: PF_INET
Service: SOCK_STREAM (TCP) Local IP: 172.21.100.8
Remote IP: 172.21.100.45 Local Port: 3571
Remote Port: 6358 Socket Descriptor Table
Sockets (con’t)
l When a socket is first created it does not contain enough information
to initiate a network conversation.
l Special socket functions (TCP/IP API’s) must be called in order to populate the socket data structure
37 80 9654 3156 9512
Socket Socket Details
Socket Data Structure Family: ??????????
Service: ??????????
Local IP: ??????????
Remote IP: ??????????
Local Port: ??????????
Remote Port: ??????????
Socket Descriptor Table
Port Numbers
l A port number is a 16-bit integer that represents the logical endpoint of
a conversation
l The mechanism that enables a computer to support more than one IP
conversation at a time
l Port numbers can (and most likely will) be different for the same
conversation on the client and server computers
172.21.100.45
172.21.100.7 Program 1, 172.21.100.7, Port 52
Program 2, 172.21.100.7 Port 87 . . .
Program n, 172.21.56.2 Port 382
LAN
Program 1, 172.21.100.45 Port 97 Program 2, 172.21.100.45 Port 156
Program 1, 172.21.100.45, Port 1041
172.21.56.2
Port Numbers (con’t)
l A Client application can obtain the server application port number in
one of two ways
l Must know the port number in advance
lProvided by the server’s documentation
l Can determine the port number dynamically by sending a message to the port mapper service
lAll services that want their port number to be found by name must register (and deregister) with the port mapper service
lPort number is obtained by calling the getportbyname (...) API Client
Application
Port Mapper Service Phone Directory Service
Port 158
Port Database The phone directory service is
listening on port number 158
Connect 172.21.100.87:158
What port is handling the phone directory?
TCP/IP Client/Server
Conversation
l TCP/IP socket programs are controlled by a series of API’s:
l Socket, connect, accept, read, write, close, listen, bind, accept Create a socket - socket( )
Connect to the server - connect( ) Send data to the server - write( ) Receive data from the server - read( ) Terminate the conversation - close( )
socket( ) - create a socket
bind( ) - bind program to a port number listen( ) - get ready to accept client requests accept( ) - wait for a client request
read( ) - Receive data from the client write( ) - Send data to the client
close( ) - Terminate the conversation
Client Program Server Program
Supporting concurrent clients
with Socket Servers
l The server creates a new process or thread to handle the clients request
l Client connection is serviced by the new socket that was returned by the accept( ) call l The original socket is freed up to handle another client request
Multiprocessing Socket Server s1 = socket ()
bind (s1, port_number) listen (s1)
s2 = accept (s1)
create_process(client_handler, s2)
// Create a socket
// Bind the socket to a port
// Place the socket into a listening mode // Wait for a client connection
accept( ) returns a new socket to handle the client connection // Create a new process to service the // client request
client_handler (client_socket) read(client_socket)
write(client_socket) close(client_socket)
Example: A Distributed File
Viewer Service
l The distributed file service will consist of the following components
l Client ProgramlConnect to the remote server program lRequest a file by name
lReceive an image of the actual file from the server program lDisplay the file image for the user to view
l Server Program
lWait for a client request
lRead a file from the local hard disk based on the client request lSend an image of the file to the remote client
Client Program
Server Program
Hard Disk Get me file ‘c:\test.txt’
Image of the ‘c:\test.txt’ file
Example: A Distributed File
Server (con’t)
l Read the file ‘c:\test.txt’ on the server given
l Client IP = 172.21.100.7l Server IP = 172.21.100.45
l File read service port number = 1031 Client
Program TCP/IP Conversation
Server Program Port 1031 171.21.100.45
Hard Disk
Client socket ( )
connect (172.21.100.45,1031) write (‘c:\test.txt’)
read (data-area) close ( )
display_data(data_area)
Server socket () bind (1031) listen ( ) accept ( )
read (file_name)
read_file_from_disk (file_name, data_area) write (data_area)
close () 171.21.100.7