Wednesday, February 24, 2016

TCP Socket Programming in Java


Write a server-client TCP socket program where the client takes inputs, sends it to the server to perform a certain operation, and gets back the output of the operation. Run the programs in local host and in different systems in LAN.

/**
*
* @author anuz
*/
public class ClientSide {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here

try (
//name of the computer and the port number to which you want to connect.
Socket socket = new Socket("localhost", 8888);
//readers and writers to write characters over the socket.
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);//to send data to server
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//for response from the server
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));//standard input
) {
System.out.println(in.readLine());
String userInput;
//the loop reads a line at a time from the standard input stream and immediately sends it to the server
while ((userInput = stdIn.readLine()) != null) {
if(userInput.equalsIgnoreCase("exit")){
break;
}
out.println(userInput);
System.out.println(in.readLine());
}

} catch (IOException ex) {
System.out.println(ex.getMessage());
}

}

}





/**
*
* @author anuz
*/
public class ServerSide {

/**
* @param args the command line arguments
* @throws javax.script.ScriptException
*/
public static void main(String[] args) throws ScriptException {
// TODO code application logic here
try {

//server socket port number
ServerSocket serverSocket = new ServerSocket(8888);
while(true){
Socket clientSocket = serverSocket.accept();
System.out.println("connection from :" + clientSocket.getInetAddress()); //display client IP connected to server

PrintStream out = new PrintStream(clientSocket.getOutputStream());
out.println("welcome to the calculator server");

BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
try {
System.out.println(line);
if (line.equalsIgnoreCase("exit")) {
break;
}

//code for operation of user input.

//------------------------------------------------
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
System.out.println("=>" + engine.eval(line));
out.println("=>" + engine.eval(line));
//-------------------------------------------------

/*
 Security Note:
 You should never use ScriptEngine in a server context with user input. The executed
JavaScript can access all Java classes and thus hijack your application without limit
It executes a script, not evaluates an expression.
*/


}catch (ScriptException ex) {
System.out.println(ex.getMessage());
}
}

}catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

}
}



Output:


Illustration 1: ServerSide

Illustration 2: ClientSide













 

No comments:

Post a Comment