139
Java Network Programming

Java Network Programming. We will learn how Java handles Internet Addresses URLs Sockets Server Sockets UDP

Embed Size (px)

Citation preview

Page 1: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Java Network Programming

Page 2: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

We will learn how Java handles

Internet Addresses URLs Sockets Server Sockets UDP

Page 3: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Some Background

Hosts Internet Addresses Ports Protocols

Page 4: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Network Layers

Page 5: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Hosts

Devices connected to the Internet are called hosts

Most hosts are computers, but hosts also include routers, printers, fax machines, soda machines, bat houses, etc.

Page 6: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Internet addresses

Every host on the Internet is identified by a unique, four-byte Internet Protocol (IP) address.

This is written in dotted quad format like 199.1.32.90 where each byte is an unsigned integer between 0 and 255.

There are about four billion unique IP addresses, but they aren’t very efficiently allocated

Page 7: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Sockets and ports

7

message

agreed portany port socketsocket

Internet address = 138.37.88.249Internet address = 138.37.94.248

other ports

client server

Page 8: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

TCP TCP stream communication A dedicated point-to-point channel Use TCP (Transmission Control Protocol) for data transmission.

– For integrity, checksum is used to detect and reject corrupt packets an sequence numbers to detect and reject duplicate packets. For validity, timeouts and retransmission to deal with lost packets.

– So messages are guaranteed to be delivered. Lossless and reliable. Sent and received in the same order.

Use of TCP: many frequently used services run over TCP connections

HTTP, FTP, Telnet, SMTP

8

Page 9: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

9 CS551 - Lecture 11

Application

Presentation

Session

Transport

Application

Presentation

Session

TransportInput Stream

Output Stream

Requests

Results

Client Server

TCP for Request ImplementationTCP for Request Implementation

Page 10: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

10 CS551 - Lecture 11

Connection-oriented Communication

Page 11: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

UDP UDP datagram communication No dedicated point-to-point channel Use UDP (User Datagram Protocol) for data transmission. No

acknowledge and retries.

– Omission failure as messages may be dropped occasionally either because of checksum error or no buffer space at source or destination

– Messages can be sometimes be delivered out of sender order

– So applications using UDP are left to provide their own checks to achieve quality of reliable communication, which can be constructed by using acknowledgements.

– Applications that are acceptable to have occasional omission failure and out of order. For example, DNS and Voice Over IP (VOIP)

11

Page 12: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

12 CS551 - Lecture 11

Result Datagrams

Application

Presentation

Session

Transport

Application

Presentation

Session

Transport

Request Datagrams

Client Server

UDP for Request ImplementationUDP for Request Implementation

Page 13: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

13 CS551 - Lecture 11

Datagram Communication

Page 14: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

TCP Protocol

Page 15: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

UDP Protocol

Page 16: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The InetAddress Class

16

Occasionally, you would like to know who is connecting to the server. You can use the InetAddress class to find the client's host name and IP address. The InetAddress class models an IP address. You can use the statement shown below to create an instance of InetAddress for the client on a socket. 

InetAddress inetAddress = socket.getInetAddress();

 Next, you can display the client's host name and IP address, as follows:

 System.out.println("Client's host name is " + inetAddress.getHostName());System.out.println("Client's IP Address is " + inetAddress.getHostAddress());

Page 17: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The InetAddress Class

The java.net.InetAddress class represents an IP address.

It converts numeric addresses to host names and host names to numeric addresses.

It is used by other network classes like Socket and ServerSocket to identify hosts

Page 18: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Creating InetAddresses

There are no public InetAddress() constructors. Arbitrary addresses may not be created.

All addresses that are created must be checked with DNS

Page 19: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The getByName() factory method

public static InetAddress getByName(String host) throws UnknownHostException

InetAddress uit, duke;

