Network Programming in Java Chapter 22 Network Programming

  • Slides: 19
Download presentation
Network Programming in Java Chapter 22

Network Programming in Java Chapter 22

Network Programming • One of the original purpose for creating Java • Network Programming

Network Programming • One of the original purpose for creating Java • Network Programming – two or more computers communicate and share resources to solve a problem • Example: Client-Server architecture

Client-Server Architecture • Server – an application that runs on the host computer that

Client-Server Architecture • Server – an application that runs on the host computer that provides a means of connection and useful information once a connection is established. • Client – application(s) running on different computer(s) that seek to establish a connection and request computation/ information from the server • Actually, computation can be performed on both ends of the network connection

Networking Basics • Computers running on the Internet communicate with each other using either

Networking Basics • Computers running on the Internet communicate with each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP), as this diagram illustrates: • Java programs communicate over the network at the application layer, so you typically don't need to concern yourself with the TCP and UDP layers. • Instead, you can use the classes in the java. net package to provide system-independent network communication. • However, to decide which Java classes your programs should use, you do need to understand how TCP and UDP differ.

TCP vs. UDP • TCP (Transmission Control Protocol) is a connection-based protocol that provides

TCP vs. UDP • TCP (Transmission Control Protocol) is a connection-based protocol that provides a reliable flow of data between two computers. – This is analogous to making a telephone call. – TCP guarantees that data sent from one end of the connection actually gets to the other end and in the same order it was sent. Otherwise, an error is reported. – applications that require reliable communications. The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet • UDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about their order of delivery or arrival at all. UDP is not connection-based like TCP. – This is analogous to the sending a series of letters through the postal service. – Used for applications that cannot afford or don’t need the slow down of reliable communication. – Note: Many firewalls and routers are configured to not allow UDP packets.

IP Address and Ports • Data transmitted over the Internet is accompanied by addressing

IP Address and Ports • Data transmitted over the Internet is accompanied by addressing information that identifies the computer and the port for which it is destined. • Computers usually have a single physical connection to the Internet whose domain name address (fienup 2. cs. uni. edu) gets mapped to an IP address (134. 161. 243. 240) • Ports are identified by a 16 -bit number, which TCP and UDP use to deliver the data to the right application.

Port Numbers • port numbers 0 - 1023 are reserved for wellknown services such

Port Numbers • port numbers 0 - 1023 are reserved for wellknown services such as HTTP and FTP and other system services • IP number and port # are used to create a socket

Networking Classes in the JDK • Through the classes in java. net, Java programs

Networking Classes in the JDK • Through the classes in java. net, Java programs can use TCP or UDP to communicate over the Internet. – URL, URLConnection, Socket, and Server. Socket classes all use TCP – Datagram. Packet, Datagram. Socket, and Multicast. Socket classes all use UDP

Socket: Java Network Connection • Analogous to an electrical socket, the client can plug

Socket: Java Network Connection • Analogous to an electrical socket, the client can plug into the server • This creates a connection along which information can flow • When the client disconnects, the socket is free for the next client to use • Input and output streams can be created to allow communication over the socket.

Date Server Example import java. util. Date; import java. net. *; import java. io.

