我有一个客户端-服务器应用程序。我通过客户端发送一些信息,服务器读取此信息,执行操作并将结果发送给客户端。
当我等待客户端产生结果时,它会阻止我的服务器应用程序。服务器接收信息但不执行我的应用程序...如果我不在客户端等待结果,我的服务器将执行我的应用程序但是...我的客户端没有结果。
ps:这是作业,我知道在正常情况下最好使用rmi。
这是我的客户代码:
public static void transfertFile(BufferedReader in,Socket so,long sizeFile) throws IOException{
BufferedReader buffRes;
buffRes = new BufferedReader(new InputStreamReader(so.getInputStream()));
PrintWriter printOut;
printOut = new PrintWriter(so.getOutputStream());
String taille = String.valueOf(sizeFile);
printOut.println(taille);
printOut.flush();
printOut.println("client.Calc");
printOut.flush();
printOut.println("add");
printOut.flush();
printOut.println("10");
printOut.flush();
printOut.println("2");
printOut.flush();
String str;
int n;
String line;
System.out.println("file=>"+ "\n\n");
System.out.println("Sending file...");
while((line = in.readLine())!= null){
str = line;
System.out.println(str);
printOut.println(str);
printOut.flush();
}
in.close();
String res="";
res = buffRes.readLine();
System.out.println("res:"+res);
}
现在我在服务器中的第一个函数(只接收我的文件和信息来执行):
public static String[] transfert(InputStream in, OutputStream out) throws IOException, InterruptedException {
//reception des params et de la class
buffIn = new BufferedReader(new InputStreamReader(in));
String size = buffIn.readLine();
System.out.println("size:"+size);
String nomClasse = buffIn.readLine();
System.out.println("Name class:" + nomClasse + " !");
String methode = buffIn.readLine();
System.out.println("Method :" + methode + " !");
String param1 = buffIn.readLine();
System.out.println("Parameter 1 :" + param1 + " !");
String param2 = buffIn.readLine();
System.out.println("Parameter 2:" + param2 + " !");
String fileReadLine ="";
String file ="";
while((fileReadLine = buffIn.readLine()) != null){
System.out.println(fileReadLine);
file += fileReadLine;
}
System.out.println("file :" + file + " !");
// byte buff[] = DatatypeConverter.parseHexBinary(file);
byte[] buff = file.getBytes();
out.write(buff);
out.close();
out.flush();
//Tableau qui contient les valeurs à connaitre pour excevuter la classe
String param[] = new String[4];
param[0] = nomClasse;
param[1] = methode;
param[2] = param1;
param[3] = param2;
return param;
}
我的第二个函数执行一个方法并将结果发送给客户端:
public void reflexion(String[] param, OutputStream out) {
String className = param[0];
Class saClass;
try {
saClass = Class.forName(className);
try {
Object obj = saClass.newInstance();
try {
Method m = saClass.getMethod(param[1], int.class, int.class);
int res;
try {
Object[] args = {Integer.parseInt(param[2]), Integer.parseInt(param[3])};
res = (int) m.invoke(obj, args);
String taille = Integer.toString(res);
taille = "Le resultat du calcul est:"+res+"\r";
System.out.println(taille);
System.out.println("Envoie du resultat...");
PrintWriter printOut;
printOut = new PrintWriter(out);
printOut.flush();
printOut.checkError();
printOut.print(taille);
printOut.flush();
printOut.close();
System.out.println("fin");
} catch (IllegalArgumentException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (NoSuchMethodException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (InstantiationException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(AcceptClient.class.getName()).log(Level.SEVERE, null, ex);
}
}