May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

72
May 27, 2015 JDP Networking Part II Networking Part II Networking Part II May 27, 2015 1 of 72

Transcript of May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

Page 1: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Networking Part IINetworking Part II

May 27, 2015

1 of 72

Page 2: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Thought for the DayThought for the Day

Networking (on UNIX) tries to emulate the notion of a sequential file.

2 of 72

Page 3: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Last LectureLast Lecture• Topic: Network programming

What is a network: introduction and basic concepts Hosts, client/server, naming, ports Sockets Examples

3 of 72

Page 4: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

This LectureThis LectureTopics: • Sockets• I/O in Java

OutputStream and InputStream Filter streams Readers and writers Buffering

• I/O and Networking• Dynamic Class Loading• Applets• Datagrams• Multicast• NIO and Networking

4 of 72

Page 5: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

This LectureThis LectureReading see

http://condor.depaul.edu/dmumaugh/JDP• Harold: Chapters 1-4, 12-14; pp 53-63; pp. 497, 525-542• Pitt: Chapters 3, 9, 11, 5, 13• Core Java, Vol. 1: pp. 516-550• Core Java, Vol. 2: pp. 1-64, 169-217, • Technology Brief TCP• Technology Brief HTTP • Sun's (Oracle) API Javadoc <

http://download.oracle.com/javase/8/docs/api/index.html>• Java Streams Basics• Java in a Nutshell: Input and Output Streams

5 of 72

Page 6: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

A More Complex ExampleA More Complex Example

Simple Single File HTTP Server

6 of 72

Page 7: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

HTTPHTTPHTTP - Hyper Text Transmission Protocol• See pp 53-63 of Harold• See Technology Brief: HTTP• Newest is HTTP 1.1, look up the RFC for that

protocol.

7 of 72

Page 8: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

HTTPHTTPHTTP - Hyper Text Transmission Protocol• Three main commands GET, PUT, POST• Commands consist of

Command and optional header information Blank line Payload (data); format specified by header

• Response consists of Header information Blank line Payload (data); format specified by header

8 of 72

Page 9: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

HTTPHTTPHTTP/1.1 is the stable version of the HTTP protocol. Earlier HTTP/1.0 has fewer "commands".

Requests: GET, POST, HEADResponse: HTTP/1.1 200 OK or 301 Moved Permanently or 404 Not Found or ...Header lines: Connection: close Accept-Language: en ...

9 of 72

Page 10: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Typical HTTP SessionTypical HTTP Session$ telnet condor.depaul.edu 80GET /dmumaugh/index.html HTTP/1.1Host: condor.depaul.edu<blank line>HTTP/1.1 200 OKDate: Wed, 02 Apr 2003 20:35:34 GMTServer: Apache/2.0.39 (Unix) PHP/4.2.1Last-Modified: Wed, 02 Apr 2003 05:06:49 GMTETag: "29261-103d-e21d0c40"Accept-Ranges: bytesContent-Length: 4157Content-Type: text/html; charset=ISO-8859-1<blank line>Blah…Blah…Blah…

10 of 72

Page 11: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Error MessageError MessageHTTP/1.1 404 Not Found

Date: Wed, 02 Apr 2003 20:33:34 GMT

Server: Apache/2.0.39 (Unix) PHP/4.2.1

Vary: accept-language

Accept-Ranges: bytes

Content-Length: 804

Content-Type: text/html; charset=ISO-8859-1

<blank line>

Message explaining that server could not find the file.

11 of 72

Page 12: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Example: A HTTP ServerExample: A HTTP Server• Single file server

Very limited, serves one filehttp://condor.depaul.edu/dmumaugh/JDP/examples/lect02/SingleFileHTTPServer.java

12 of 72

Page 13: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Testing Servers Using Testing Servers Using telnet• The telnet program is invaluable for testing

servers that transmit ASCII strings over Internet connections Web servers Mail servers

• Usage: unix> telnet <host> <portnumber> Creates a connection with a server running on <host>

and listening on port <portnumber>.

• See slide 10 for example.

13 of 72

Page 14: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Network ProgrammingNetwork Programming

14 of 72

Page 15: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

Network ProgrammingNetwork Programming• General theory

Create and/or open a socket Convert a socket to a standard Java I/O class

Input stream Output stream

Use standard Java I/O for all operations• Works for "normal" TCP connections• Does not work for UDP

Need something specific and lower level

May 27, 2015 JDP Networking Part II 15 of 72

Page 16: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

SocketsSockets

16 of 72

Page 17: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

