+ All Categories
Home > Documents > Networking in Java

Networking in Java

Date post: 20-Mar-2016
Category:
Upload: ondrea
View: 56 times
Download: 1 times
Share this document with a friend
Description:
Networking in Java. Representation and Management of Data on the Internet. Client-Server Model. Port 80. Client application. 132.65.32.29. Server application. 64.208.34.100. www.google.com. Client application. 132.68.7.11. Clients. Client - initiates connection retrieves data - PowerPoint PPT Presentation
55
Networking in Networking in Java Java Representation and Management of Data on the Internet
Transcript
Page 1: Networking in Java

Networking in JavaNetworking in JavaRepresentation and Management of

Data on the Internet

Page 2: Networking in Java

Client-Server ModelClient-Server Model

64.208.34.100

132.68.7.11

132.65.32.29

Port 80

Server application

Client application

Client applicationwww.google.com

Page 3: Networking in Java

ClientsClients• Client - initiates connection

– retrieves data – displays data – responds to user input – requests more data

• Examples:– Web Browser– Chat Program– PC accessing files

Page 4: Networking in Java

ServersServers

• Server - responds to connection– receives request for data– looks it up– delivers it

• Examples:– Web Server– Database Server– Domain Name Server

Page 5: Networking in Java

Networking BasicsNetworking Basics

• Communication layers:

Link (device, driver,…)

Network (IP)

Transport (TCP, UDF)

Our ApplicationsApplication (HTTP, FTP, Telnet)

Page 6: Networking in Java

Internet Architecture ModelInternet Architecture Model

Application Layer DATA

Transport Layer HEADER DATA

Internet Layer HEADER HEADER DATA

Network Layer HEADER HEADER HEADER DATA

Page 7: Networking in Java

TCP and UDPTCP and UDP• TCP (Transmission Control Protocol)

– connection-based protocol – provides a reliable flow of data between two computers

• UDP (User Datagram Protocol) – a protocol that sends independent packets of data,

called datagrams, from one computer to another – arrival of datagrams is not gaurantees– UDP is not connection-based like TCP

Page 8: Networking in Java

How to Choose TCP or UDPHow to Choose TCP or UDP• Use TCP when reliability is critical:

– HTTP– FTP– Telnet

• Use UDP when reliability is not critical:– Ping– Clock– Audio and Video transmission– NFS – Network File System

Page 9: Networking in Java

Host and PortHost and Port

• Destination in the Internet is identified by host + port

– a 32 bits IP-address – a 16 bits port

• Q: Why don’t we specify the port in a Web browser?

• Ports 0-1023 are restricted– Do not use them

Page 10: Networking in Java

Destination of a ConnectionDestination of a Connection

• Q: How does an HTTP request ‘knows’ where is the server to which it is intended?

• Q: How does an HTTP response ‘knows’ where is the server to which it is intended?

• Q: There can be more then one application (ever some servers) running on the same host, how do we know to which application a message is intended?

Page 11: Networking in Java

A Connection to Many A Connection to Many ApplicationsApplications

• Q: There can be many applications that are on the same time connected to the same host, (for example, many browser and one search engine)

How do we send the right transmission to eachclient?

• A: By adding the IP address of the client and the port of the client to the IP packets

Page 12: Networking in Java

Known PortsKnown Ports

• Some known ports are– 20, 21: FTP– 23: telnet– 25: SMTP– 43: whois– 80: HTTP– 119: NNTP

21 23 25 43 80 119

Client Application

web browsermail client

Page 13: Networking in Java

Internet AddressesInternet Addresses• InetAddress – a final class that represents Internet

Protocol (IP) addresses and the names of hosts • Getting the InetAdddress

– getLocalHost• Returns the local host

– getByName(String host)• For the given host name one of its IP addresses is

returned– getAllByName(String host)

• For a given host name all its IP addresses are returned

Page 14: Networking in Java

Methods of InetAddressMethods of InetAddress

• getHostAddress– Returns the IP address of the host– The address is in the form “%d.%d.%d.%d”

• getHostName– Returns the name of the host

Page 15: Networking in Java

ProtocolHost Name

Port Number

File Name