try {

uit = InetAddress.getByName(“uit.edu.vn");

duke = InetAddress.getByName("128.238.2.92");

}

catch (UnknownHostException e) {

System.err.println(e);

}

Page 20: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Other ways to create InetAddress objects

public static InetAddress[] getAllByName(String host) throws UnknownHostException

public static InetAddress getLocalHost() throws UnknownHostException

Page 21: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Getter Methods

public boolean isMulticastAddress() public String getHostName() public byte[] getAddress() public String getHostAddress()

Page 22: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Datagrams

Before data is sent across the Internet from one host to another using TCP/IP, it is split into packets of varying but finite size called datagrams.

Datagrams range in size from a few dozen bytes to about 60,000 bytes.

Packets larger than this, and often smaller than this, must be split into smaller pieces before they can be transmitted.

Page 23: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Packets Allow Error Correction

If one packet is lost, it can be retransmitted without requiring redelivery of all other packets.

If packets arrive out of order they can be reordered at the receiving end of the connection.

Page 24: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Abstraction

Datagrams are mostly hidden from the Java programmer.

The host's native networking software transparently splits data into packets on the sending end of a connection, and then reassembles packets on the receiving end.

Instead, the Java programmer is presented with a higher level abstraction called a socket.

Page 25: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Sockets

A socket is a reliable connection for the transmission of data between two hosts.

Sockets isolate programmers from the details of packet encodings, lost and retransmitted packets, and packets that arrive out of order.

There are limits. Sockets are more likely to throw IOExceptions than files, for example.

Page 26: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Socket Operations

There are four fundamental operations a socket performs. These are:1. Connect to a remote machine

2. Send data

3. Receive data

4. Close the connection

A socket may not be connected to more than one host at a time.

A socket may not reconnect after it's closed.

Page 27: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

A Simple Example

Page 28: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The java.net.Socket class

The java.net.Socket class allows you to create socket objects that perform all four fundamental socket operations.

You can connect to remote machines; you can send data; you can receive data; you can close the connection.

Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new Socket object.

Page 29: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Constructing a Socket Connection is accomplished through the

constructors.

public Socket(String host, int port) throws UnknownHostException, IOException

public Socket(InetAddress address, int port) throws IOException

public Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException

public Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException

Page 30: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Opening Sockets

The Socket() constructors do not just create a Socket object. They also attempt to connect the underlying socket to the remote server.

All the constructors throw an IOException if the connection can't be made for any reason.

Page 31: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

You must at least specify the remote host and port to connect to.

The host may be specified as either a string like "utopia.poly.edu" or as an InetAddress object.

The port should be an int between 1 and 65535.

Socket webMetalab = new Socket("metalab.unc.edu", 80);

Page 32: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

You cannot just connect to any port on any host. The remote host must actually be listening for connections on that port.

You can use the constructors to determine which ports on a host are listening for connections.

Page 33: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

public static void scan(InetAddress remote) {

String hostname = remote.getHostName();

for (int port = 0; port < 65536; port++) {

try {

Socket s = new Socket(remote, port);

System.out.println("There is a server on port "

+ port + " of " + hostname);

s.close();

}

catch (IOException e) {

// The remote host is not listening on this port

}

}

}

Page 34: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Picking an IP address

The last two constructors also specify the host and port you're connecting from.

On a system with multiple IP addresses, like many web servers, this allows you to pick your network interface and IP address.

Page 35: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Choosing a Local Port You can also specify a local port number, Setting the port to 0 tells the system to randomly

choose an available port. If you need to know the port you're connecting

from, you can always get it with getLocalPort().Socket webMetalab = new Socket("metalab.unc.edu",

80, "calzone.oit.unc.edu", 0);

Page 36: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Create Client

Page 37: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Sending and Receiving Data

Data is sent and received with output and input streams.

There are methods to get an input stream for a socket and an output stream for the socket.public InputStream getInputStream() throws IOException

public OutputStream getOutputStream() throws IOException

There's also a method to close a socket.public synchronized void close() throws IOException

Page 38: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Reading Input from a Socket

The getInputStream() method returns an InputStream which reads data from the socket.

You can use all the normal methods of the InputStream class to read this data.

Most of the time you'll chain the input stream to some other input stream or reader object to more easily handle the data.

Page 39: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example The following code fragment connects to the

daytime server on port 13 of metalab.unc.edu, and displays the data it sends.try {

Socket s = new Socket("metalab.unc.edu", 13);

InputStream in = s.getInputStream();

InputStreamReader isr = new InputStreamReader(in);

BufferedReader br = new BufferedReader(isr);

String theTime = br.readLine();

System.out.println(theTime);

}

catch (IOException e) {

return (new Date()).toString();

}

Page 40: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Writing Output to a Socket

The getOutputStream() method returns an output stream which writes data to the socket.

Most of the time you'll chain the raw output stream to some other output stream or writer class to more easily handle the data.

Page 41: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Discardbyte[] b = new byte[128];try { Socket s = new Socket("metalab.unc.edu", 9); OutputStream theOutput = s.getOutputStream(); while (true) { int n = theInput.available(); if (n > b.length) n = b.length; int m = theInput.read(b, 0, n); if (m == -1) break; theOutput.write(b, 0, n); } s.close();}catch (IOException e) {}

Page 42: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Socket Options Several methods set various socket options.

Most of the time the defaults are fine.public void setTcpNoDelay(boolean on) throws SocketException

public boolean getTcpNoDelay() throws SocketException

public void setSoLinger(boolean on, int val) throws SocketException

public int getSoLinger() throws SocketException

public synchronized void setSoTimeout(int timeout) throws SocketException

public synchronized int getSoTimeout() throws SocketException

Page 43: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

These methods to return information about the socket: public InetAddress getInetAddress()

public InetAddress getLocalAddress()

public int getPort()

public int getLocalPort()

Finally there's the usual toString() method: public String toString()

Page 44: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Servers There are two ends to each connection: the

client, that is the host that initiates the connection, and the server, that is the host that responds to the connection.

Clients and servers are connected by sockets.

A server, rather than connecting to a remote host, a program waits for other hosts to connect to it.

Page 45: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Create Server

Page 46: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Server Sockets

A server socket binds to a particular port on the local machine.

Once it has successfully bound to a port, it listens for incoming connection attempts.

When a server detects a connection attempt, it accepts the connection. This creates a socket between the client and the server over which the client and the server communicate.

Page 47: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Multiple Clients Multiple clients can connect to the same port on

the server at the same time. Incoming data is distinguished by the port to

which it is addressed and the client host and port from which it came.

The server can tell for which service (like http or ftp) the data is intended by inspecting the port.

It can tell which open socket on that service the data is intended for by looking at the client address and port stored with the data.

Page 48: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Queueing

Incoming connections are stored in a queue until the server can accept them.

On most systems the default queue length is between 5 and 50.

Once the queue fills up further incoming connections are refused until space in the queue opens up.

Page 49: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The java.net.ServerSocket Class

The java.net.ServerSocket class represents a server socket.

A ServerSocket object is constructed on a particular local port. Then it calls accept() to listen for incoming connections.

accept() blocks until a connection is detected. Then accept() returns a java.net.Socket object that performs the actual communication with the client.

Page 50: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Constructors

There are three constructors that let you specify the port to bind to, the queue length for incoming connections, and the IP address to bind to:public ServerSocket(int port) throws IOException

public ServerSocket(int port, int backlog) throws IOException

public ServerSocket(int port, int backlog, InetAddress networkInterface) throws IOException

Page 51: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Constructing Server Sockets

Normally you only specify the port you want to listen on, like this: try { ServerSocket ss = new ServerSocket(80); } catch (IOException e) { System.err.println(e); }

Page 52: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

When a ServerSocket object is created, it attempts to bind to the port on the local host given by the port argument.

If another server socket is already listening to the port, then a java.net.BindException, a subclass of IOException, is thrown.

No more than one process or thread can listen to a particular port at a time. This includes non-Java processes or threads.

For example, if there's already an HTTP server running on port 80, you won't be able to bind to port 80.

Page 53: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

On Unix systems (but not Windows or the Mac) your program must be running as root to bind to a port between 1 and 1023.

0 is a special port number. It tells Java to pick an available port.

The getLocalPort() method tells you what port the server socket is listening on. This is useful if the client and the server have already established a separate channel of communication over which the chosen port number can be communicated.

FTP

Page 54: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Expanding the Queue

If you think you aren't going to be processing connections very quickly you may wish to expand the queue when you construct the server socket. For example,

try { ServerSocket httpd = new ServerSocket(80, 50);

}catch (IOException e) { System.err.println(e);}

Page 55: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Choosing an IP address

Many hosts have more than one IP address. By default, a server socket binds to all

available IP addresses on a given port. You can modify that behavior with this

constructor: public ServerSocket(int port, int backlog, InetAddress bindAddr)throws IOException

Page 56: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Example

try { InetAddress ia = InetAddress.getByName("199.1.32.90");

ServerSocket ss = new ServerSocket(80, 50, ia);

}catch (IOException e) { System.err.println(e);}

Page 57: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

On a server with multiple IP addresses, the getInetAddress() method tells you which one this server socket is listening to. public InetAddress getInetAddress()

The getLocalPort() method tells you which port you're listening to.public int getLocalPort()

Page 58: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The accept() and close() methods provide the basic functionality of a server socket.

public Socket accept() throws IOException

public void close() throws IOException

A server socket can’t be reopened after it’s closed

Page 59: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Reading Data with a ServerSocket

ServerSocket objects use their accept() method to connect to a client.

public Socket accept() throws IOException

There are no getInputStream() or getOutputStream() methods for ServerSocket.

accept() returns a Socket object, and its getInputStream() and getOutputStream() methods provide streams.

Page 60: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Example

try { ServerSocket ss = new ServerSocket(2345);

Socket s = ss.accept(); PrintWriter pw = new PrintWriter(s.getOutputStream()); pw.println("Hello There!"); pw.println("Goodbye now."); s.close();}catch (IOException e) { System.err.println(e);}

Page 61: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Better Example

try { ServerSocket ss = new ServerSocket(2345);

Socket s = ss.accept(); PrintWriter pw = new PrintWriter(s.getOutputStream()); pw.print("Hello There!\r\n"); pw.print("Goodbye now.\r\n"); s.close();}catch (IOException e) { System.err.println(e);}

Page 62: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Writing Data to a Clienttry { ServerSocket ss = new ServerSocket(port); while (true) { try { Socket s = ss.accept(); PrintWriter pw = new PrintWriter(s.getOutputStream()); pw.print("Hello " + s.getInetAddress() + " on port " + s.getPort() + "\r\n"); pw.print("This is " + s.getLocalAddress() + " on port " + s.getLocalPort() + "\r\n"); pw.flush(); s.close(); } catch (IOException e) {} }}catch (IOException e) { System.err.println(e); }

Page 63: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Interacting with a Client

More commonly, a server needs to both read a client request and write a response.

Page 64: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Multiple Clients

Page 65: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Threading

No more than one server socket can listen to a particular port at one time.

Since a server may need to handle many connections at once, server programs tend to be heavily multi-threaded.

Generally the server socket passes off the actual processing of connections to a separate thread.

Page 66: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Adding Threading to a Server

It's better to make your server multi-threaded.

There should be a loop which continually accepts new connections.

Rather than handling the connection directly the socket should be passed to a Thread object that handles the connection.

Page 67: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Adding a Thread Pool to a Server

Multi-threading is a good thing but it's still not a perfect solution.

Look at this accept loop:while (true) { try { Socket s = ss.accept(); ThreadedEchoServer tes = new ThreadedEchoServer(s) tes.start();

}catch (IOException e) {}

Page 68: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Every pass through this loop, a new thread gets created. Every time a connection is finished the thread is disposed of.

Spawning a new thread for each connection takes a non-trivial amount of time, especially on a heavily loaded server.

Too many simultaneous threads overload a VM.

Page 69: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Thread Pools

Create a pool of threads when the server launches, store incoming connections in a queue, and have the threads in the pool progressively remove connections from the queue and process them.

The main change you need to make to implement this is to call accept() in the run() method rather than in the main() method.

Page 70: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Setting Server Socket Options

There are three methods to set and get various options. The defaults are generally fine.

public synchronized void setSoTimeout(int timeout) throws SocketException

public synchronized int getSoTimeout() throws IOException

public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException

Page 71: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Utility Methods

Finally, there's the usual toString() method:

public String toString()

Page 72: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

UDP

Unreliable Datagram Protocol Packet Oriented, not stream oriented like

TCP/IP Much faster but no error correction NFS, TFTP, and FSP use UDP/IP Must fit data into packets of about 8K or

less

Page 73: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The UDP Classes

Java's support for UDP is contained in two classes:java.net.DatagramSocket

java.net.DatagramPacket

A datagram socket is used to send and receive datagram packets.

Page 74: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

java.net.DatagramPacket

a wrapper for an array of bytes from which data will be sent or into which data will be received.

also contains the address and port to which the packet will be sent.

Page 75: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

java.net.DatagramSocket

A DatagramSocket object is a local connection to a port that does the sending and receiving.

There is no distinction between a UDP socket and a UDP server socket.

Also unlike TCP sockets, a DatagramSocket can send to multiple, different addresses.

The address to which data goes is stored in the packet, not in the socket.

Page 76: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

UDP ports

Separate from TCP ports. Each computer has 65,536 UDP ports as

well as its 65,536 TCP ports. A server socket can be bound to TCP port

20 at the same time as a datagram socket is bound to UDP port 20.

Page 77: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Two DatagramPacket Constructors

public DatagramPacket(byte[] data, int length)

public DatagramPacket(byte[] data, int length, InetAddress remote, int port)

First is for receiving, second is for sending

Page 78: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example,

String s = "My first UDP Packet";byte[] b = s.getBytes();DatagramPacket dp = new DatagramPacket(b, b.length);

Page 79: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

With a destination:

try { InetAddress metalab = InetAddress.getByName("metalab.unc.edu");

int chargen = 19; String s = "My second UDP Packet"; byte[] b = s.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length, metalab, chargen);

}catch (UnknownHostException e) { System.err.println(e);}

Page 80: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

DatagramPackets are not immutable.

public synchronized void setAddress(InetAddress remote)

public synchronized void setPort(int port) public synchronized void setData(byte[] data)

public synchronized void setLength(int length)

public synchronized InetAddress getAddress()public synchronized int getPort()public synchronized byte[] getData()public synchronized int getLength()

These methods are primarily useful when you're receiving datagrams.

Page 81: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

java.net.DatagramSocket

public DatagramSocket() throws SocketExceptionpublic DatagramSocket(int port) throws SocketException

public DatagramSocket(int port, InetAddress laddr) throws SocketException

The first is for client datagram sockets; that is sockets that send datagrams before receiving any.

The second two are for server datagram sockets since they specify the port and optionally the IP address of the socket

Page 82: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Sending UDP Datagrams

To send data to a particular server– Convert the data into byte array. – Pass this byte array, the length of the data in

the array (most of the time this will be the length of the array) and the InetAddress and port to which you wish to send it into the DatagramPacket() constructor.

– Next create a DatagramSocket and pass the packet to its send() method

Page 83: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example,

InetAddress metalab = InetAddress.getByName("metalab.unc.edu");

int chargen = 19;String s = "My second UDP Packet";byte[] b = s.getBytes();DatagramPacket dp = new DatagramPacket(b, b.length, ia, chargen);

DatagramSocket sender = new DatagramSocket();

sender.send(dp);

Page 84: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Receiving UDP Datagrams

Construct a DatagramSocket object on the port on which you want to listen.

Pass an empty DatagramPacket object to the DatagramSocket's receive() method.

public synchronized void receive(DatagramPacket dp) throws IOException

The calling thread blocks until a datagram is received.

Page 85: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

dp is filled with the data from that datagram. Use getPort() and and getAddress() to

tell where the packet came from, getData() to retrieve the data, and getLength() to see how many bytes were in the data.

If the received packet was too long for the buffer, it's truncated to the length of the buffer.

Length is reset when packet is received

Page 86: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example, try { byte buffer = new byte[65536]; DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

DatagramSocket ds = new DatagramSocket(2134); ds.receive(incoming); byte[] data = incoming.getData(); String s = new String(data, 0, data.getLength()); System.out.println("Port " + incoming.getPort() + " on " + incoming.getAddress() + " sent this message:");

System.out.println(s);}catch (IOException e) { System.err.println(e);}

Page 87: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Receiving Multiple Packets Watch out for strictly decreasing packet sizes

DatagramPacket incoming = new DatagramPacket(new byte[8192], 8192);

DatagramSocket ds = new DatagramSocket(2134);while (true) { try { incoming.setLength(8192); ds.receive(incoming); byte[] data = incoming.getData(); String s = new String(data, 0, data.getLength()); System.out.println("Port " + incoming.getPort() + " on " + incoming.getAddress() + " sent this message:"); System.out.println(s); } catch (IOException e) { System.err.println(e); }}

Page 88: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Broadcasting

Broadcasting allows a single datagram to be sent to a group of listeners

The group consists of all the computers within the local network

The previous code examples can be used for broadcasting

Just change the address: each network has a unique broadcast address

Page 89: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

IP addresses revisited

Each 32 bit IP number consists of two components:– The network address

The unique international address of the network

– The host address The unique address of a specific host in the net

There are three classes of network address denoted class ‘A’, ‘B’ and ‘C’

Page 90: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Class A,B and C addresses

Class A

Class B

Class C

192 . 85 . 35 . 87

Network Address Byte

Host Address Byte

0...

10...

110...

Page 91: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Broadcast addresses CIIPS has a class ‘C’ network which has the address

130.95.72 This portable computer has host address 134 within

the CIIPS network Each network has a single host address which is set

aside for broadcasts (either all one bits or all zero bits)

The CIIPS network uses broadcast address 130.95.72.255

Broadcasts are never routed onto other networks

Page 92: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Multicasting (1) Described in RFC1112 (August 1989) Multicasting allows distribution of a datagram

to a group of listeners who are not within the local network

Routers between networks need to pass multicast datagrams. . but many do not!

The MBONE is a way of tunneling datagrams across the Internet between islands of multicast activity

Page 93: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Multicasting (2) Multicasts are also sent to a special address (known as

a “group”) Multicast groups need to be agreed in advance. They

are not derived from a specific network/host address– Multicast groups identify a subject area (or stream of content)

rather than a specific computer or network. They are more like a TV channel number than a telephone number.

The IETF has set aside addresses from 224.0.0.1 to 239.255.255.255 specifically for multicasting

Page 94: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Multicasting (3)

To send to (or receive from) a multicast group it is first necessary to register interest in the group– This results in an Internet Group Management

Protocol (IGMP) message being sent to your router (RFCs 988/1112/2236)

Then a datagram is created, addressed to the group (and the chosen port)

Java has a specialised socket for multicasting: java.net.MulticastSocket

Page 95: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Some multicast groups

224.0.0.1 All hosts within local subnet 224.0.1.7 Audio news multicast 224.0.1.12 Video from IETF meetings 224.0.1.20 Expts. within local subnet 224.0.1.25 NBC Professional News There are 268 million multicast addresses

(in IPv4) with 65 thousand ports in each!

Page 96: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

java.net.MulticastSocket Subclass of java.net.DatagramSocket Constructed the same way Adds some extra methods:

– void joinGroup(InetAddress mcastGroup)Enter the specifies group so that you can send or receive datagrams

– void leaveGroup(InetAddress mcastGroup)Leave a group that you previously joined

– void setTimeToLive(int ttl)Sets how far your datagrams will travel before routers ignore them

– int getTimeToLive()

Page 97: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

MulticastSender

Sending similar to the previous example. . . . .but must register with the multicast group

and decide the longevity The steps involved are:

– Create the MulticastSocket.– Join the multicast group(s) (on startup).– Create the DatagramPacket.– Send the packet through the socket.– Leave the multicast group (on exit).

Page 98: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

MulticastSenderInetAddress multicastGroup = InetAddress.getByName(multicastGroupAddr);MulticastSocket socket = new MulticastSocket();socket.joinGroup(multicastGroup);socket.setTimeToLive(5);

byte[] data = “This is the message”.getBytes();DatagramPacket datagram = new DatagramPacket(data, data.length);datagram.setAddress(multicastGroup);datagram.setPort(9876);

socket.send(datagram);

socket.leaveGroup(multicastGroup);

Page 99: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

MulticastReceiver

The steps are:– Create a multicast socket on an agreed port.– Join the multicast group (on startup).– Create an empty datagram.– Wait for datagram to be delivered on the

socket.– Unpack and use the datagram.– Leave the multicast group (on exit).

Page 100: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

MulticastReceiverInetAddress multicastGroup = InetAddress.getByName(multicastGroupAddr);MulticastSocket socket = new MulticastSocket(9876);socket.joinGroup(multicastGroup);

byte[] data = new byte[1000];DatagramPacket packet = new DatagramPacket(data, data.length);socket.receive(packet);

String message = new String( packet.getData(), 0, packet.getLength());

socket.leaveGroup(multicastGroup);

Page 101: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Case Studies: Distributed TicTacToe Games

Server

Player 2

Session N...

Player 1

Session 1

Player 2Player 1...

Page 102: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Distributed TicTacToe Game

102

Server

Create a server socket.

Accept connection from the first player and notify the playeris Player 1 with token X.

Accept connection from the second player and notify theplayer is Player 2 with token O. Start a thread for thesession.

Player 1

1. Initialize user interface.

2. Request connection to the server andknow which token to use from the server.

3. Get the start signal from the server.

4. Wait for the player to mark a cell, sendthe cell's row and column index to theserver.

5. Receive status from the server.

6. If WIN, display the winner; if player 2wins, receive the last move from player 2.Break the loop

7. If DRAW, display game is over; breakthe loop.

8. If CONTINUE, receive player 2'sselected row and column index and markthe cell for player 2.

Player 2

1. Initialize user interface.

2. Request connection to the server andknow which token to use from the server.

3. Receive status from the server.

4. If WIN, display the winner. If player 1wins, receive player 1's last move, andbreak the loop.

5. If DRAW, display game is over, andreceive player 1's last move, and break theloop.

6. If CONTINUE, receive player 1'sselected row and index and mark the cellfor player 1.

7. Wait for the player to move, and sendthe selected row and column to the server.

Handle a session:

1. Tell player 1 to start.

2. Receive row and column of the selected cell fromPlayer 1.

3. Determine the game status (WIN, DRAW,CONTINUE). If player 1 wins, or drawn, send the status(PLAYER1_WON, DRAW) to both players and sendplayer 1's move to player 2. Exit..4. If CONTINUE, notify player 2 to take the turn, andsend player 1's newly selected row and column index toplayer 2.

5. Receive row and column of the selected cell fromplayer 2.

6. If player 2 wins, send the status (PLAYER2_WON) toboth players, and send player 2's move to player 1. Exit.

7. If CONTINUE, send the status, and send player 2'snewly selected row and column index to Player 1.

Page 103: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Distributed TicTacToe, cont.

103

TicTacToeServer -

JFrame -char token +getToken +setToken +paintComponet +mouseClicked

TicTacToeConstants -char token +getToken +setToken +paintComponet +mouseClicked

TicTacToeConstants +PLAYER1=1: int +PLAYER2 = 2: int +PLAYER1_WON = 1: int +PLAYER2_WON = 2: int +DRAW = 3: int +CONTINUE = 4: int

HandleASession -

TicTacToeClient -

JApplet -char token +getToken +setToken +paintComponet +mouseClicked

Cell -

TicTacToeServer +main(args: String[]): void

Runnable -char token +getToken +setToken +paintComponet +mouseClicked

HandleASession -player1: Socket -player2: Socket -cell char[][] -continueToPlay: boolean +run(): void -isWon(): boolean -isFull(): boolean -sendMove(out: DataOuputStream, row: int, column: int): void

TicTacToeClient -myTurn: boolean -myToken: char -otherToken: char -cell: Cell[][] -continueToPlay: boolean -rowSelected: int -columnSelected: int -isFromServer: DataInputStream -osToServer: DataOutputStream -waiting: boolean +run(): void -connectToServer(): void -recieveMove(): void -sendMove(): void -receiveInfoFromServer(): void -waitForPlayerAction(): void

Similar as in Example 12.7

-

Page 104: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

URLs

A URL, short for "Uniform Resource Locator", is a way to unambiguously identify the location of a resource on the Internet.

Page 105: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Example URLs

http://java.sun.com/

file:///Macintosh%20HD/Java/Docs/JDK%201.1.1%20docs/api/java.net.InetAddress.html#_top_

http://www.macintouch.com:80/newsrecent.shtml

ftp://ftp.info.apple.com/pub/

mailto:[email protected]

telnet://utopia.poly.edu

ftp://mp3:[email protected]:21000/c%3a/stuff/mp3/

http://[email protected]/ http://metalab.unc.edu/nywc/comps.phtml?

category=Choral+Works

Page 106: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The Pieces of a URL

the protocol, aka scheme the authority

– user infouser namepassword

– host name or address– port

the path, a.k.a. file the ref, a.k.a. section or anchor the query string

Page 107: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

The java.net.URL class

A URL object represents a URL. The URL class contains methods to

– create new URLs – parse the different parts of a URL– get an input stream from a URL so you can

read data from a server– get content from the server as a Java object

Page 108: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Content and Protocol Handlers

Content and protocol handlers separate the data being downloaded from the the protocol used to download it.

The protocol handler negotiates with the server and parses any headers. It gives the content handler only the actual data of the requested resource.

The content handler translates those bytes into a Java object like an InputStream or ImageProducer.

Page 109: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Finding Protocol Handlers

When the virtual machine creates a URL object, it looks for a protocol handler that understands the protocol part of the URL such as "http" or "mailto".

If no such handler is found, the constructor throws a MalformedURLException.

Page 110: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

URL Constructors

There are four (six in 1.2) constructors in the java.net.URL class.

public URL(String u) throws MalformedURLException

public URL(String protocol, String host, String file) throws MalformedURLException

public URL(String protocol, String host, int port, String file) throws MalformedURLException

public URL(URL context, String url) throws MalformedURLException

public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException

public URL(URL context, String url, URLStreamHandler handler) throws MalformedURLException

Page 111: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Constructing URL Objects

An absolute URL like http://www.poly.edu/fall97/grad.html#cstry {

URL u = new URL( "http://www.poly.edu/fall97/grad.html#cs");

}

