|
Socket Communication
Part:
1
2
3
4
(Continued from previous part...)
To create a server application that can handle multiple connections, one
thread needs to be created for each connection. The following program,
called ReverseEchoServer, offers the same function to client program.
But it can handle multiple connections.
/**
* ReverseEchoServer.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.*;
import java.net.*;
public class ReverseEchoServer implements Runnable {
private Socket con = null;
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(8888);
printServerSocketInfo(s);
while (true) {
Socket c = s.accept();
printSocketInfo(c);
ReverseEchoServer v = new ReverseEchoServer(c);
Thread t = new Thread(v);
t.start();
}
} catch (IOException e) {
System.err.println(e.toString());
}
}
public ReverseEchoServer(Socket c){
con = c;
}
public void run() {
try {
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
con.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String m = "Welcome to Reverse Echo Server."+
" Please type in some words.";
w.write(m,0,m.length());
w.newLine();
w.flush();
while ((m=r.readLine())!= null) {
if (m.equals(".")) break;
char[] a = m.toCharArray();
int n = a.length;
for (int i=0; i<n/2; i++) {
char t = a[i];
a[i] = a[n-1-i];
a[n-i-1] = t;
}
w.write(a,0,n);
w.newLine();
w.flush();
}
w.close();
r.close();
con.close();
} catch (IOException e) {
System.err.println(e.toString());
}
}
private static void printSocketInfo(Socket s) {
System.out.println("Remote address = "
+s.getInetAddress().toString());
System.out.println("Remote port = "
+s.getPort());
System.out.println("Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println("Local address = "
+s.getLocalAddress().toString());
System.out.println("Local port = "
+s.getLocalPort());
}
private static void printServerSocketInfo(ServerSocket s) {
System.out.println("Server socker address = "
+s.getInetAddress().toString());
System.out.println("Server socker port = "
+s.getLocalPort());
}
}
Here is the output displayed on the ReverseEchoServer console window, with
two copies of SocketClient running:
Server socker address = 0.0.0.0/0.0.0.0
Server socker port = 8888
Remote address = /127.0.0.1
Remote port = 1084
Local socket address = /127.0.0.1:8888
Local address = /127.0.0.1
Local port = 8888
Remote address = /127.0.0.1
Remote port = 1085
Local socket address = /127.0.0.1:8888
Local address = /127.0.0.1
Local port = 8888
Binding Sockets to Local Ports
As I mentioned previously, I used a special socket constructor to allow the system
to bind the socket an un-specified local address and port number. This gives the
system the freedom to pickup an un-used port number. If I change SocketClient.java
from:
Socket c = new Socket("localhost",8888);
to:
Socket c = new Socket();
c.bind(new InetSocketAddress("localhost",4444));
c.connect(new InetSocketAddress("localhost",8888));
the first running instance of SocketClient will work, but additional running
instances will get the following error:
java.net.BindException: Address already in use: JVM_Bind
Why? Because, at any given time, only one socket can be bound to a given port.
So, allowing the system to pick up a free local port number is a better idea.
However, the sockets created by the ServerSocket.accept() method seems to be
allowed to bind to the same local port. See the output of ReverseEchoServer.
When two or more client programs are connected to it, sockets created for
the connections are all bound to the same port number, 8888.
Exercise: Can re-bind a bound socket to new port number?
Part:
1
2
3
4
|