I have created a simple client server application using TCP. I am using eclipse and am pretty much new to TCP, so my questions: 1)If i want the client to send an expression such as "10+20-5" so i put it in the argument? which is arg[0]. 2)After sending such an expression mentioned above how do I make the server calculate this actual expression so as to return back to the client the result "25"?
Client Code:
public class TCPClient {
public static void main (String args[]) {
// arguments supply message and hostname
Socket s = null;
try{
int serverPort = 7896;
s = new Socket(args[1],serverPort);
DataInputStream in =new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeUTF(args[0]);
String data = in.readUTF();
System.out.println("Received: "+ data) ;
}catch (UnknownHostException e) {System.out.println("Socket:"+e.getMessage());
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
}catch (IOException e){System.out.println("readline:"+e.getMessage());
}finally {if(s!=null)
try {s.close();
}catch (IOException e) {System.out.println ("close:" + e.getMessage());}
}
}
}
Server Code:
public class TCPServer {
public static void main (String args[]) {
try{
int serverPort = 7896; // the server port
ServerSocket listenSocket = new ServerSocket(serverPort);
while(true) {
System.out.println("Server is ready and waiting for requests ... ");
Socket clientSocket = listenSocket.accept();
Connection c = new Connection(clientSocket);
}
} catch(IOException e) {System.out.println("Listensocket:"+e.getMessage());}
}
}
class Connection extends Thread {
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
public Connection (Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new DataInputStream( clientSocket.getInputStream());
out =new DataOutputStream( clientSocket.getOutputStream());
this.start();
} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
}
public void run(){
try {
String data = in.readUTF();
out.writeUTF(data);
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
} catch(IOException e) {System.out.println("readline:"+e.getMessage());
} finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}
}
}
1/ In java, when you pass java youclass_or_jar_file argument argument
Then the argument[0] will be you your first argument you typed, not the program name as in C/C++ . So in this situation, you can use the argument vector in the format as [host, port, "message"]
. Then in your program, you just send your arg[2]
in this case is the message
2/To evaluate the received expression, you should have implemented the evaluation method yourself.
In this method, in my opinion you should parse the input into the postfix notation
http://en.wikipedia.org/wiki/Reverse_Polish_notation , by using stack , read the wiki for more infomation then evaluate it.
Hope this help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.