Riki Dev Riki Dev - 1 month ago 12
Java Question

Error application java client -server

Hello i have an error on my application java, this application send value from client to server, the server executes the powers of 2 for value received, and returns it to the client, everything works properly, but it works only once after it plays once the server is no longer receiving the value, I can not understand what the problem is,how can I fix this? the code is below

CLIENT:

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);
}


}

}

Answer

This is likely to do with the fact that the client is opening a socket inside its while loop and then not closing it.
The server isn't aware that the socket hasn't been closed and so still awaits input from it.

Try moving the client connection code (below) to before the while loop.

Socket s=new Socket(host,port);
PrintWriter output=new PrintWriter(s.getOutputStream()); 
BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream())); 

Also, your server's while loop should really have a way of exiting, as currently the only way it can exit is if it throws an exception, which isn't ideal... Maybe make it so that when the client sends the message 'close', it disconnects.

Comments