*
@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:
}catch
(ScriptException ex) {
System.out.println(ex.getMessage());
}
}
}catch
(IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
Output:
Illustration
1: ServerSide
Illustration
2: ClientSide