A simple TCP echo back server
suggest changeOur TCP echo-back server will be a separate thread. It is simple, because this is only a start. It echoes back whatever you send, in upper case.
public class CAPECHOServer extends Thread{
// This class implements server sockets. A server socket waits for requests to come
// in over the network only when it is allowed through the local firewall
ServerSocket serverSocket;
public CAPECHOServer(int port, int timeout){
try {
// Create a new Server on specified port.
serverSocket = new ServerSocket(port);
// SO_TIMEOUT is the socket timeout.
// timeout is the time until socket timeout in milliseconds
serverSocket.setSoTimeout(timeout);
} catch (IOException ex) {
Logger.getLogger(CAPECHOServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run(){
try {
// We want the server to continuously accept connections
while(!Thread.interrupted()){
}
// Close the server once done.
serverSocket.close();
} catch (IOException ex) {
Logger.getLogger(CAPECHOServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Now to accept connections. Let’s update the run method.
@Override
public void run(){
while(!Thread.interrupted()){
try {
// Log with the port number and machine ip
Logger.getLogger((this.getClass().getName())).log(Level.INFO, "Listening for Clients at {0} on {1}", new Object[]{serverSocket.getLocalPort(), InetAddress.getLocalHost().getHostAddress()});
Socket client = serverSocket.accept(); // Accept the client connection
// Now get DataInputStream and DataOutputStreams
DataInputStream istream = new DataInputStream(client.getInputStream()); // From client's input stream
DataOutputStream ostream = new DataOutputStream(client.getOutputStream());
// Important Note
/*
The server's input is the client's output
The client's input is the server's output
*/
// Send a welcome message
ostream.writeUTF("Welcome!");
// Close the connection
istream.close();
ostream.close();
client.close();
} catch (IOException ex) {
Logger.getLogger(CAPECHOServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
// Close the server once done
try {
serverSocket.close();
} catch (IOException ex) {
Logger.getLogger(CAPECHOServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
writeUTF writes a 2-byte length followed by modified UTF-8, not a plain text line, so a telnet client will not show a clean Welcome!. The Java client below uses readUTF, so it prints Welcome!.
The server closes the socket after that welcome message. Because the server sends first, the client must read before it sends. Connect to the host and port you passed to the client.
public class CAPECHOClient extends Thread{
Socket server;
Scanner key; // Scanner for input
public CAPECHOClient(String ip, int port){
try {
server = new Socket(ip, port);
key = new Scanner(System.in);
} catch (IOException ex) {
Logger.getLogger(CAPECHOClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run(){
DataInputStream istream = null;
DataOutputStream ostream = null;
try {
istream = new DataInputStream(server.getInputStream()); // Familiar lines
ostream = new DataOutputStream(server.getOutputStream());
System.out.println(istream.readUTF()); // Print what the server sends
System.out.print(">");
String tosend = key.nextLine();
ostream.writeUTF(tosend); // Send whatever the user typed to the server
System.out.println(istream.readUTF()); // Finally read what the server sends before exiting.
} catch (IOException ex) {
Logger.getLogger(CAPECHOClient.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
istream.close();
ostream.close();
server.close();
} catch (IOException ex) {
Logger.getLogger(CAPECHOClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Now update the server
ostream.writeUTF("Welcome!");
String inString = istream.readUTF(); // Read what the user sent
String outString = inString.toUpperCase(); // Change it to caps
ostream.writeUTF(outString);
// Close the connection
istream.close();
Run the server and the client. The client prints something like this before you type a line:
Welcome!
>
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents