• No results found

Application Development with TCP/IP. Brian S. Mitchell Drexel University

N/A
N/A
Protected

Academic year: 2021

Share "Application Development with TCP/IP. Brian S. Mitchell Drexel University"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)

Application Development with

TCP/IP

Brian S. Mitchell

Drexel University

(2)

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

(3)

TCP/IP Application Development

Environment

l TCP/IP applications are constructed using the sockets interface

l Open standard

lExtensions 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 Frame

Datagram Header Data Area IP Datagram

Datagram

Header Data Area TCP Datagram

TCP/IP Application Development Interface (Sockets)

TCP/IP Applications TCP /IP

Protocol Suite

(4)

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 machines

l 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

(5)

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

(6)

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.

(7)

Application Development with

TCP/IP

lChecklist

4IP Address

4Socket

4Port Number

4Socket Verbs (Functions)

(8)

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

(9)

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

(10)

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

(11)

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?

(12)

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

(13)

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)

(14)

Example: A Distributed File

Viewer Service

l The distributed file service will consist of the following components

l Client Program

lConnect 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

(15)

Example: A Distributed File

Server (con’t)

l Read the file ‘c:\test.txt’ on the server given

l Client IP = 172.21.100.7

l 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

(16)

Sockets in Java

l The java.net package provides several classes that enable

socket-based communication in Java

l The Java socket classes are:

l java.net.Socket

l java.net.ServerSocket

l java.net.DatagramSocket

l java.net.SocketImpl

l See the Java documentation for details on these classes

(17)

Parameters in Java Class

Constructors

l String host: String host can be specified either as machine

name or IP address. For queen.mcs.drexel.edu, machine

name 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 of

the machine if the machine is connected to the network.

l int port: integer port number

l boolean stream: It specify whether communication

through the socket based on TCP protocol, or UDP

datagram protocol. The defaulat protocol is TCP.

(18)

Parameters in Java Class

Constructors (con’t)

l InetAddress address: InetAddress is the class that

represents an Internet Protocol(IP) address. It does not

have a constructor, but instead it has several methods

which return the InetAddress objects. Method

getLocalHost() returns an InetAddress for the local host.

GetAllByName() returns an array of InetAddress that

represents all of the available addresses for a host specified

by name; getByName() returns the InetAddress of a host

specified by name. The InetAddress class definition is

shown on the next slide

(19)

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();

}

(20)

Implementing A Server

l Create a socket, use the ServerSocket class

l ServerSocket s = new ServerSocket(4444)

l Socket listens to port 4444

l Accept a connection request by a client

l Socket client_socket=s.accept();

l Creates a socket that communicates with the client

l Open the input and output stream to/from the client

l DataInputStream in = new

DataInputStream(client_socket.getInputStream());

l PrintStream out = new

PrintStream(client_socket.getOutputStream());

(21)

Implementing a Server

l Close the sockets

l s.close();

l client_socket.close();

l in.close();

l out.close();

l Closing the sockets and streams terminates the session

between the server process and the client process

(22)

Implementing a Client

l Create a socket

l Specify server host name or IP address and port number

l 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

l Open the input and output stream on the socket

l DataInputStream in = new DataInputStream(c.getInputStream());

// get input stream

l PrintStream out = new PrintStream(c.getOutputStream());

// get output stream

(23)

Implementing the Client

l Close the sockets

l in.close(); // close the input stream

l out.close(); // close the output stream

l c.close(); // close the client socket

(24)

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());

...

(25)

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;

}

}...

(26)

Sample Server

client_socket.close();

}

catch (Exception e)

{

System.out.println(e);

}

}

} //end of class EchoServer

(27)

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);

}...

(28)

Sample Client

}

catch (IOException e) {

System.out.println("Error" + e);

}

} //end of class EchoClient

References

Related documents