Date Server Example import java. util. Date; import java. net. *; import java. io. *; public class Date. Server { static final public int port. Number = 4291; public static void main(String [] args) { try { Date. Server world = new Date. Server(); } catch (IOException e) { System. out. println("IO exception " + e); } // end try-catch } // end main public Date. Server ( ) throws IOException { Server. Socket server = new Server. Socket(port. Number); for (int i = 0; i < 3; i++) { System. out. println("Waiting for a client"); Socket sock = server. accept(); System. out. println("Got client #" + i); Output. Stream. Writer out = new Output. Stream. Writer(sock. get. Output. Stream()); String message = "Current date and time is " + new Date() + "n"; out. write(message); out. close(); } // end for } // end Date. Server } // end class Date. Server

Date Server Example Server. Socket server = new Server. Socket(port. Number); Socket sock =

Date Server Example Server. Socket server = new Server. Socket(port. Number); Socket sock = server. accept(); – A server socket waits for requests to come in over the network. – accept() - listens for a connection to be made to this socket and accepts it. Output. Stream. Writer out = new Output. Stream. Writer(sock. get. Output. Stream()); out. write(message); out. close(); – Output stream created to allow communication over the socket

Date Client Example import java. net. *; import java. io. *; public class Date.

Date Client Example import java. net. *; import java. io. *; public class Date. Client { static final public int port. Number = 4291; public static void main(String [] args) { try { Date. Client world = new Date. Client(); } catch (IOException e) { System. out. println("Received an IO exception " + e); } // end try-catch } // end main public Date. Client ( ) throws IOException { Socket sock = new Socket(Inet. Address. get. Local. Host(), port. Number); Reader isread = new Input. Stream. Reader(sock. get. Input. Stream()); Buffered. Reader input = new Buffered. Reader(isread); System. out. println("message is " + input. read. Line()); } // end Date. Client } // end class Date. Client

Date Client/Server Limitations • Only one-way communication from the server to the client was

Date Client/Server Limitations • Only one-way communication from the server to the client was utilized • Only one client can be handled by the server at any time

Therapist Client/Server Example Eliza-like (Weizenbaum ’ 76) simulation of a Gestalt psychotherapist conducting a

Therapist Client/Server Example Eliza-like (Weizenbaum ’ 76) simulation of a Gestalt psychotherapist conducting a question-and-answer session with a user • Multiple clients are possible by creating a new server thread to handle each client

Therapist Server Example import java. net. *; import java. io. *; class Therapist {

Therapist Server Example import java. net. *; import java. io. *; class Therapist { static public void main (String [ ] args) { try { Therapist world = new Therapist(); } catch (IOException e) { System. out. println("Received an IO Exception" + e); } // end try-catch } // end main static final public int port. Number = 5321; public Therapist () throws IOException { Server. Socket server = new Server. Socket(port. Number); while (true) { Socket sock = server. accept(); Thread session = new Therapy. Session (sock. get. Input. Stream(), sock. get. Output. Stream()); session. start(); } // end while } // end Therapist constructor } // end class Therapist • Spawns a new server thread to handle each new client

Therapist Server Example import java. io. *; import java. util. Vector; import java. util.

Therapist Server Example import java. io. *; import java. util. Vector; import java. util. String. Tokenizer; class Therapy. Session extends Thread { public Therapy. Session (Input. Stream ins, Output. Stream outs) { Reader isread = new Input. Stream. Reader(ins); in = new Buffered. Reader(isread); out = new Output. Stream. Writer(outs); } // end Therapy. Session constructor private String name = ""; private Buffered. Reader in; private Writer out; private String response(String text) { // answer a question with a question if (text. ends. With("? ")) return "Why do you want to know? "; // break up line Vector words = new Vector(); String. Tokenizer breaker = new String. Tokenizer(text, ". , ? !"); while (breaker. has. More. Elements()) words. add. Element(breaker. next. Element()); // look for ``I feel'' if ((words. size() > 1) && words. element. At(0). equals("i") && words. element. At(1). equals("feel")) return "Why do you feel that way? "; // look for relatives for (int i = 0; i < words. size(); i++) { String relative = (String) words. element. At(i); if (is. Relative(relative)) return "Tell me more about your " + relative; } // end for // nothing else, generic response return "Tell me more"; } // end response

Therapist Server Example private boolean is. Relative(String name) { return name. equals("mother") || name.

Therapist Server Example private boolean is. Relative(String name) { return name. equals("mother") || name. equals("father") || name. equals("brother") || name. equals("sister") || name. equals("uncle"); } // end is. Relative public void run() { try { // get name out. write("Hello. Welcome to therapy. What is your name? n"); out. flush(); name = in. read. Line(); out. write("Well " + name + " what can we do for you today? n"); out. flush(); // now read and respond while (true) { String text = in. read. Line(); out. write(response(text) + "n"); out. flush(); } // end while } catch (IOException e) { stop(); } } // end run } // end class Therapy. Session

Therapist Client Example import java. io. *; import java. net. *; class Therapy. Client

Therapist Client Example import java. io. *; import java. net. *; class Therapy. Client { public static void main (String [ ] args) { try { Therapy. Client world = new Therapy. Client(); } catch (IOException e) { System. out. println("got IO exception " + e); } // end try-catch } // end main static final public int port. Number = 5321; private Buffered. Reader input, term; private Writer output; public Therapy. Client () throws IOException { // open standard input as buffered reader term = new Buffered. Reader(new Input. Stream. Reader(System. in)); // open socket as a reader and a writer Socket sock = new Socket(Inet. Address. get. Local. Host(), port. Number); Reader isread = new Input. Stream. Reader(sock. get. Input. Stream()); input = new Buffered. Reader(isread); output = new Output. Stream. Writer(sock. get. Output. Stream()); while (true) { // now read and print // read and print something from therapist String line = input. read. Line(); System. out. println(line); // get our response line = term. read. Line(); if (line. equals("Quit")) break; output. write(line + "n"); output. flush(); } // end while } // end Therapy. Client constructor } // end class Therapy. Client

Transmitting Objects over the Network • Objects to be transmitted over the network via

Transmitting Objects over the Network • Objects to be transmitted over the network via object serialization • The object to be transmitted implements the Serializable interface • Classes that do not implement this interface will not have any of their state serialized or deserialized. • All subtypes of a serializable class are themselves serializable. • The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. • To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible noarg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.