|
Socket Communication
Part:
1
2
3
4
(Continued from previous part...)
In the telnet window, type in the following text:
Fish, I love you and respect you very much.
But I will kill you dead before this day ends.
.
The text will be reversed and returned back from the ReverseEchoer:
Welcome to Reverse Echo Server. Please type in some words.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
.sdne yad siht erofeb daed uoy llik lliw I tuB
.
Note that the telnet program is not displaying text you typed in.
It only displays the text received from ReverseEchoer program.
SocketClient - A Simple Client Socket Application
Instead of using the telnet program, we can use the following simple
socket application to communicate with ReverseEchoer:
/**
* SocketClient.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.*;
import java.net.*;
public class SocketClient {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
PrintStream out = System.out;
try {
Socket c = new Socket("localhost",8888);
printSocketInfo(c);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
c.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String m = null;
while ((m=r.readLine())!= null) {
out.println(m);
m = in.readLine();
w.write(m,0,m.length());
w.newLine();
w.flush();
}
w.close();
r.close();
c.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());
}
}
Note that:
- The constructor used to instantiate the socket also performs the
functions of bind() and connect().
Now, run ReverseEchoer first, then run SocketClient in a separate command
window. If you type in the same text as in the previous test, you will get
the following output:
Remote address = localhost/127.0.0.1
Remote port = 8888
Local socket address = /127.0.0.1:1073
Local address = /127.0.0.1
Local port = 1073
Welcome to Reverse Echo Server. Please type in some words.
Fish, I love you and respect you very much.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
But I will kill you dead before this day ends.
.sdne yad siht erofeb daed uoy llik lliw I tuB
.
Note that socket constructor automatically picked up a free local port, 1073.
ReverseEchoServer - A Simple Multi-Connection Socket Server
ReverseEchoer runs perfectly if there is only one client program talking
to it. If we run another copy of SocketClient, while a copy of SocketClient
is running and connected to ReverseEchoer, the second copy of SocketClient
will not be able establish a communication link with ReverseEchoer, because
it is busy with the first copy of SocketClient.
(Continued on next part...)
Part:
1
2
3
4
|