catch (MalformedURLException e) {}

Page 112: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Constructing URL Objects in Pieces

You can also construct the URL by passing its pieces to the constructor, like this:

URL u = null;try { u = new URL("http", "www.poly.edu", "/schedule/fall97/bgrad.html#cs");

} catch (MalformedURLException e) {}

Page 113: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Including the Port

URL u = null;try { u = new URL("http", "www.poly.edu", 8000, "/fall97/grad.html#cs");

}catch (MalformedURLException e) {}

Page 114: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Relative URLs

Many HTML files contain relative URLs. Consider the page

http://metalab.unc.edu/javafaq/index.html On this page a link to “books.html" refers to

http://metalab.unc.edu/javafaq/books.html.

Page 115: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Constructing Relative URLs

The fourth constructor creates URLs relative to a given URL. For example,try {

URL u1 = new URL("http://metalab.unc.edu/index.html");

URL u2 = new URL(u1, "books.html");

}

catch (MalformedURLException e) {}

This is particularly useful when parsing HTML.

Page 116: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Parsing URLs

The java.net.URL class has five methods to split a URL into its component parts. These are: public String getProtocol()

public String getHost()

public int getPort()

public String getFile()

public String getRef()

Page 117: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example,

try { URL u = new URL("http://www.poly.edu/fall97/grad.html#cs ");

System.out.println("The protocol is " + u.getProtocol());

System.out.println("The host is " + u.getHost()); System.out.println("The port is " + u.getPort()); System.out.println("The file is " + u.getFile()); System.out.println("The anchor is " + u.getRef());

}catch (MalformedURLException e) { }

