[英]Java client block server application
我有一個客戶端服務器應用程序。 我通過客戶端發送一些信息,服務器讀取此信息,進行操作並將結果發送到客戶端。
當我等待客戶端結果時,它阻止了我的服務器應用程序。 服務器收到信息,但不執行我的應用程序...如果我不等待客戶端中的結果,我的服務器將執行我的應用程序,但是...我的客戶端沒有結果。
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);
}
}
我最終決定關閉我的套接字,然后在將結果發送給客戶端后再次打開它。 它不會阻止我的serv應用程序,並且我可以收到結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.