SLIDE 1
Basic Java Network Programming CS211 July 30 th , 2001 The Network - - PowerPoint PPT Presentation
Basic Java Network Programming CS211 July 30 th , 2001 The Network - - PowerPoint PPT Presentation
Basic Java Network Programming CS211 July 30 th , 2001 The Network and OSI Model IP Header TCP Header TCP/IP: A Paradox Connection Oriented and Connectionless Protocols TCP Reliable: no loss, or duplication, or out of order
SLIDE 2
SLIDE 3
IP Header TCP Header
SLIDE 4
TCP/IP: A Paradox
SLIDE 5
Connection Oriented and Connectionless Protocols
- TCP
– Reliable: no loss, or duplication, or out of order – End-to-end – Connection Oriented: congestion control, flow control
- UDP & IP
– Unreliable – Datagram – Connectionless
SLIDE 6
InetAddress Class
- represents an IP address.
- numeric addresses <---> host names
InetAddress utopia, duke; try { utopia=InetAddress.getByName("utopia.poly.edu"); duke=InetAddress.getByName("128.238.2.92"); } catch (UnknownHostException e) { System.err.println(e); } public static InetAddress[] getAllByName(String host) throws UnknownHostException public static InetAddress getLocalHost() throws UnknownHostException
SLIDE 7
URL class
- create new URLs
- parse the different parts of a URL, namely:
– the protocol – the authority
- user info: user name, password
– host name or address – port – the path, a.k.a. file – the ref, a.k.a. section or anchor – the query string
- get an input stream from a URL so you can read data
from a server
- get content from the server as a Java object
SLIDE 8
Webcat.java: Reading data from a URL
public class Webcat { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); BufferedReader br = new BufferedReader( new InputStreamReader (new InputStream( u.openStream()))); String theLine; while ((theLine = br.readLine()) != null) { System.out.println(theLine); } } catch (IOException e) { System.err.println(e); } } } }
SLIDE 9
Socket Communication
Client Server Paradigm
SLIDE 10
TCP Sockets
- Server socket class: ServerSocket
– wait for requests from clients. – after a request is received, a client socket is generated.
- Client socket class: Socket
– an endpoint for communication between two apps/applets. – obtained by contacting a server – generated by the server socket
- Communication is handled by
input/output streams.
– Socket provides an input and an output stream.
SLIDE 11
TCP Sockets cont.
- public Socket(String host, int port,
InetAddress localAddr, int localPort)
- public Socket(InetAddress address, int port,
InetAddress localAddr, int localPort)
- public ServerSocket(int port, int backlog,
InetAddress bindAddr)
SLIDE 12
Summary: TCP Server
- 1. Create the server socket and begin listening.
- 2. Call the accept() method to get new connections.
- 3. Create input and output streams for the returned
socket.
- 4. Conduct the conversation based on the agreed protocol.
- 5. Close the client streams and socket.
- 6. Go back to step 2 or continue to step 7.
- 7. Close the server socket
SLIDE 13
Datagram Sockets
- same class is used for both client and server halves
- public DatagramSocket (int port, InetAddress laddr)
DatagramSocket serverSocket = new DatagramSocket( 4545 ); DatagramSocket clientSocket = new DatagramSocket();
- public DatagramPacket(byte[] buf, int offset, int length,
InetAddress address, int port) DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length, addr, port);
SLIDE 14
Summary: UDP Server
- 1. Create the datagram socket on a specific port.
- 2. Call receive() to wait for incoming packets.
- 3. Respond to received packets according to the
agreed protocol.
- 4. Go back to step 2 or continue to step 5.
- 5. Close the datagram socket.
SLIDE 15
Multicast
A multicast address is a class D address in the range 224.0.0.1 to 239.255.255.255, inclusive
SLIDE 16
Multicast Sockets
- Multicast Sockets use Datagram Packets
- BIG difference lies in Multicast's capability to
transmit to all listening hosts simultaneously.
- Downside is the lack of support for multicast
- routing. Most routers still don’t forward
multicast packets.
- Notion of Groups: Join and leave groups.
SLIDE 17
Applets
public void init(): To initialize the applet each time it's loaded (or reloaded). public void start(): To start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet. public void stop(): To stop the applet's execution, such as when the user leaves the applet's page or quits the browser. public void destroy(): To perform a final cleanup in preparation for unloading.
SLIDE 18
Applet Example
import java.applet.*; import javax.swing.*; public class HTMLLabelApplet extends JApplet { public void init() { JLabel theText = new JLabel( "<html>Hello! This is a multiline label with <b>bold</b> " + "and <i>italic</i> text. <P> " + "It can use paragraphs, horizontal lines, <hr> " + "<font color=red>colors</font> " + "and most of the other basic features of HTML 3.2</html>"); this.getContentPane().add(theText); } }
SLIDE 19
Applet Tags
<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt> <PARAM NAME=parameter1Name VALUE=aValue> <PARAM NAME=parameter2Name VALUE=anotherValue> </APPLET> e.g. <APPLET CODE="Animator.class" WIDTH=460 HEIGHT=160> <PARAM NAME="imageSource" VALUE="images/Beans"> <PARAM NAME="backgroundColor" VALUE="0xc0c0c0"> <PARAM NAME="endImage" VALUE=10> <PARAM NAME="soundSource" VALUE="audio"> <PARAM NAME="soundtrack" VALUE="spacemusic.au"> <PARAM NAME="sounds" VALUE="1.au|2.au|3.au|4.au|5.au|6.au|7.au|8au|9.au|0.au" > <PARAM NAME="pause" VALUE=200> . . . </APPLET>
SLIDE 20
Applet Example
SLIDE 21
Applet Security
- Can open a socket only to the host name
from which it was loaded. Other connections results in a SecurityException.
- Datagram sockets don't open connections. Hence
when an inbound packet is received, the host name is checked.
- Multicast sockets are disallowed.