Reference

Working with URLsWorking with URLs

• URL (Uniform Resource Locator) - a reference (an address) to a resource on the Internet

http://www.cs.huji.ac.il:80/~dbi/main.html#notes

Page 16: Networking in Java

Creating URLsCreating URLs

• The class URL is defined in the packagejava.net

• Basic constructor:URL w3c = new URL("http://www.w3.org/");

• Relative links:– Are created from baseURL + relativeURL

URL amaya = new URL(w3c, “Amaya/Amaya.html”); URL jigsaw = new URL(w3c, “Jigsaw/#Getting”);

Page 17: Networking in Java

Creating URLs (cont.)Creating URLs (cont.)• The following two are equivalent:URL dbiNotes = new

URL(“http://www.cs.huji.ac.il:80/” + ”~dbi/main.html#notes”);

URL dbiNotes = new URL(“http”, “www.cs.huji.ac.il”, 80, “~dbi/main.html#notes”);

• Construction of URLs can throw MalformedURLException

Page 18: Networking in Java

Why Do We Need the URL Why Do We Need the URL ClassClass??

• The main usage of URL is for parsing URLs– getting the protocol– getting the host– getting the port– getting the file name– getting the reference

• An example of parsing a URL

Page 19: Networking in Java

Running the ExampleRunning the Example

Page 20: Networking in Java

Running the ExampleRunning the ExampleJava URLInfo http://www.cs.huji.ac.il

Protocol: httpHost: www.cs.huji.ac.ilFile name:

Port: -1Reference: null

Java URLInfo http://www.cs.huji.ac.il:80/~dbi/main.html#notes

Protocol: httpHost: www.cs.huji.ac.ilFile name: /~dbi/main.htmlPort: 80Reference: notes

Page 21: Networking in Java

Reading From A URLReading From A URL

url

InputStreamReader

openStream

BufferedReader

readLine

Page Content

Page 22: Networking in Java

Reading From a URLReading From a URL• Example of reading from a given URL:

UrlReader.java

• For reading a URL using a proxy, we do the following:

Java–Dhttp.proxyHost=wwwproxy.cs.huji.ac.il–Dhttp.proxyPort=8080 UrlReader url

Page 23: Networking in Java

ReadingReading… …

Java UrlReader url

The content of the file

Page 24: Networking in Java

Connecting to A URLConnecting to A URLurl

InputStreamReader

getInputStream

BufferedReader

URLConnection

openConnection

PrintWriter

getOutputStream

Page

println

readLine

Page 25: Networking in Java

Interacting with a CGI scriptInteracting with a CGI script

1. Create a URL2. Open a connection to the URL 3. Set output capability on the URLConnection 4. Get an output stream from the connection

• This output stream is connected to the standard input stream of the cgi-bin script on the server

5. Write to the output stream 6. Close the output stream

Page 26: Networking in Java

HTTP connectionsHTTP connections• You can create a connection to an HTTP

server with an objectHttpURLConnection

• This object extends the URLConnection object– getResponseCode– getResponseMessage– setRequestMethod

• Look in the Java API

HTTP/1.1 200 OK

<HTML> …</HTML>

Page 27: Networking in Java

URLEncoderURLEncoder• contains a utility method encode for converting

a string into an encoded format• To convert a string, each character is examined in

turn: – The ASCII characters 'a' – 'z', 'A' – 'Z', '0' – '9', ".", "-",

"*", "_" remain the same – Space is converted into a plus sign '+'– All other characters are converted into the 3-character

string "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits of the character

Page 28: Networking in Java

URL Connection ExampleURL Connection Example

• The next example connects to a CGI script on www.walla.co.il – a search tool is given a word to search

SearchWalla

Page 29: Networking in Java

SocketsSockets• Communication is between sockets• A socket is one end-point of a two-way

communication link between two programs running on the network

• A socket has a binding to a port to which it listens

• A socket implementation gives us the ability to read from it and write to it as if it is a file

Page 30: Networking in Java

In SocketsIn Sockets

• A socket ‘knows’ the followings– The port number of the remote host– The host name (InetAddress) of the remote host– The local port to which it is bound– The local address of its local host

Page 31: Networking in Java

SocketsSockets

• The server has a socket that listen to a known port, e.g., Web server and port 80

• The server waits (listening to the socket) till a client requests a connection

• A client requests a connection on the known host and port of the server

• Q: What should the server do so that it can serve other clients as well?

Page 32: Networking in Java

Client Requests for a ConnectionClient Requests for a Connection

64.208.34.100 132.68.7.11

132.65.32.29

Port 80

Server application

Client application

Client application

www.google.com

Port 8090

Port 8888

Port: 8090Local Port: 80Local host: www.google.comRemote host: 132.65.32.29

Port: 8888Local Port: 80Local host: www.google.comRemote host: 132.68.7.11

Page 33: Networking in Java

SocketSocket

• Class Socket – implements the client side of the connection

• Class ServerSocket – implements the server side of the connection

Page 34: Networking in Java

Using a SocketUsing a Socket

InputStreamReader

getInputStream

BufferedReader

readLine

Page Content

Sockethost

port

Page 35: Networking in Java

Using a SocketUsing a Socket// Constructors (partial list)public Socket()public Socket(InetAddress address, int port);public Socket(String host, int port);

// Methods (partial list)public void close();

public InetAddress getInetAddress(); public int getLocalPort();

public InputStream getInputStream();public OutputStream getOutputStream();

public int getPort(); public String toString();

Page 36: Networking in Java

Using a Socket (client)Using a Socket (client)

• A client:1.Opens a socket (with a binding to which port?)2.Opens an input stream and output stream to the

socket3.Reads from and write to the stream according to

the client’s protocol 4.Closes the streams5.Closes the socket

Page 37: Networking in Java

Using a Socket (cont.)Using a Socket (cont.)

• A server:1. Open a socket2. Open an input stream and output stream to the

socket3. Read from and write to the stream according

to the server's protocol 4. Close the streams5. Close the socket

Page 38: Networking in Java

A Client ExampleA Client Example

• The following is a client that connects to a Time Server (port 13) and returns the curret time

TimeClient

Page 39: Networking in Java

Running the TimeClientRunning the TimeClient

java TimeClient

Tue Mar 13 12:42:09 2001

Page 40: Networking in Java

ServerSocketServerSocket// Constructors (partial list)

public ServerSocket(int port);public ServerSocket(int port, int count);

// Methods (partial list)

public Socket accept();public void close();

public InetAddress getInetAddress();public int getLocalPort();

public String toString();

What happens when arunning program reachesaccept()?

The size or requestsqueue with defaultof 50 requests

Why don’t we have getPort in addition to getLocalPort?

Page 41: Networking in Java

A Server ExampleA Server Example

• The following is a time server that returns the time:

TimeServer

• Note: When the ServerSocket constructor is given port number 0, a random free port is chosen

Page 42: Networking in Java

More on Server SocketMore on Server Socket• A ServerSocket waits for requests to come in

over the network • It performs some operation based on that request,

and then possibly returns a result to the requester • The actual work of the ServerSocket is performed

by an instance of the SocketImpl class• The abstract class SocketImpl is a common

superclass of all classes that actually implement sockets

• It is used to create both client and server sockets

Page 43: Networking in Java
Page 44: Networking in Java

Server sideServer sideimport java.net.*;import java.io.*;

// A server that says 'hello'class HelloServer {

public static void main(String[] args) { int port = Integer.parseInt(args[0]); ServerSocket server = null; try { server = new ServerSocket(port); } catch (IOException ioe) { System.err.println("Couldn't run"+ "server on port "+port); return; }

Page 45: Networking in Java

Server sideServer side while(true) { try { Socket connection = server.accept(); BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); PrintWriter writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream())); String clientName = reader.readLine(); writer.println("Hello "+clientName); writer.flush(); } catch (IOException ioe1) {} }

Page 46: Networking in Java

Client sideClient sideimport java.net.*;import java.io.*;

// A client of an HelloServerclass HelloClient {

public static void main(String[] args) { String hostname = args[0]; int port = Integer.parseInt(args[1]); Socket connection = null; try { connection = new Socket(hostname, port); } catch (IOException ioe) { System.err.println("Connection failed");

return; }

Page 47: Networking in Java

Client sideClient side try { BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); PrintWriter writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream())); writer.println(args[2]); // client name String reply = reader.readLine(); System.out.println("Server reply: "+reply); writer.flush(); } catch (IOException ioe1) { }}

Page 48: Networking in Java

DatagramsDatagrams• Datagram packets are used to implement a

connectionless, packet based, delivery service• Each message is routed from one machine to

another based solely on information contained within that packet

• Multiple packets sent from one machine to another might be routed differently, and might arrive in any order

• Packets may be lost or duplicated during transit• The class DatagramPacket represents a datagram

in Java

Page 49: Networking in Java

DatagramPacket ClassDatagramPacket Class//Constructorspublic DatagramPacket(byte ibuf[], int ilength);public DatagramPacket( byte ibuf[], int ilength, InetAddress iaddr, int

iport);

// Methodspublic synchronized InetAddress getAddress();public synchronized int getPort();public synchornized byte[] getData();int getLength();

void setAddress(InetAddress iaddr);void setPort(int iport);void setData(byte ibuf[]);void setLength(int ilength);

Page 50: Networking in Java

DatagramSocketDatagramSocket• This class represents a socket for sending and receiving

datagram packets• Addressing information for outgoing packets is contained

in the packet header• A socket that is used to read incoming packets must be

bound to an address (sockets that are used for sending must be bound as well, but in most cases it is done automatically)

• There is no special datagram server socket class• Since packets can be lost, the ability to set timeouts is

important

Page 51: Networking in Java

Class DatagramSocketClass DatagramSocket// ConstructorsDatagramSocket() DatagramSocket(int port)DatagramSocket(int port, InetAddress iaddr) // Methodsvoid close() InetAddress getLocalAddress() int getLocalPort() int getSoTimeout() void receive(DatagramPacket p) void send(DatagramPacket p) setSoTimeout(int timeout)

