Network Programming in Java

computerharpySoftware and s/w Development

Dec 2, 2013 (3 years and 8 months ago)

110 views

Network Programming in Java

Chapter 22

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 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 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 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 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 (fienup2.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 well
-
known 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 can use TCP or UDP to
communicate over the Internet.


URL
,
URLConnection
,
Socket
, and
ServerSocket

classes all use TCP


DatagramPacket
,
DatagramSocket
, and
MulticastSocket

classes all use UDP

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.*;


public class DateServer {



static final public int portNumber = 4291;



public static void main(String [] args) {



try {




DateServer world = new DateServer();



} catch (IOException e) {




System.out.println("IO exception " + e);



} // end try
-
catch




} // end main




public DateServer ( ) throws IOException {



ServerSocket server = new ServerSocket(portNumber);



for (int i = 0; i < 3; i++) {




System.out.println("Waiting for a client");




Socket sock = server.accept();




System.out.println("Got client #" + i);




OutputStreamWriter out = new







OutputStreamWriter(sock.getOutputStream());




String message = "Current date and time is " + new Date() + "
\
n";




out.write(message);




out.close();



} // end for


} // end DateServer

} // end class DateServer

Date Server Example

ServerSocket server = new ServerSocket(portNumber);

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.


OutputStreamWriter out =




new OutputStreamWriter(sock.getOutputStream());

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 DateClient {



static final public int portNumber = 4291;



public static void main(String [] args) {



try {




DateClient world = new DateClient();



} catch (IOException e) {




System.out.println("Received an IO exception " + e);






} // end try
-
catch


} // end main




public DateClient ( ) throws IOException {



Socket sock = new Socket(InetAddress.getLocalHost(),







portNumber);



Reader isread = new InputStreamReader(sock.getInputStream());



BufferedReader input = new BufferedReader(isread);



System.out.println("message is " + input.readLine());


} // end DateClient



} // end class DateClient

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
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 {


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 portNumber = 5321;



public Therapist () throws IOException {



ServerSocket server = new ServerSocket(portNumber);



while (true) {




Socket sock = server.accept();




Thread session = new TherapySession





(sock.getInputStream(),







sock.getOutputStream());




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.StringTokenizer;


class TherapySession extends Thread {


public TherapySession (InputStream ins, OutputStream outs) {



Reader isread = new InputStreamReader(ins);



in = new BufferedReader(isread);



out = new OutputStreamWriter(outs);


} // end TherapySession constructor



private String name = "";


private BufferedReader in;


private Writer out;



private String response(String text) {




// answer a question with a question



if (text.endsWith("?"))




return "Why do you want to know?";




// break up line



Vector words = new Vector();



StringTokenizer breaker = new StringTokenizer(text, " .,?!");



while (breaker.hasMoreElements())




words.addElement(breaker.nextElement());





// look for ``I feel''



if ((words.size() > 1) &&




words.elementAt(0).equals("i") &&




words.elementAt(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.elementAt(i);




if (isRelative(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 isRelative(String name) {



return name.equals("mother") || name.equals("father")




|| name.equals("brother") || name.equals("sister")




|| name.equals("uncle");


} // end isRelative





public void run() {



try {





// get name




out.write("Hello. Welcome to therapy. What is your name?
\
n");




out.flush();




name = in.readLine();




out.write("Well " + name + " what can we do for you today?
\
n");




out.flush();





// now read and respond




while (true) {




String text = in.readLine();




out.write(response(text) + "
\
n");




out.flush();




} // end while



} catch (IOException e) { stop(); }


} // end run

} // end class TherapySession

Therapist Client Example


import java.io.*;

import java.net.*;


class TherapyClient {


public static void main (String [ ] args) {



try {




TherapyClient world = new TherapyClient();



} catch (IOException e) {




System.out.println("got IO exception " + e);



} // end try
-
catch


} // end main



static final public int portNumber = 5321;


private BufferedReader input, term;


private Writer output;



public TherapyClient () throws IOException {




// open standard input as buffered reader



term = new BufferedReader(new InputStreamReader(System.in));





// open socket as a reader and a writer



Socket sock = new Socket(InetAddress.getLocalHost(), portNumber);



Reader isread = new InputStreamReader(sock.getInputStream());



input = new BufferedReader(isread);



output = new OutputStreamWriter(sock.getOutputStream());





// now read and print



while (true) {





// read and print something from therapist




String line = input.readLine();




System.out.println(line);





// get our response




line = term.readLine();




if (line.equals("Quit"))





break;




output.write(line + "
\
n");




output.flush();



} // end while


} // end TherapyClient constructor


} // end class TherapyClient


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 no
-
arg 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.