Application Development with TCP/IP - Computer Sciencebmitchell/course/mcs721/tcpipad.pdf · TCP/IP...
Transcript of Application Development with TCP/IP - Computer Sciencebmitchell/course/mcs721/tcpipad.pdf · TCP/IP...
Agenda
l TCP/IP Application Development EnvironmentlClient/Server Computing with TCP/IPl Socketsl Port Numbersl The TCP/IP Application Programming Interface (API)l Example: A Distributed File Viewerl Java Socket Implementation
TCP/IP Application DevelopmentEnvironment
l TCP/IP applications are constructed using the sockets interfacel Open standard
lExtensions to support the Windows (3.1, 95, NT) environment havebeen developed by an open committee (Winsock)
Network Media (Wire)
Network Interface Card
Control Header Data Area IEEE 802.2 LLC Frame
Datagram Header Data Area IP Datagram
DatagramHeader Data Area TCP Datagram
TCP/IP Application DevelopmentInterface (Sockets)
TCP/IP Applications
TCP /IPProtocolSuite
Client/Server Model with TCP/IP
l TCP/IP provides peer to peer communication between two application processesl The peer applications can execute on the same or different machines
l Client application initiates the conversationl Server application accepts the conversationl Client applications generally request services whereas server applications
generally provide servicesl Example: CETS
Client Application
TCP/IPInterface Network
TCP/IP Interface
Server Application
Application Responsibilities
lClient Applicationsl Locate and request a service from a serverl Can run on a single (DOS, Windows 3.x) or a multi-process
(Windows 95, NT, OS2, UNIX, MVS) operating system
l Server Applicationsl Server has additional responsibilities beyond providing a service to
a clientlAuthentication - verifying the identity of the clientlAuthorization - determining if a given client is permitted to
access the service that the server supplieslData Security - guaranteeing that the data that is exchanged
between the client and the server is not revealed orcompromised
Application Responsibilities(con’t)
lPrivacy - keeping information about an individual (e.g. userID and password) from unauthorized accesslProtection - guaranteeing that network applications cannot
abuse system resourcesl Requires a multiprocessing operating system to concurrently
support more than one client.
Application Development withTCP/IP
lChecklist4IP Address4Socket4Port Number4Socket Verbs (Functions)
Sockets
l A socket is a 16-bit number that is used as an index into a socketdescriptor tablel Each entry in this table references a socket data structure.
l The socket data structure contains all of the information that isrequired to setup and exchange data over an IP conversation
l The socket model has many advantagesl 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.
3780
965431569512
Socket Socket Details
Socket Data Structure
Family: PF_INETService: SOCK_STREAM (TCP)Local IP: 172.21.100.8Remote IP: 172.21.100.45Local Port: 3571Remote Port: 6358
Socket Descriptor Table
Sockets (con’t)
l When a socket is first created it does not contain enough informationto initiate a network conversation.l Special socket functions (TCP/IP API’s) must be called in order to
populate the socket data structure
3780
965431569512
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 ofa conversation
l The mechanism that enables a computer to support more than one IPconversation at a time
l Port numbers can (and most likely will) be different for the sameconversation on the client and server computers
172.21.100.45
172.21.100.7
Program 1, 172.21.100.7, Port 52Program 2, 172.21.100.7 Port 87 . . .Program n, 172.21.56.2 Port 382
LAN
Program 1, 172.21.100.45 Port 97Program 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 inone of two waysl Must know the port number in advance
lProvided by the server’s documentationl Can determine the port number dynamically by sending a message to the
port mapper servicelAll services that want their port number to be found by name must
register (and deregister) with the port mapper servicelPort number is obtained by calling the getportbyname (...) API
ClientApplication
Port Mapper Service
Phone Directory Service Port 158
PortDatabase
The phone directory service islistening on port number 158
Connect 172.21.100.87:158
What port is handling the phone directory?
TCP/IP Client/ServerConversation
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 socketbind( ) - bind program to a port numberlisten( ) - get ready to accept client requestsaccept( ) - wait for a client requestread( ) - Receive data from the clientwrite( ) - Send data to the clientclose( ) - Terminate the conversation
Client Program Server Program
Supporting concurrent clientswith Socket Servers
l The server creates a new process or thread to handle the clients requestl Client connection is serviced by the new socket that was returned by the accept( ) calll 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 tohandle 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 FileViewer Service
l The distributed file service will consist of the following componentsl Client Program
lConnect to the remote server programlRequest a file by namelReceive an image of the actual file from the server programlDisplay the file image for the user to view
l Server ProgramlWait for a client requestlRead a file from the local hard disk based on the client requestlSend an image of the file to the remote client
Client Program
ServerProgram
HardDisk
Get me file ‘c:\test.txt’
Image of the ‘c:\test.txt’ file
Example: A Distributed FileServer (con’t)
l Read the file ‘c:\test.txt’ on the server givenl Client IP = 172.21.100.7l Server IP = 172.21.100.45l File read service port number = 1031
ClientProgram TCP/IP Conversation
Server ProgramPort 1031
171.21.100.45
Hard Disk
Clientsocket ( )connect (172.21.100.45,1031)write (‘c:\test.txt’)read (data-area)close ( )display_data(data_area)
Serversocket ()bind (1031)listen ( )accept ( )read (file_name)read_file_from_disk (file_name, data_area)write (data_area)close ()
171.21.100.7
Sockets in Java
l The java.net package provides several classes that enablesocket-based communication in Java
l The Java socket classes are:l java.net.Socketl java.net.ServerSocketl java.net.DatagramSocketl java.net.SocketImpl
l See the Java documentation for details on these classes
Parameters in Java ClassConstructors
l String host: String host can be specified either as machinename or IP address. For queen.mcs.drexel.edu, machinename is ”queen.mcs.drexel.edu", IP address is"129.25.6.176". In unix, if you know the machine name,then you can use "nslookup" to identify the IP address ofthe machine if the machine is connected to the network.
l int port: integer port numberl boolean stream: It specify whether communication
through the socket based on TCP protocol, or UDPdatagram protocol. The defaulat protocol is TCP.
Parameters in Java ClassConstructors (con’t)
l InetAddress address: InetAddress is the class thatrepresents an Internet Protocol(IP) address. It does nothave a constructor, but instead it has several methodswhich return the InetAddress objects. MethodgetLocalHost() returns an InetAddress for the local host.GetAllByName() returns an array of InetAddress thatrepresents all of the available addresses for a host specifiedby name; getByName() returns the InetAddress of a hostspecified by name. The InetAddress class definition isshown on the next slide
The InetAddress Class
public final class InetAddress extends Object { // Methods public boolean equals(Object obj); public byte[] getAddress(); public static InetAddress[] getAllByName(String host); public static InetAddress getByName(String host); public String getHostName(); public static InetAddress getLocalHost(); public int hashCode(); public String toString();}
Implementing A Server
lCreate a socket, use the ServerSocket classl ServerSocket s = new ServerSocket(4444)l Socket listens to port 4444
lAccept a connection request by a clientl Socket client_socket=s.accept();l Creates a socket that communicates with the client
lOpen the input and output stream to/from the clientlDataInputStream in = new
DataInputStream(client_socket.getInputStream());l PrintStream out = new
PrintStream(client_socket.getOutputStream());
Implementing a Server
lClose the socketsl s.close();l client_socket.close();l in.close();l out.close();
lClosing the sockets and streams terminates the sessionbetween the server process and the client process
Implementing a Client
lCreate a socketl Specify server host name or IP address and port numberl Socket c= new Socket(”queen.mcs.drexel.edu", 4444)l If you only have a single machine you can use the “127.0.0.1”
local loopback address
lOpen the input and output stream on the socketlDataInputStream in = new DataInputStream(c.getInputStream());
// get input streaml PrintStream out = new PrintStream(c.getOutputStream());
// get output stream
Implementing the Client
lClose the socketsl in.close(); // close the input streaml out.close(); // close the output streaml c.close(); // close the client socket
Sample Server
class EchoServer {
public static void main(String[] args) {
try { ServerSocket s = new ServerSocket(4444); Socket client_socket=s.accept(); DataInputStream in = new DataInputStream
(client_socket.getInputStream()); PrintStream out = new PrintStream
(client_socket.getOutputStream()); ...
Sample Server
…out.println("Hello! Enter BYE to exit. \r"); boolean done = false;
while (!done) { String str = in.readLine(); if (str == null)
done = true; else {
out.println("Echo: " + str + "\r"); if (str.trim().equals("BYE"))
done = true; }
}...
Sample Server
client_socket.close(); } catch (Exception e) {
System.out.println(e); }
}} //end of class EchoServer
Sample Client
class EchoClient { public static void main(String[] args) {
try { Socket c = new Socket(”queen.mcs.drexel.edu", 4444); DataInputStream in = new DataInputStream(c.getInputStream()); bolean more = true; while (more) {
String str = in.readLine(); if (str == null)
more = false;else
System.out.println(str); }...