The Java Socket ClassThe Java Socket ClassSockets can1. Connect to a remote machine2. Send data3. Receive data4. Close a connection5. Bind to a port6. Listen for incoming connection7. Accept connections from remote machines on a

bound port

17 of 72

Page 18: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

The Java Socket ClassThe Java Socket ClassThe Socket class supports the

1. Connect to a remote machineSocket is = new Socket(hostname, portnumber);

The Java ServerSocket Class supports1. Open for connections

ServerSocket ss = new ServerSocket(portnumber);

18 of 72

Page 19: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

More on SocketsMore on Sockets• Our examples ignored sockets except to open

them, now more details• What can we do with a socket?

bind(SocketAddress, bindpoint) Binds the socket to a local (port) address.

void close() Closes this socket.

InputStream getInputStream() Returns an input stream for this socket. [Note well!]

OutputStream getOutputStream() Returns an output stream for this socket. [Note well!]

19 of 72

Page 20: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

More on SocketsMore on Sockets• What can we do with a socket?

SocketAddress getRemoteSocketAddress() Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

Methods for SocketAddress String getHostName()

Gets the hostname. int getPort()

Gets the port number.

• See the Sockets API for more details

20 of 72

Page 21: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Network I/ONetwork I/O• So the question is now that we have a connection,

what do with it and how?

21 of 72

Page 22: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

More on SocketsMore on SocketsNow what?• To make a connection:

1. Construct a socket

2. Convert to InputStream and/or OutputStream

3. Read and/or write

4. Flush

5. Close

• To make a serverw Construct a ServerSocketw Bind to a portw Accept a connectionw Convert to InputStream or OutputStreamw Read/Write, flush and close (as above)

22 of 72

Page 23: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

More on SocketsMore on SocketsTo make a connection:

1. Construct a socketSocket theSocket = new Socket(hostname, port);

2. Convert to InputStream or OutputStreamrawIn = theSocket.getInputStream();rawOut = theSocket.getOutputStream();

3. Read and/or writeint abyte = rawIn.read();rawOut.write(something);

4. FlushrawOut.flush();

5. CloserawOut.close(); rawIn.close();theSocket.close();

23 of 72

Page 24: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

More on SocketsMore on SocketsTo make a server

1. Construct a ServerSocket*ServerSocket server = new ServerSocket();

2. Bind to a port*server.bind(8000);

3. Listen for connectionserver.listen()

4. Accept a connectionfor(;;) { Socket client = server.accept();

5. Read/Write, flush and close (as in previous slide)String response = "Blah, blah!\r\n"OutputStream os = client.getOutputStream();os.write(response.getBytes() ); os.flush();client.close();

}

24 of 72

Page 25: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Java I/OJava I/O

25 of 72

Page 26: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

FilesFiles• There are two types of files:

The standard input, output and error: System.in, System.out and System.err

Regular files

• Files are supported by the File class Most the time you can use a convenience class such as FileReader instead

Or FileWriter Or PrintWriter

26 of 72

Page 27: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

I/O in JavaI/O in Java• Java has four basic classes for I/O:

OutputStream: sends binary data [Note previous!] InputStream: receives binary data [Note previous!] Writer: sends text data Reader: receives text data

• Everything else (sockets, files, System.out etc. are subclasses of these).

• Why two of everything (binary vs. text)?

27 of 72

Page 28: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

OutputStreamOutputStream• Where do output streams come from?

System.out and System.err FileOutputStream and Socket.getOutputStream() [Note!]

• Where do these streams write to?• Most other output streams are built on top of these

basic ones.

28 of 72

Page 29: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

OutputStreamOutputStream• The methods of an output stream are:

void write (int b) throws IOException

void write (byte[] data) throws IOException

void write (byte[] data, int offset, int length) throws IOException

void flush () throws IOException

void close () throws IOException

• What do these methods do?

29 of 72

Page 30: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

OutputStreamOutputStreamWhat does this code try to do? What is wrong with it? How

can we fix it?

1: void printHelloWorld(final OutputStream out)

2: {

3: final String hw = "Hello, World\n";

4: for (int i=0; i < hw.length (); i++)

5: {

6: out.write (hw.charAt (i));

7: }

8: out.close ();

9: } 30 of 72

Page 31: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

OutputStreamOutputStreamAnswer:

19: void printHelloWorld (final OutputStream out) {

20: try {

21: final String hw = "Hello, World\n";

22: for (int i=0; i < hw.length (); i++) {

23: out.write (hw.charAt (i));

24: }

25: out.flush ();

26: } catch (final IOException ex) {

27: // Some error handling code goes here

28: System.err.println ("Oops 1: " + ex);

29: } finally {

31 of 72

Page 32: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

OutputStreamOutputStream29: } finally {30: // Always a good idea to close streams31: try {32: out.close ();33: } catch (final IOException ex) {34: // Some more error handling code goes here35: System.err.println ("Oops 2: " + ex);36: }37: }38: }

32 of 72

Page 33: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

InputStreamInputStream• Where do input streams come from?

System.in FileInputStream

and Socket.getInputStream() [Note!]

• Where do these streams read from?• Most other input streams are built on top of these

basic ones.

33 of 72

Page 34: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

InputStreamInputStream• The important methods of an input stream are:

int read() throws IOException

int read(byte[] input) throws IOException

int read(byte[] input, int offset, int length)

throws IOException

int available() throws IOException

void close() throws IOException

• What do these methods do?• Why does read() return an int rather than a byte?

What happens at the end of file?

34 of 72

Page 35: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Reading data from a streamReading data from a stream... Socket ss = ...InputStream sin = ss.getInputStream();01: StringBuffer sb = new StringBuffer(); 02: line = ""; 03: int achar = 0; 04: achar = sin.read(); 05: while(achar != -1 ) 06: { 07: sb.append( (char) achar); 08: achar = sin.read(); 09: }10: line = sb.toString();Problems with this code?

35 of 72

Page 36: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

InputStreamInputStreamWhat does this code try to do? What is wrong with it? How can

we fix it?01: byte[] get128 (final InputStream in) {

02: final byte[] result = new byte[128];

03: for (int bytesRead = 0; bytesRead < 128;)

04: {

05: final int read = in.read (result,

06: bytesRead, 128-bytesRead);

07: bytesRead = bytesRead + read;

08: }

09: return result;

10: }

36 of 72

Page 37: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

InputStreamInputStreamAnswer:19: byte[] get128 (final InputStream in) {20: final byte[] result = new byte[128];21: try {22: for (int bytesRead = 0; bytesRead < 128;) {23: final int read = in.read (result, bytesRead, 128-bytesRead);24: // Check to see if we hit the end of the stream25: if (read > 0) {26: bytesRead = bytesRead + read;27: } else {28: // End of stream!29: break;30: }31: }32: } catch (IOException ex) {

37 of 72

Page 38: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

InputStreamInputStream32: } catch (IOException ex) {33: System.err.println ("Oops: " + ex);34: } finally {35: return result;36: }37: }

38 of 72

Page 39: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Stream HintsStream Hints• Remember to flush output buffers.• Remember IOExceptions! (Expect the majority of your code

to be exception handling.).• Remember that exception handlers can raise exceptions!• Use finally to make sure clean-up is always executed.• Use close to close any streams you open.• Remember to flush output streams before closing them.

39 of 72

Page 40: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Filter streamsFilter streams• Raw InputStreams and OutputStreams aren't much

use!• Often we want to add functionality to an existing stream.• For example:

A BufferedInputStream which adds buffering to an input stream. A CipherInputStream which decrypts an encrypted input stream. A GZIPInputStream which uncompresses a compressed input

stream.

• Streams which read from other streams are called filter streams in Java.

40 of 72

Page 41: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Filter streamsFilter streams• Filter streams can be chained together:

final FileInputStream rawIn =

new FileInputStream ("foo.des.gz");

final InputStream processedIn =

new GZIPInputStream (

new CipherInputStream (

new BufferedInputStream (

rawIn), desCipher)); • This is an example of dataflow programming.

41 of 72

Page 42: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Readers and writersReaders and writers• Input and OutputStreams handle raw data.• Readers and Writers handle text.• Most protocols (e.g. HTTP) and data exchange formats

(e.g. XML) these days are text based. Why? Isn't it much more efficient to send binary data? Protocols expect Text strings; specifically TELNET

format• For example:

final OutputStream out = ...;

// Create a new text writer with encoding UTF-8

final Writer writer = new OuputStreamWriter (out, "UTF-8");

42 of 72

Page 43: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Readers and writersReaders and writers• What is text data?

A string of characters, usually 8-bits each, that are printable

• Common values for the encoding: ISO-8859-1, UTF-8 or UTF-16. (What are these?) Traditional was ASCII 7-bit American code Internationalization

European character sets: Latin-1 or ISO 8859 Then came the other countries: Russian, Hebrew,

Arabic, Chinese, Japanese and others UNICODE: a 8 or 16 bit code for all languages: UTF-

8 and UTF-16

43 of 72

Page 44: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Readers and writersReaders and writers• Two very useful methods:

String BufferedReader.readLine () PrintWriter.println (String msg)

• for example from a Socket s: PrintWriter writer = new PrintWriter (new OutputStreamWriter (s.getOutputStream (), "UTF-8")); writer.println ("GET /index.html HTTP/1.1"); writer.flush (); • and: BufferedReader reader = new BufferedReader (new InputStreamReader (s.getInputStream (),

"UTF-8")); String request = reader.readLine (); • Connect these up and what happens?

44 of 72

Page 45: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

Readers and writersReaders and writersUnfortunately, there's a number of gotchas with Sun's

(Oracle) code:• PrintWriter doesn't throw any IOExceptions, you

have to do your error-handling the old-fashioned C way (blech).

• Mixing text and binary is very hard! [Note well!!]• BufferedReader and PrintWriter can't get their act

together about what a new line is!

May 27, 2015 JDP Networking Part II 45 of 72

Page 46: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Readers and writersReaders and writersFor example, on a Mac sending on a socket (non-Java):

writer.println ("A message"); and on any Java machine receiving on a socket:

final String line = reader.readLine ();

this will deadlock! Why?• What is a line of text?

In MS/DOS and Windows: sequence of characters ending in "\r\n"

In LINUX and UNIX: a sequence of characters ending in "\n" In MAC OS: a sequence of characters ending in "\r"

• What is a line in Java? [As in println] a sequence of characters ending in "\n"

• What is a line on the Internet? What is TELNET protocol? sequence of characters ending in "\r\n"

46 of 72

Page 47: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Buffered Readers and WritersBuffered Readers and Writers• What is buffering?

How does it work?• BufferedReaders have buffering.

Is this a problem? Why?• BufferedWriters have buffering.

Is this a problem? Why?• How do you switch from Buffered I/O to Unbuffered I/O?

1. Why do you want to? [Hint: see note].2. Is it possible?3. No!

47 of 72

Page 48: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Network ProgrammingNetwork Programming

48 of 72

Page 49: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Network ProgrammingNetwork ProgrammingNow what?• Normally, we do not do basic read/write on

streams.• We encapsulate in a

BufferedInputStream BufferedOutputStream BufferedReader BufferedPrinter BufferedWriter

• This provides a more powerful set of operations on the data.

49 of 72

Page 50: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Network ProgrammingNetwork Programming• This allows us to use methods such as

readLine() println

• See slide 44 for an vague idea.• The basic methodology of network programming is to send

data to the server (using print and println) • Then receive the result (using readLine() )• You parse the result and take action.• Most of the work is in the parsing of possible responses.• Occasionally one has problems with mixed data streams

(partly text and partly binary). See slide 47.

50 of 72

Page 51: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Class LoadingClass Loading

51 of 72

Page 52: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Class LoadingClass LoadingWhat is the problem?• We are an application on one machine and the class/applet

lives on another. How do we access the classes we want to use?

• How does a Java application work? How does a JVM startup and run a program?

• What is a class loader?• Where does the default Java class loader load classes

from?• Where does the applet class loader load classes from?

52 of 72

Page 53: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Class LoadingClass Loading• What is an URL?• What does one look like?

http://condor.depaul.edu/dmumaugh/JDP/test/

http://condor.depaul.edu/dmumaugh/JDP/test/classBD.jar

Class loaders:URLClassLoader(URL[] urls) Loads classes from list [array] of URLs. If URL ends in '/' it is a directory, otherwise it expects a

JAR file.

53 of 72

Page 55: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

AppletsApplets• What is an applet?

Lets assume you have an applet on another host. Now what?

• Write a small framework to load an applet and run.http://condor.depaul.edu/dmumaugh/JDP/examples/lect02/AppletHost.java

55 of 72

Page 56: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

DatagramsDatagrams• What is a Datagram?• What is UDP?

56 of 72

Page 57: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

DatagramsDatagrams(Unicast) UDP = Datagram

Benefits• Connectionless protocol• Server: No connection socket to accept or close• Client: No connection to create or terminate

Limitations• No reassembly of fragmented packets• Payload limited in practice (512 bytes good working

number) due to router behavior• No flow control• No sequencing; no loss detection and retransmission

57 of 72

Page 58: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

DatagramsDatagrams• UDP packet is an object of type java.net.DatagramPacket

DatagramPacket(byte[] data, int length); DatagramPacket(byte[] data, int length, InetAddress address, int port);

DatagramPacket(byte[] data, int offset, int length);

Etc.• UDP socket is an object of type java.net.DatagramSocket

DatagramSocket(int port); DatagramSocket(int port, InetAddress localAddress);

DatagramSocket(SocketAddress localSocketAddress);

58 of 72

Page 59: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Datagram PacketsDatagram Packets• DatagramSocket

connect(InetAddress address, int port); send(packet) receive(packet) disconnect()

• DatagramPacket byte[] getData() setData(byte[] data);

Misc. variants like we have in read and write int getLength() setLength(int length); int getOffset()

59 of 72

Page 61: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

MulticastingMulticastingUDP has some special properties that TCP does not:• What is unicast?

A communication from one host to another.

• What is broadcast? A communication from one host to all hosts on a net or

subnet. Address with 255.255.255.255 set – send to all hosts,

subject to policy by routers and gateways. Address with host id all ones (in a subnet) set – sent to

all on the subnet.

61 of 72

Page 62: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

MulticastingMulticastingUDP has some special properties that TCP does not:• What is multicast?• What can be multicast?

Software distribution Time services Naming services – DNS Stock market ticker Database replication Video and audio streaming Multiplayer gaming

62 of 72

Page 63: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

MulticastingMulticasting• Multicast addresses

Logical IP address that identifies a multicast group multicast group – a set of zero or more hosts that have

joined the group Transmissions sent to a multicast address are delivered

to all current members of the group. Must join group to receive, but not to send. All addresses 224.0.0.0 to 239.255.255.255 are reserved

for multicasting.

63 of 72

Page 64: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

MulticastingMulticasting• Limitations

May not be supported by router unless specially enabled. ISP may block Router may fragment packet (same as UDP) 512 bytes maximum safe packet size. Firewall and/or router may not support

64 of 72

Page 65: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

MulticastingMulticasting• Sending

Sending a multicast in Java is identical to sending a normal UDP datagram to a multicast address.int port = 8888;

byte[] data; // initialization not shown

// Multicast address to send to

InetAddress address = InetAddress.getByName("239.1.12.126");

DatagramPacket packet = new DatagramPacket(data, data.length, address, port);

DatagramSocket socket = new DatagramSocket();

socket.send(packet);

65 of 72

Page 66: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

MulticastingMulticasting• Receiving

Initialize a MulticastSocket Join a multicast group Receive a datagram

• Example

int port = 8888;

MulticastSocket socket = new MulticastSocket(port);

// Multicast address to receive from

InetAddress mcastAddr = InetAddress.getByName("239.1.12.126");

socket.joinGroup(mcastAddr);

byte[] data = new byte[8193]; // note size

DatagramPacket packet = new DatagramPacket(data, data.length, address, port);

socket.receive(packet);66 of 72

Page 68: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

New I/ONew I/O

68 of 72

Page 69: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

Scalable I/OScalable I/OIntended for servers with large number of network connections.• Concepts

Buffers Channels

• Features Non-blocking mode Multiplexing

• See: Java™ I/O, NIO, and NIO.2 <http://download.oracle.com/javase/8/docs/technotes/guides/io/index.html>

• See notes for some API general information• See examples given class page: Code not covered in class (uses NIO

package) but in handouts.• See Chapters 4 and 5 of Pitt, Chapter 12 of Harold.

69 of 72

Page 70: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

For More InformationFor More Information• W. Richard Stevens, “Unix Network Programming:

Networking APIs: Sockets and XTI”, Volume 1, Second Edition, Prentice Hall, 1998. THE network programming bible. Uses C

70 of 72

Page 71: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

SummarySummary• Networks are the current dominating technology of

computer technology• Networks can be totally separate and known as an

intranet or connected via gateways and routers to the internet.

• The layering of networks allows a programmer to concentrate on the application.

• We use the sockets abstraction to encapsulate the lower levels.

• We have showed a sample of network programming.

71 of 72

Page 72: May 27, 2015JDP Networking Part II Networking Part II May 27, 2015 1 of 72.

May 27, 2015 JDP Networking Part II

SummarySummary• We discussed Java Networking

Just the fundamentals

• Many nuances exist – especially with UDP, Multicasting and Scalable Network I/O Look at a good text for details and discussion.

• Recommended texts: Core Java, Volume II – Advanced Features, Eight Edition, Cay S.

Horstmann and Gary Cornell, ISBN: 978-0132354790 Java Network Programming by Elliotte Rusty Harold, O'Reilly, 3rd

edition, 2004, ISBN 0-596-00217-3 Fundamental Networking in Java by Esmond Pitt, Springer, ISBN-

10: 918-1846-2803-6

72 of 72