0
你好,我在我的應用程序java上有一個錯誤,這個應用程序發送客戶端到服務器的值,服務器執行2的權力收到價值,並返回給客戶端,一切正常,但是一旦服務器不再接收數值,它只會播放一次,我不明白問題所在,我該如何解決這個問題?代碼如下錯誤應用程序java客戶端-server
客戶:
import java.io.*;
import java.net.*;
public class Client{
public static void main(String args[]){
try{
//controllo che i valori siano corretti
/* if(args.lenght != 2){
System.out.println(" Errore la sintassi ");
System.exit(1);
}
*/
String host=""+args[0];
int port=Integer.parseInt(args[1]);
// System.out.println("\n Scriverò sul socket "+host);
// System.out.println("\n Porta: "+port);
//leggo valori da input
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Inserisci un valore (CLIENT): ");
String lettura=input.readLine();
while(!lettura.equals("fine")){
System.out.println("\n Inserisci un valore (CLIENT): ");
Socket s=new Socket(host,port);
PrintWriter output=new PrintWriter(s.getOutputStream());
output.println(lettura);
output.flush();
//attendo dal server il risultato
BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream()));
String lettsock=insok.readLine();
//System.out.println(" Ho ricevuto dal server il valore "+insok.readLine());
System.out.println("\n Ho ricevuto dal server : "+lettsock);
lettura=input.readLine();
}
// System.out.println("\n Client Terminato con successo \n");
//chiusura input
input.close();
}
catch(Exception ex){
System.out.println("\n Errore: " + ex);
System.exit(2);
}
}
}
SERVER:
import java.io.*;
import java.net.*;
public class Server{
static final int PORT = 1024;
public static void main(String args[]){
try{
ServerSocket ss=new ServerSocket(PORT);
while(true) {
Socket ns = ss.accept();
BufferedReader networkIn = new BufferedReader(new InputStreamReader(ns.getInputStream()));
PrintWriter networkOut=new PrintWriter(ns.getOutputStream());
String line;
System.out.println("\n Attendo valori \n ");
while ((line = networkIn.readLine()) != null) {
System.out.println("Ricevuto: " + line);
try{
int number=Integer.parseInt(line);
number=number*number;
networkOut.println(number);
networkOut.flush();
}
catch(Exception ex){
System.out.println("\n Non mi hai inviato un numero \n");
}
}
ns.close();
}
}
catch(Exception ex){
System.out.println("\n Errore Server: "+ex);
System.exit(2);
}
}
}
謝謝你的開始 –