0
클라이언트 - 서버 응용 프로그램이 있습니다. 클라이언트에서 일부 정보를 보내고 서버가이 정보를 읽고 작업을 수행 한 다음 결과를 클라이언트에게 보냅니다.Java 클라이언트 블록 서버 응용 프로그램
클라이언트에서 결과를 얻으려고 기다릴 때 내 서버 응용 프로그램을 차단합니다. 서버는 정보를 받지만 내 응용 프로그램을 excecute하지 않습니다 ... 내 클라이언트에서 결과를 기다리지 않으면 내 서버가 내 응용 프로그램을 제외하지만 ... 내 클라이언트에 대한 결과가 없습니다.
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);
}
이제 서버 내 첫 번째 함수는 (내 파일 excecute하는 정보 수신) : 방법을 excecute
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;
}
그리고 제 2 기능 및 전송 클라이언트에게 결과 :
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);
}
}
일반적으로 연결이 안정되면 서버가 다른 스레드를 생성하고 해당 스레드에서 연결을 처리해야합니다. 메인 스레드에서 서버는 포트에서 다시 수신 대기합니다. – SJuan76
멀티 스레드 서버입니다. 당신이 말하는 것을 이해하지 못합니다. 입력을위한 스레드와 아웃 아웃을위한 스레드가 있어야합니까? – user1904731
각각의 경우에서 마지막 하나를 제외한 모든 flush() 호출을 제거하십시오. 당신은 TCP/IP를 미치고 있습니다. 요청 또는 응답이 끝나면 실제로 플러시해야합니다. – EJP