[英]Client/server expression evaluation in JAVA
I have created a simple client server application using TCP. 我已经使用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?
我正在使用eclipse,并且对TCP来说是个新手,所以我的问题是:1)如果我希望客户端发送诸如“ 10 + 20-5”的表达式,那么我将其放在参数中? which is arg[0].
这是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"?
2)发送完上述表达式后,如何使服务器计算该实际表达式,以便将结果“ 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
1 /在Java中,当您传递
java youclass_or_jar_file argument argument
时,您会使用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++ . 然后,参数[0]将是您输入的第一个参数 ,而不是C / C ++中的程序名称。 So in this situation, you can use the argument vector in the format as
[host, port, "message"]
. 因此,在这种情况下,您可以使用格式为
[host, port, "message"]
的参数向量。 Then in your program, you just send your arg[2]
in this case is the message
然后,在您的程序中,您只需发送
arg[2]
在这种情况下就是message
2/To evaluate the received expression, you should have implemented the evaluation method yourself. 2 /要评估接收到的表达式,您应该自己实现评估方法。
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. 在我看来,以这种方法,您应该使用stack将输入解析为
postfix notation
http://en.wikipedia.org/wiki/Reverse_Polish_notation ,阅读Wiki以获得更多信息,然后对其进行评估。
Hope this help 希望这个帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.