Soel
28-01-2009, 17:36
Salve a tutti, avrei un problema con la trasmissione di dati tramite stream in Java. In particolare, il problema riguarda un server multithread. Ho bisogno di far convivere su ogni thread un DataInputStream (che riceve un messaggio iniziale) ed un BufferedReader (che riceve tutti i successivi). Il primo messaggio giunge a destinazione correttamente, ma il problema è che, nel momento in cui sposto la ricezione dal DataInputStream al BufferedReader, il server non sempre riesce a ricevere i messaggi che dovrebbe. Talvolta, senza logica apparente, invece dei messaggi corretti riceve un valore "null". Dico senza logica apparente perchè, lanciando più volte il programma, un messaggio che la volta precedente era arrivato a destinazione non è detto che ci riesca di nuovo, e viceversa, messaggi che all'esecuzione precedente erano stati sostituiti da "null" possono invece giungere correttamente al server.
Il codice è stato precedentemente provato in condizioni di connessione singola client-server senza thread, e in quel caso tutto funziona come dovrebbe. Inoltre il client mi segnala in entrambi i casi di essere riuscito ad inviare tutto.
Aggiungo che purtroppo il sistema a due stream con cui vengono inviati i messaggi, così come la struttura dei messaggi e più in generale l'architettura di comunicazione, non possono essere cambiati. Il mio lavoro consiste nel ricevere i dati inviati da un client scritto da altri che comunica in questo modo, e non mi è concesso in alcun modo modificarlo.
Se può servire, programmo con Eclipse su Mac OSX 10.4.10, con Java alla versione 1.5.0_13. Grazie anticipatamente per l'aiuto.
Questo è il codice:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
Socket socketuno, socketdue, sockettre;
try {
socketuno = new Socket(InetAddress.getLocalHost(), 8000);
System.out.println("Client 1: Connessione effettuata!");
DataOutputStream ostuno = new DataOutputStream(new BufferedOutputStream(socketuno.getOutputStream()));
String rfuno = "Prova readfully 1!";
byte[] contentBytesuno = rfuno.getBytes("US-ASCII");
int msgLengthuno = contentBytesuno.length + 1;
ostuno.writeInt(msgLengthuno);
System.out.println("Lunghezza 1 spedita!");
ostuno.write(contentBytesuno);
System.out.println("Contenuto 1 spedito!");
ostuno.write(0);
ostuno.flush();
PrintWriter outuno = new PrintWriter (new BufferedOutputStream(socketuno.getOutputStream()));
outuno.println("Prova printwriter 1!");
outuno.flush();
System.out.println("Stringa 1 spedita!");
socketdue = new Socket(InetAddress.getLocalHost(), 8000);
System.out.println("Client 2: Connessione effettuata!");
DataOutputStream ostdue = new DataOutputStream(new BufferedOutputStream(socketdue.getOutputStream()));
String rfdue = "Prova readfully 2!";
byte[] contentBytesdue = rfdue.getBytes("US-ASCII");
int msgLengthdue = contentBytesdue.length + 1;
ostdue.writeInt(msgLengthdue);
System.out.println("Lunghezza 2 spedita!");
ostdue.write(contentBytesdue);
System.out.println("Contenuto 2 spedito!");
ostdue.write(0);
ostdue.flush();
PrintWriter outdue = new PrintWriter (new BufferedOutputStream(socketdue.getOutputStream()));
outdue.println("Prova printwriter 2!");
outdue.flush();
System.out.println("Stringa 2 spedita!");
sockettre = new Socket(InetAddress.getLocalHost(), 8000);
System.out.println("Client 3: Connessione effettuata!");
DataOutputStream osttre = new DataOutputStream(new BufferedOutputStream(sockettre.getOutputStream()));
String rftre = "Prova readfully 3!";
byte[] contentBytestre = rftre.getBytes("US-ASCII");
int msgLengthtre = contentBytestre.length + 1;
osttre.writeInt(msgLengthtre);
System.out.println("Lunghezza 3 spedita!");
osttre.write(contentBytestre);
System.out.println("Contenuto 3 spedito!");
osttre.write(0);
osttre.flush();
PrintWriter outtre = new PrintWriter (new BufferedOutputStream(sockettre.getOutputStream()));
outtre.println("Prova printwriter 3!");
outtre.flush();
System.out.println("Stringa 3 spedita!");
} catch (UnknownHostException e) {
System.err.println("Host sconosciuto");
} catch (EOFException e) {
System.out.println("Connessione chiusa dal server!");
} catch (Exception e) {
System.err.println(e);
}
}
}
import java.net.*;
public class Server {
public static void main(String args[]) {
int i=1;
try {
ServerSocket server = new ServerSocket(8000);
System.out.println("SERVER in esecuzione...");
while(true) {
Socket client = server.accept();
System.out.println("Client "+i+" raggiunto!");
ServerThread T = new ServerThread(client, i);
T.start();
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
import java.io.*;
import java.net.*;
public class ServerThread extends Thread {
Socket client;
int numero;
public ServerThread(Socket socket, int num) {
this.client = socket;
this.numero = num;
}
public void run() {
try {
System.out.println("Thread "+numero+" lanciato!");
DataInputStream ist = new DataInputStream(new BufferedInputStream(client.getInputStream()));
int msgLen = ist.readInt();
System.out.print("Lunghezza "+numero+" ricevuta! ");
System.out.println(msgLen);
byte[] buf = new byte[msgLen];
ist.readFully(buf, 0, msgLen);
System.out.print("Byte "+numero+" ricevuti! ");
String data = new String(buf, 0, msgLen - 1, "US-ASCII");
System.out.println(data);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = in.readLine();
System.out.print(numero+": ");
System.out.println(line);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Il codice è stato precedentemente provato in condizioni di connessione singola client-server senza thread, e in quel caso tutto funziona come dovrebbe. Inoltre il client mi segnala in entrambi i casi di essere riuscito ad inviare tutto.
Aggiungo che purtroppo il sistema a due stream con cui vengono inviati i messaggi, così come la struttura dei messaggi e più in generale l'architettura di comunicazione, non possono essere cambiati. Il mio lavoro consiste nel ricevere i dati inviati da un client scritto da altri che comunica in questo modo, e non mi è concesso in alcun modo modificarlo.
Se può servire, programmo con Eclipse su Mac OSX 10.4.10, con Java alla versione 1.5.0_13. Grazie anticipatamente per l'aiuto.
Questo è il codice:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
Socket socketuno, socketdue, sockettre;
try {
socketuno = new Socket(InetAddress.getLocalHost(), 8000);
System.out.println("Client 1: Connessione effettuata!");
DataOutputStream ostuno = new DataOutputStream(new BufferedOutputStream(socketuno.getOutputStream()));
String rfuno = "Prova readfully 1!";
byte[] contentBytesuno = rfuno.getBytes("US-ASCII");
int msgLengthuno = contentBytesuno.length + 1;
ostuno.writeInt(msgLengthuno);
System.out.println("Lunghezza 1 spedita!");
ostuno.write(contentBytesuno);
System.out.println("Contenuto 1 spedito!");
ostuno.write(0);
ostuno.flush();
PrintWriter outuno = new PrintWriter (new BufferedOutputStream(socketuno.getOutputStream()));
outuno.println("Prova printwriter 1!");
outuno.flush();
System.out.println("Stringa 1 spedita!");
socketdue = new Socket(InetAddress.getLocalHost(), 8000);
System.out.println("Client 2: Connessione effettuata!");
DataOutputStream ostdue = new DataOutputStream(new BufferedOutputStream(socketdue.getOutputStream()));
String rfdue = "Prova readfully 2!";
byte[] contentBytesdue = rfdue.getBytes("US-ASCII");
int msgLengthdue = contentBytesdue.length + 1;
ostdue.writeInt(msgLengthdue);
System.out.println("Lunghezza 2 spedita!");
ostdue.write(contentBytesdue);
System.out.println("Contenuto 2 spedito!");
ostdue.write(0);
ostdue.flush();
PrintWriter outdue = new PrintWriter (new BufferedOutputStream(socketdue.getOutputStream()));
outdue.println("Prova printwriter 2!");
outdue.flush();
System.out.println("Stringa 2 spedita!");
sockettre = new Socket(InetAddress.getLocalHost(), 8000);
System.out.println("Client 3: Connessione effettuata!");
DataOutputStream osttre = new DataOutputStream(new BufferedOutputStream(sockettre.getOutputStream()));
String rftre = "Prova readfully 3!";
byte[] contentBytestre = rftre.getBytes("US-ASCII");
int msgLengthtre = contentBytestre.length + 1;
osttre.writeInt(msgLengthtre);
System.out.println("Lunghezza 3 spedita!");
osttre.write(contentBytestre);
System.out.println("Contenuto 3 spedito!");
osttre.write(0);
osttre.flush();
PrintWriter outtre = new PrintWriter (new BufferedOutputStream(sockettre.getOutputStream()));
outtre.println("Prova printwriter 3!");
outtre.flush();
System.out.println("Stringa 3 spedita!");
} catch (UnknownHostException e) {
System.err.println("Host sconosciuto");
} catch (EOFException e) {
System.out.println("Connessione chiusa dal server!");
} catch (Exception e) {
System.err.println(e);
}
}
}
import java.net.*;
public class Server {
public static void main(String args[]) {
int i=1;
try {
ServerSocket server = new ServerSocket(8000);
System.out.println("SERVER in esecuzione...");
while(true) {
Socket client = server.accept();
System.out.println("Client "+i+" raggiunto!");
ServerThread T = new ServerThread(client, i);
T.start();
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
import java.io.*;
import java.net.*;
public class ServerThread extends Thread {
Socket client;
int numero;
public ServerThread(Socket socket, int num) {
this.client = socket;
this.numero = num;
}
public void run() {
try {
System.out.println("Thread "+numero+" lanciato!");
DataInputStream ist = new DataInputStream(new BufferedInputStream(client.getInputStream()));
int msgLen = ist.readInt();
System.out.print("Lunghezza "+numero+" ricevuta! ");
System.out.println(msgLen);
byte[] buf = new byte[msgLen];
ist.readFully(buf, 0, msgLen);
System.out.print("Byte "+numero+" ricevuti! ");
String data = new String(buf, 0, msgLen - 1, "US-ASCII");
System.out.println(data);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = in.readLine();
System.out.print(numero+": ");
System.out.println(line);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}