Client socket
Download
Report
Transcript Client socket
Java API for distributed computing
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
1
Two pure Java mechanisms
• Socket-based communication
– Endpoints of a two-way communication
between two distributed components
communicating with each other.
– Communication must be explicitly established
by the two parties
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
2
Two pure Java mechanisms
• RMI
– Makes the network transparent
– Allows distributed component manipulation
almost as if they were on same host
– Programmers do not deal with interhost
communication
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
3
Socket-based communication
• Two kinds of sockets:
– Server sockets: wait for requests from clients
– Client sockets: used to send data to a server socket and receive
data from it.
• Server socket listens at a specific port.
• This port completely distinguishes different servers
running on the same host.
• Server socket must be running on server host before any
communicating client.
• After contact by client
– communication is established
– Client socket is created for the app running on server
host to communicate with client.
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
4
Server socket API
• Constructors
ServerSocket(int port)
port: specifies port number at which server socket will
be listening for clients
When multiple client access server socket they will be
placed in a queue.
ServerSocket(int port, in backlog)
backlog specifies max length of queue.
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
5
Common server socket methods
Socket accept();
Waits for connection request
Thread executing this call will be blocked until
request is received, at which time returns a
client socket.
void close();
Stop waiting for requests from clients.
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
6
Typical code structure for server socket
try{
ServerSocket server = new ServerSocket(port);
for ( ; ; ){
Socket client = server.accept();
// handle client …
}
}
catch(IOException exc) {
// handle failure to create server socket
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
7
Client socket
• An instance of the Socket class.
• Can be created one of two ways:
– Using constructor:
Socket( String host, int port);
– On the server side, clients sockets returned by
the accept method.
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
8
Socket communication
• Communication between server and client
handled by client sockets at both ends.
• Each client socket has
– an InputStream for receiving data
– An OutputStream for sending data
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
9
Common socket API
InputStream getInputStream();
Returns an input stream for this socket.
OutputStream getOutputStream();
Returns an output stream for this socket
void close();
Closes this socket.
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
10
Typical client socket code
try{
Socket client = new Socket(host, port);
PrintWriter out = new PrintWriter(
new OutputStreamWriter(client.getOutputStream()));
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
// send and receive data ….
in.close();
out.close();
client.close();
}
catch( IOException exc) {
// handle failure of connection
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
11
import java.io.*;
import java.net.*;
class EchoServer {
public static void main(String[] args) {
System.out.println("EchoServer started.");
try {
ServerSocket server = new ServerSocket(8008);
Socket incoming = server.accept();
System.out.println("Connected to: " +
incoming.getInetAddress() +
" at port: " + incoming.getLocalPort());
BufferedReader in = new BufferedReader(new
InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(incoming.getOutputStream()));
out.println("Hello! This is Java EchoServer. ");
out.println("Enter BYE to exit.");
out.flush();
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
12
//
while (true) {
String str = in.readLine();
if (str == null) {
break;
// client close }
else {
out.println("Echo: " + str);
out.flush();
System.out.println("Received: " + str);
if (str.trim().equals("BYE"))
break;
}
}//end while
incoming.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
System.out.println("EchoServer stopped.");
}//end main
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
13
• To run a server
– Enter the java command to execute it
• To run a client
– Enter the command to execute client followed by name
of server host and port number where server is
executing.
• You can run for test purposes server and client on
same host; client invocation will look:
telnet localhost 8008
Or
telnet 127.0.0.1 8008
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
14
import java.io.*;
import java.net.*;
class EchoClient {
public static void main(String[] args) {
try {
String host;
if (args.length > 0 && args[0] != null) {
host = args[0];
} else {
host = "localhost";
}
Socket client = new Socket(host, 8008);
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(
new OutputStreamWriter(client.getOutputStream()));
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
15
//sending data to server:
for (int i = 1; i <= 10; i++) {
System.out.println("Sending: line " + i);
out.println("line " + i);
out.flush();
}
out.println("BYE");
out.flush();
//receiving data from server:
for (;;) {
String str = in.readLine();
if (str == null) {
break; }
else {
System.out.println(str); }
}//end for
} catch (Exception e) {
System.out.println("Error: " + e); }
}//end main
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
16
A multi-echo server
• Using threads we can create sessions to
handle clients.
• The session needs a reference to the client
socket
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
17
import java.io.*;
import java.net.*;
public class ClientHandler extends Thread {
protected Socket incoming;
public ClientHandler(Socket incoming) {
this.incoming = incoming;
}
public void run() {
try {
BufferedReader in
= new BufferedReader(
new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter(
new OutputStreamWriter(incoming.getOutputStream()));
out.println("Hello! This is Java MultiEchoServer.");
out.println("Enter BYE to exit.");
out.flush();
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
18
for (;;) {
String str = in.readLine();
if (str == null) {
break;
}
else {
out.println("Echo: " + str);
out.flush();
System.out.println("Received: " + str);
if (str.trim().equals("BYE"))
break;
}
} //end for
incoming.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}//end run
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
19
import java.net.*;
public class MultiEchoServer {
public static void main(String[] args) {
System.out.println("MultiEchoServer started.");
try {
ServerSocket s = new ServerSocket(8009);
for (;;) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
System.out.println("MultiEchoServer stopped.");
}
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
20
Broadcast echo server
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
21
import java.util.*;
import java.io.*;
import java.net.*;
class BroadcastClientHandler extends Thread {
protected Socket incoming;
protected int id;
protected BufferedReader in;
protected PrintWriter out;
public BroadcastClientHandler(Socket incoming, int id) {
this.incoming = incoming;
this.id = id;
try {
if (incoming != null) {
in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
Distributed Software Engineering
22
} Spring/2002
C:\unocourses\4350\slides\DefiningThreads
public synchronized void putMessage(String msg) {
if (out != null) {
out.println(msg);
out.flush();
}
}
public synchronized void broadcastClients(String str){
Enumeration enum = BroadcastEchoServer.activeThreads.elements();
while (enum.hasMoreElements()) {
BroadcastClientHandler client =
(BroadcastClientHandler) enum.nextElement();
if (client != this) {
client.putMessage("Broadcast(" + id + "): " + str);
}
}
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
23
public void run() {
System.out.println("Client handler " + id + " started.");
if (in != null && out != null) {
putMessage("Hello! This is Java BroadcastEchoServer. Enter BYE to ex
try {
for (;;) {
String str = in.readLine();
if (str == null) {
break;
} else { // str is not null
putMessage("Echo: " + str);
System.out.println("Received (" + id + "): " + str);
if (str.trim().equals("BYE")) {
break;
} else {
broadcastClients(str);
}
} //end else of not null str
}//end for
incoming.close();
BroadcastEchoServer.activeThreads.removeElement(this);
} catch (IOException e) {}
}
System.out.println("Client thread " + id + " stopped.");
} // end run
Spring/2002
Distributed Software Engineering
24
}
C:\unocourses\4350\slides\DefiningThreads
public class BroadcastEchoServer {
static protected Vector activeThreads;
public static void main(String[] args) {
System.out.println("BroadcastEchoServer started.");
activeThreads = new Vector();
int i = 1;
try {
ServerSocket s = new ServerSocket(8010);
for (;;) {
Socket incoming = s.accept();
System.out.println("Spawning client thread " + i);
BroadcastClientHandler newThread =
new BroadcastClientHandler(incoming, i);
activeThreads.addElement(newThread);
newThread.start();
i++;
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
System.out.println("BroadcastEchoServer stopped.");
}
}
Spring/2002
Distributed Software Engineering
C:\unocourses\4350\slides\DefiningThreads
25