Page 118: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Reading Data from a URL The openStream() method connects to the

server specified in the URL and returns an InputStream object fed by the data from that connection.

public final InputStream openStream() throws IOException

Any headers that precede the actual data are stripped off before the stream is opened.

Network connections are less reliable and slower than files. Buffer with a BufferedReader or a BufferedInputStream.

Page 119: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Webcatimport java.net.*;import java.io.*;

public class Webcat { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); InputStream in = u.openStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); String theLine; while ((theLine = br.readLine()) != null) { System.out.println(theLine); } } catch (IOException e) { System.err.println(e);} } }}

Page 120: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Webcatimport java.net.*;import java.io.*;

public class Webcat { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); InputStream in = u.openStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); int c; while ((c = br.read()) != -1) { System.out.write(c); } } catch (IOException e) { System.err.println(e);} } }}

Page 121: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

URLConnections

The java.net.URLConnection class is an abstract class that handles communication with different kinds of servers like ftp servers and web servers.

Protocol specific subclasses of URLConnection handle different kinds of servers.

By default, connections to HTTP URLs use the GET method.

Page 122: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

URLConnections vs. URLs

Can send output as well as read input Can post data to CGIs Can read headers from a connection

Page 123: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

URLConnection five steps:1. The URL is constructed.

