1 / 18

Object Oriented Programming in Java

Object Oriented Programming in Java. Lecture 16. Networking in Java. Concepts Technicalities in java. Client / Server Model. Relationship between two computer programs Client Initiates communication Requests services Server Receives requests Provides services. Client. Server. Client.

Download Presentation

Object Oriented Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented ProgramminginJava Lecture 16

  2. Networking in Java • Concepts • Technicalities in java

  3. Client / Server Model • Relationship between two computer programs • Client • Initiates communication • Requests services • Server • Receives requests • Provides services Client Server Client Client

  4. Addressing • How a service uniquely identified in the internet? • IP Address • Port

  5. Running a Server • Determine server location - port (& IP address) • Listen for connections on the port (&IP) • Open network connection to client • Read data from client (request) • Write data to client (response) • Close network connection to client • Stop server

  6. Client Operations • Determine server location – IP address & port • Open network connection to server • Write data to server (request) • Read data from server (response) • Close network connection • Stop client

  7. Classes in java.net • The core package java.net contains a number of classes that allow programmers to carry out network programming • ContentHandler • DatagramPacket • DatagramSocket • DatagramSocketImplHttpURLConnection • ABNSSDKmsna • ServerSocket • Socket • SocketImpl • URL • URLConnection • URLEncoder • URLStreamHandler

  8. Exceptions in Java • BindException • ConnectException • MalformedURLException • NoRouteToHostException • ProtocolException • SocketException • UnknownHostException • UnknownServiceExceptionZx

  9. The InetAddress Class • Handles Internet addresses both as host names and as IP addresses • Static Method getByName returns the IP address of a specified host name as an InetAddress object • Methods for address/name conversion: public static InetAddress getByName(String host) throws UnknownHostException public static InetAddress[] getAllByName(String host) throws UnknownHostException public static InetAddress getLocalHost() throws UnknownHostException public boolean isMulticastAddress() public String getHostName() public byte[] getAddress() public String getHostAddress() public int hashCode() public boolean equals(Object obj) public String toString()

  10. import java.net.*; import java.io.*; public class IPFinder { public static void main(String[] args) throws IOException { String host; BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); System.out.print("\n\nEnter host name: "); host = input.readLine(); try { InetAddress address = InetAddress.getByName(host); System.out.println("IP address: " + address.toString()); } catch (UnknownHostException e) { System.out.println("Could not find " + host); } } }

  11. Retrieving the current machine’s address import java.net.*; public class MyLocalIPAddress { public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println (address); } catch (UnknownHostException e) { System.out.println("Could not find local address!"); } } }

  12. The Java.net.Socket Class • Connection is accomplished through the constructors. Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new Socket object. public Socket(String host, int port) throws UnknownHostException, IOException public Socket(InetAddress address, int port) throws IOException public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException public Socket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException • Sending and receiving data is accomplished 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 a method to close a socket: public void close() throws IOException

  13. The Java.net.SocketSocket Class • The java.net.ServerSocket class represents a server socket. It is constructed on a particular 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 is used to perform the actual communication with the client. public ServerSocket(int port) throws IOException public ServerSocket(int port, int backlog) throws IOException public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException public Socket accept() throws IOException public void close() throws IOException

  14. TCP Sockets SERVER: • Create a ServerSocket objectServerSocket servSocket = new ServerSocket(1234); • Put the server into a waiting stateSocket link = servSocket.accept(); • Set up input and output streams • Send and receive dataout.println(awaiting data…);String input = in.readLine(); • Close the connectionlink.close()

  15. Set up input and output streams • Once a socket has connected you send data to the server via an output stream. You receive data from the server via an input stream. • Methods getInputStream and getOutputStream of class Socket: BufferedReader in = new BufferedReader( new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter(link.getOutputStream(),true);

  16. TCP Sockets CLIENT: • Establish a connection to the serverSocket link = new Socket(inetAddress.getLocalHost(),1234); • Set up input and output streams • Send and receive data • Close the connection

  17. import java.net.*; • import java.io.*; • public class Server { • public static void main(String args[]) throws Exception { • ServerSocket connection = new ServerSocket( 888 ); • Socket s = connection.accept(); // connection wait • PrintStream out = new PrintStream(s.getOutputStream( ) ); • out.println("Hello World"); • } • } Server “Hello World” Client/Server 1. (F) Server listens for connection on port 888. 3. (H) Server writes out “Hello World” to connection. • import java.net.*; • import java.io.*; • public class Client { • public static void main(String args[]) throws Exception { • Socket s = new Socket("localhost", 888 ); // Connect • BufferedReader in = new BufferedReader( • new InputStreamReader( s.getInputStream( ) ) ); • System.out.println( in.readLine() ); • } • } Client 2. (e) Client connects on port 888. 4. (h) Client reads in “Hello World” from connection.

  18. Server • import java.net.*; • import java.io.*; • public class Server { • public static void main(String args[]) throws Exception { • ServerSocket connection = new ServerSocket( 888 ); • while(true) { • Socket s = connection.accept(); // connection wait • PrintStream out=new PrintStream(s.getOutputStream()); • out.println("Hello World"); • } • } • } “Hello World” Client/Server 1. (G) Server listens for connection on port 888. 3. (I) Server writes out “Hello World” to connection. 5. Server closes connection, go to 1. • import java.net.*; • import java.io.*; • public class Client { • public static void main(String args[]) throws Exception { • Socket s = new Socket("localhost", 888 ); // connect • BufferedReader in = new BufferedReader( • new InputStreamReader( s.getInputStream( ) ) ); • System.out.println( in.readLine() ); • } • } Client 2. (e) Client connects on port 888. 4. (h) Client reads in “Hello World” from connection.

More Related