Pages

Sunday 5 June 2011

Java Echo Server

Hi Guys,

I am trying to capture my imaginations in a virtual toolbox that resides in my mind. The ideas are crude and world is not willing to recognize me. Get ready, fasten your belts but wait there is no high speed travelling so remain calm. I am providing your mind a food for thought with some freaky port enabled coding that shoes server and client connection. Feel free to comment:

Server Connection:
import java.io.*;
import java.net.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class EchoServer extends Thread {
static final String APP_NAME = "EchoServer";
static final int PORT = 6991;
static ServerSocket serverSocket;
static int port = PORT;
Socket clientSocket;
BufferedReader is = null;
BufferedWriter os = null;
public EchoServer(Socket cs) {
clientSocket = cs;
}
public static void main(String args[]) {
if (usageOnly(args)) {
System.exit(0);
}
initialize(args);
printMsg("EchoServer running on port: " + port + "...Ready to accept connections...");
while (true) {
try {
Socket clientSocket = serverSocket.accept();
EchoServer es = new EchoServer(clientSocket);
es.start();
} catch (IOException e) {
printMsg("Cannot accept client connection.");} } }
public void run() {
processClientRequest();}
private static boolean usageOnly(String args[]) {
if (args.length > 1 || (args.length == 1
&& (args[0].equalsIgnoreCase("‐usage")
|| args[0].equalsIgnoreCase("‐help")
|| args[0].equalsIgnoreCase("‐h")))) {
System.out.println("Usage: java " + APP_NAME + " [<port>]");
System.out.println(" The default port is " + port + ".");
return true;} else { return false;} }
private static void initialize(String args[]) {
processCommandLine(args);
try {serverSocket = new ServerSocket(port); } catch (IOException e) {
printMsg("Cannot create server socket " + "on port: " + port + ". Exiting...");
System.exit(0);}}
private void processClientRequest() {
try {os = new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream()));
is = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));} catch (IOException e) {
printMsg("Cannot handle client connection.");
cleanup();
return;
}
try {
String input = is.readLine();
if (input != null) {
input = APP_NAME + ": " + input + " " + getDateTime();
os.write(input, 0, input.length());
os.flush();
}
} catch (IOException e) {
printMsg("I/O error while processing client's print file.");
}
cleanup();
}
private void cleanup() {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
printMsg("I/O error while closing connections.");
}
}
private static void processCommandLine(String args[]) {
if (args.length != 1) {
return;}
port = Integer.parseInt(args[0]);
if (port < 1 || port > 6881) {
port = PORT;
printMsg("Using port " + port + " instead.");
}}
private static void printMsg(String msg) {
System.out.println(APP_NAME + ": " + msg);}
private String getDateTime() {DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);}}
Client Connection:

import java.net.*;
import java.io.*;
public class EchoClient
{
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.err.println("Usage: java EchoClient <99.253.76.32> <255.255.255.0>");
System.exit(0);}
BufferedReader in = null;
PrintWriter out = null;
BufferedReader fromUser = null;
Socket sock = null;
try {
sock = new Socket(args[0], Integer.parseInt(args[1]));
// set up the necessary communication channels
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(sock.getOutputStream(),true);
while (true) {
String line = fromUser.readLine();
if (line == null || line.equals("bye"))
{
out.println("bye");
break;
}
out.println(line);
System.out.println(in.readLine());
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally {
if (in != null)
in.close();
if (fromUser != null)
fromUser.close();
if (out != null)
out.close();
if (sock != null)
sock.close();}}}

Output


Output


Thank You.

Fayyaz

No comments:

Post a Comment