Ho realizzato i seguenti client e server:
public class Server {
private static int port=2000;
private static int T=0;
private Socket client;
private DataInputStream input;
private DataOutputStream output;
private boolean isDigit(String n) throws NumberFormatException{
try{
Integer.parseInt(n);
return true;
}catch(NumberFormatException nfe){
return false;
}
}
public Server(int port){
this.port=port;
}
public void Start() throws IOException {
while(true){
try{
System.out.println ("Server in partenza sulla porta " + port);
ServerSocket server = new ServerSocket(port);
System.out.println ("Server partito sulla porta " + server.getLocalPort() ) ;
System.out.println ("In attesa di connessioni...");
client = server.accept ();
System.out.println ("Richiesta di connessione da " + client.getInetAddress ());
input =new DataInputStream(new BufferedInputStream (client.getInputStream()));
output = new DataOutputStream( new BufferedOutputStream(client.getOutputStream()));
do{
try{
byte c=input.readByte();
switch(c){
case 10:
System.out.println("mmmmmm");
default:
System.out.println("zzzzzzzzz");
}
}catch(IOException ioex){
System.err.println(ioex.getMessage());
}
}while(true);
}catch(EOFException ex){
System.out.println("Client ha terminato la connessione");
}catch(IOException io){
io.printStackTrace();
}finally{
input.close();
output.close();
client.close();
}
}
}
}
mentre il client è:
public class SocketClient {
private boolean autorizzazione=false;
private Socket socket=null;
private DataInputStream input = null;
private DataOutputStream output = null;
private static boolean DEBUG = false;
public SocketClient() {
}
public void run(GUI frame){
FileConfig f=new FileConfig();
try{
frame.writeTextArea("Trying to connect.....");
f.readProperties();
socket=new Socket(InetAddress.getByName("localhost"),f.getPor t());
input =new DataInputStream(new BufferedInputStream(socket.getInputStream()));
output = new DataOutputStream( new BufferedOutputStream(socket.getOutputStream()));
output.write(10);
output.flush();
}catch (IOException e) {
System.err.println((e.getMessage()));
if(e.getMessage().equals("Connection refused: connect"))
frame.writeTextArea("Connection refused");
else
frame.writeTextArea("Problem with configuration file");
}catch(Exception eof){
System.err.println(eof.getMessage());
}
finally{
try{
output.close();
input.close();
socket.close();
}catch(IOException e){
System.err.println(e.getMessage());
}
}
}
public boolean getAutorizzazione(){
return autorizzazione;
}
}
quello che ottengo è che sul server viene stampato il seguente messeggio di errore:
null
A che è dovuto questo?
tulipan

Rispondi quotando

