我有一个客户端和一个通过对象流进行通信的服务器。我有另一个类math.java,它填充数学计算的方法(如add,substract等)。当服务器和客户端启动时,服务器会显示math.java类的功能,以显示客户端打印它的欢迎消息。然后用户必须输入(a - add,s - substract等)。更改用户输入服务器接收它并传递给math.java并创建两个随机数并显示给用户,然后用户手动输入结果,然后由math.java检查,如果正确,则打印正确,如果它不正确,打印不正确,并在两种情况下开始另一个线程。这一直持续到用户按q退出或至少10次....
我的客户端服务器连接出现问题。当显示欢迎时,我按a进行添加,然后在服务器端我看到a被拍了两次,但它仍然给我两个随机数字,每件事情都很好但是当服务器检查并打印正确或不正确时,我&#39 ; m无法启动新线程.....客户端给我另一行(空白),然后另一个线程启动。而服务器现在收到空行并且比客户端
我需要尽快完成这个项目,帮助我一个人。
文件在这里:
client.java
public class clientRPC扩展了Thread {
public static void main(String args[]) {
Socket s = null;
try {
int serverPort = 8888;
s = new Socket("localhost", serverPort);// server name is local host
clientRPC tt = new clientRPC();
Thread thr = new Thread(tt);
thr.start();
//initializing input and output streams object and referencing them to get input and output
ObjectInputStream in = null;
ObjectOutputStream out = null;
out = new ObjectOutputStream(s.getOutputStream());
in = new ObjectInputStream(s.getInputStream());
// out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
//reads and prints welcome message
System.out.println(in.readObject());
//reads user input
String value = br.readLine();
out.writeObject(value);
out.flush();
String objstr = String.valueOf(in.readObject());
System.out.print(objstr);
value = br.readLine();
out.writeObject(value);
}
} 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());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
System.out.println("close:" + e.getMessage());
}
}
}
}
}
server.java
public class serverRPC extends Thread {
Socket clientSocket;
public static void main (String args[]) {
try{
int serverPort = 8888;
ServerSocket listen_socket = new ServerSocket(serverPort);
while(true) {
Socket clientSocket = listen_socket.accept();
Connection c = new Connection(clientSocket);
serverRPC rpc = new serverRPC();
Thread thr=new Thread(rpc);
thr.start();
}
} catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());}
}
}
class Connection extends Thread {
ObjectInputStream in;
ObjectOutputStream out;
Socket clientSocket;
public Connection (Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new ObjectInputStream( clientSocket.getInputStream());
out =new ObjectOutputStream( clientSocket.getOutputStream());
this.start();
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
}
public String printMessage(InetAddress a, String s){
String address = a.getHostAddress();
return String.format("The Received Message from Client at address:%s\n====================================\n%s", address, s);
}
public void run(){
try {
MathsTutor mt = new MathsTutor();
InetAddress ip = clientSocket.getInetAddress();
while(true)
{
//display welcome message
out.writeObject(mt.displayOperations());
Object obj = in.readObject();
String objstr = String.valueOf(obj);
if(!objstr.equalsIgnoreCase("q"))
{
System.out.println(printMessage(ip, objstr));
mt.setOperation(objstr);
out.writeObject(mt.setProblem());
//reading another input
Object obj1 = in.readObject();
int objint = Integer.parseInt(obj1.toString());
out.writeObject(mt.checkAnswer(objint));
}
else
{
clientSocket.close();
}
// printWriter output = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
}
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
} catch(IOException e) {System.out.println("readline:"+e.getMessage());
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}
}
}
math.java
public class Math implements Serializable{
private int valueOne,valueTwo;
private char operation;
int [] problems;
SecureRandom rand;
final int SIZE = 20;
public Math ()
{
problems = new int [SIZE];
rand = new SecureRandom();
}
public void setProblems()
{
Date today = new Date();
rand.setSeed(today.getTime());
for (int i=0; i<problems.length;i++)
{
problems[i] = 10+ rand.nextInt(90);
}
Arrays.sort(problems);
}
public int [] getProblems()
{
return problems;
}
public String getStringOfNumbers()
{
this.setProblems();
System.out.printf("problem length %d", Arrays.toString(problems).length());
return Arrays.toString(problems);
}
public void setValueOne (int one)
{
this.valueOne = one;
}
public void setValueTwo (int two)
{
this.valueTwo = two;
}
public void setOperation (String op)
{
op.trim();
char ch = op.charAt(0);
System.out.println(op);
char symbol = '+';
switch (ch)
{
case 'A'| 'a': symbol = '+';
break;
case 'S'| 's': symbol = '-';
break;
case 'M'| 'm': symbol = '*';
break;
case 'D'| 'd': symbol = '/';
break;
}
this.operation = symbol;
}
public String setProblem ()
{
Date today = new Date();
rand.setSeed(today.getTime());
setValueOne (10+ rand.nextInt(90));
setValueTwo (10+ rand.nextInt(90));
return String.format("%d %d: ", valueOne, this.valueTwo);
}
public String checkAnswer( int answer)
{
String result = " incorrect";
switch (operation)
{
case '+': if (valueOne+ valueTwo ==answer )
result = " correct";
break;
case '-': if (valueOne - valueTwo ==answer )
result = " correct";
break;
case '*': if (valueOne / valueTwo ==answer )
result = " correct";
break;
case '/': if (valueOne * valueTwo ==answer )
result = " correct";
break;
}
return result;
}
//display menu.
public String displayOperations()
{
return String.format("\nWelcome to Maths Tutor Service."
+ " The available maths exercises are:"
+ "\n Addition: Enter 'A' or 'a' "
+ "\n Subtraction: Enter 'S' or 's' "
+ "\n Multiplication: Enter 'M' or 'm' "
+ "\n Division: Enter 'D' or 'd' "
+ "\n Enter 'Q' or 'q' to quit\n");
}
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
Math one = new Math();
System.out.println(one.displayOperations());
String in;
in = input.next();
one.setOperation(in);
for (int i =0 ; i< 5; i++)
{
System.out.println( one.setProblem());
int answer = Integer.parseInt(input.next());
System.out.println( one.checkAnswer(answer));
}
}
}
输出:
红色表示服务器正在输入两次 蓝色是空白客户端正在添加而不是我需要欢迎再次开始至少10次