Page 52: Networking in Java

Echo ServersEcho Servers

• A common network service is an echo server

• An echo server simply sends packets back to the sender

• A client creates a packet, sends it to the server, and waits for a response

• Echo services can be used to test network connectivity and performance

Page 53: Networking in Java

Echo Client ExampleEcho Client Exampleimport java.net.*; import java.io.*; import java.util.*;

public class EchoClient { static int echoPort = 7; static int msgLen = 16; static int timeOut=1000;

public static void main(String argv[]) { try { DatagramSocket socket = new DatagramSocket(); DatagramPacket packet; byte msg[] = new byte[msgLen];

InetAddress echoHost = InetAddress.getByName(argv[0]); packet = new DatagramPacket(msg,msgLen,echoHost,echoPort);

socket.send(packet); socket.setSoTimeout(timeOut); socket.receive(packet); } catch (InterruptedIOException e) {System.out.println("Timeout");} catch (Exception e) {}}}

Page 54: Networking in Java

EchoServerEchoServerimport java.net.*;import java.io.*;import java.util.*;

public class EchoServer { static int echoPort = 7000; static int msgLen = 1024; public static void main(String args[]) { try { DatagramSocket socket = new DatagramSocket(echoPort); DatagramPacket p,reply; byte msg[] = new byte[msgLen]; packet = new DatagramPacket(msg,msgLen);

for (;;) { sock.receive(p); System.out.println(p.getAddress()); reply = new DatagramPacket(p.getData(),p.getLength(),p.getAddress(),p.getPort()); socket.send(reply); } } catch (Exception e) {} }}

Page 55: Networking in Java

Java Net ClassesJava Net ClassesClassDescriptionDatagramPacketThis class represents a datagram packet.

DatagramSocketThis class represents a socket for sending and receiving datagram packets.

InetAddressThis class represents an Internet Protocol (IP) address.

MulticastSocketThe multicast datagram socket class is useful for sending and receiving IP multicast packets.

ServerSocketThis class implements server sockets.

Socket This class implements client sockets (also called just "sockets").

URLA pointer to a "resource" on the World Wide Web.

URLConnectionThe superclass of all classes that represent a communications link between an application and a URL.


Recommended