2. The URL’s openConnection() method creates the URLConnection object.

3. The parameters for the connection and the request properties that the client sends to the server are set up.

4. The connect() method makes the connection to the server. (optional)

5. The response header information is read using getHeaderField().

Page 124: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

I/O Across a URLConnection

Data may be read from the connection in one of two ways– raw by using the input stream returned by getInputStream()

– through a content handler with getContent().

Data can be sent to the server using the output stream provided by getOutputStream().

Page 125: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example,

try { URL u = new URL("http://www.wwwac.org/");

URLConnection uc = u.openConnection();

uc.connect(); InputStream in = uc.getInputStream();

// read the data... } catch (IOException e) { //...

Page 126: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Reading Header Data

The getHeaderField(String name) method returns the string value of a named header field.

Names are case-insensitive. If the requested field is not present, null is

returned. String lm = uc.getHeaderField("Last-modified");

Page 127: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Six Convenience Methods

These return the values of six particularly common header fields:

public int getContentLength()public String getContentType()public String getContentEncoding()public long getExpiration()public long getDate()public long getLastModified()

Page 128: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

try { URL u = new URL("http://www.sdexpo.com/"); URLConnection uc = u.openConnection(); uc.connect(); String key=null; for (int n = 1; (key=uc.getHeaderFieldKey(n)) != null; n++) { System.out.println(key + ": " + uc.getHeaderField(key));

}}catch (IOException e) { System.err.println(e);}

Page 129: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Writing data to a URLConnection

Similar to reading data from a URLConnection. First inform the URLConnection that you plan to

use it for output Before getting the connection's input stream, get

the connection's output stream and write to it. Commonly used to talk to CGIs that use the

POST method

Page 130: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Eight Steps:1.Construct the URL.

2.Call the URL’s openConnection() method to create the URLConnection object.

3.Pass true to the URLConnection’s setDoOutput() method

4.Create the data you want to send, preferably as a byte array.

Page 131: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

5.Call getOutputStream() to get an output stream object.

6.Write the byte array calculated in step 5 onto the stream.

7.Close the output stream.

8.Call getInputStream() to get an input stream object. Read from it as usual.

Page 132: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

HttpURLConnection

java.net.HttpURLConnection is an abstract subclass of URLConnection that provides some additional methods specific to the HTTP protocol.

URL connection objects that are returned by an http URL will be instances of java.net.HttpURLConnection.

Page 133: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Response Codes The getHeaderField() and getHeaderFieldKey() don't return the HTTP response code

After you've connected, you can retrieve the numeric response code--200 in the above example--with the getResponseCode() method and the message associated with it--OK in the above example--with the getResponseMessage() method.

Page 134: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

getRequestMethod()

The getRequestMethod() method returns the string form of the request method currently set for the URLConnection. GET is the default method.

Page 135: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

disconnect()

The disconnect() method of the HttpURLConnection class closes the connection to the web server.

Needed for HTTP/1.1 Keep-alive

Page 136: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

For example,try { URL u = new URL( "http://www.metalab.unc.edu/javafaq/books.html" );

HttpURLConnection huc = (HttpURLConnection) u.openConnection();

huc.setRequestMethod("PUT"); huc.connect(); OutputStream os = huc.getOutputStream(); int code = huc.getResponseCode(); if (code >= 200 && < 300) { // put the data... } huc.disconnect();}catch (IOException e) { //...

Page 137: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Following RedirectsHttpURLConnection.setFollowRedirects(true) method says that connections will follow redirect instructions from the web server. Untrusted applets are not allowed to set this.

HttpURLConnection.getFollowRedirects() returns true if redirect requests are honored, false if they're not.

Page 138: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

To Learn More Java Network Programming

– O’Reilly & Associates, 1997– ISBN 1-56592-227-1

Java I/O– O’Reilly & Associates, 1999– ISBN 1-56592-485-1

Web Client Programming with Java– http://www.digitalthink.com/catalog/cs/

cs308/index.html

Page 139: Java Network Programming. We will learn how Java handles  Internet Addresses  URLs  Sockets  Server Sockets  UDP

Questions?