Originariamente inviato da VincenzoTheBest
Il problema potrebbe essere a livello di rete e non nell'applicazione. Quindi, potrebbe dipendere da dove hai eseguito il test.
Intanto grazie per la risposta. Comunque potrebbe darsi. Ma lo escluderei solamente perchè è stato testato in locale(clients e server in localhost), e su calcolatori differenti nella stessa rete locale(clients su due e server su un altro), e in entrambi i casi tutte le altre richieste, che il client faceva al server(un altro esempio,oltre quelli già scritti sopra è la lista degli interessi di un utente che viene correttamente inviata dal server (a seguito della richiesta) e ricevuta dal client), funzionano alla grande.
Ora posto il codice, che in questo momento, si occupa di inviare un messaggio destinato al client Y inviato da un client X;
il codice,ovviamente lato server, sarà un thread che si occupa di controllare una "lavagna virtuale" in cui sono scritti tutti i messaggi per Y e una volta trovatone uno lo invia tramite l'oggetto ObjectOutputStream;
Non è ben indentato a causa del fatto che è stato copiato e incollato.
codice:
public class Client extends Thread{
public Client(Socket client) throws IOException{
this.client = client;
this.in = new ObjectInputStream(client.getInputStream());
this.out = new ObjectOutputStream(client.getOutputStream());
}
public Socket getClientSocket() {
return client;
}
public void setClientUsername(String clientUsername) {
this.clientUsername = clientUsername;
}
public void run(){
try{
receiveMess();
}
catch(IOException ex){
System.out.println(ex.getMessage());
}
}
public void receiveMess() throws IOException{
while(true){
ArrayList<String> myBoard = ServerAccept.getBoard(this.clientUsername);
if(!myBoard.isEmpty()){
String x = (String) myBoard.remove(myBoard.size() - 1);
this.out.writeObject(new Packet(666, "PROVA")); /*uscito il pacchetto da qui non se ne ha più notizia, aggiungo che Packet(classe creata da me) implementa Serializable e funziona in tutti gli altri casi in cui l'ho usato */
}
}
}
public ObjectOutputStream getOut(){
return this.out;
}
public String getUsername(){
return this.clientUsername;
}
public ObjectInputStream getIn(){
return this.in;
}
public void sendMess(Object payload) throws SQLException, IOException{
Vector received;
Vector toSend;
PreparedStatement query;
Connection con = null;
ResultSet rs;
Packet response;
int id;
received = (Vector) payload;
try{
con = Database.getCon();
query = con.prepareStatement("SELECT `idUser` FROM `AdvancedChat`.`User` WHERE `Username` = ?");
query.setObject(1, received.get(2));
rs = query.executeQuery();
rs.next();
id = rs.getInt("idUser");
toSend = new Vector();
toSend.add(0,received.get(0));//sender
toSend.add(1,received.get(1));//messaggio
response = new Packet(6,toSend);
ArrayList<String> receiverBoard = ServerAccept.getBoard((String) received.get(2));
System.out.println(received.get(1)+" "+received.get(2));
/*il server virtualizzato per l'utente X aggiungie alla "lavagna" per l'utente Y il messaggio da X verso Y*/
receiverBoard.add((String) received.get(1));
}
catch(SQLException ex){
response = new Packet(666, "Ci sono dei problemi tecnici nella Chat");
this.out.writeObject(response);
}
}
}
private ObjectInputStream in;
private ObjectOutputStream out;
private String clientUsername;
private Socket client;
}