I have a chat that exchanges messages ( String
) with each other. Everything is working properly. But now, I want to start sending objects via Socket
, for example, a class that has some attributes set by me (eg, name, IP, host name, time, message, etc) .
My question is:
When I use only String
, to receive the data, I do it as follows:
Scanner s = new Scanner(servidor.getInputStream());
while (s.hasNext()) {
cliente.writeHistorico(s.nextLine());
}
And to send:
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("mensagem aqui);
PS: Remembering that they are always within threads and in LOOP. Until then, no secret. However, when do I do to read an object:
readObject = new ObjectInputStream(input);
User s = (User) readObject.readObject();
How do I check if the client has sent data to the server or not? Since that way, I do not have, for example, a " hasNext()
". Because if I leave a while(true)
, with the instance of ObjectInputStream
off, it does not work, and if left inside the loop will be creating multiple instances. Is there any way to do that? I think I screwed up a bit, but that's it. I have already looked for material on the net and all possible examples show without using a loop with UM connected client, even in the sockets documentation it shows only how to exchange messages ( String
) and only one user .. < p>
EDIT:
I made a very basic example. Class read server objects:
public class ThreadRecebedor extends Thread implements Runnable {
InputStream inputCliente;
ObjectInputStream input;
User user;
public ThreadRecebedor(InputStream inputCliente) {
this.inputCliente = inputCliente;
}
@Override
public void run() {
try {
//while (true) {
input = new ObjectInputStream(inputCliente);
Object aux = input.readObject();
if (aux instanceof User) {
user = (User) aux;
System.out.println(user.getNome());
System.out.println(user.getMsg());
System.out.println(user.getHora());
}
// input.close();
// }
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException ex) {
Logger.getLogger(ThreadRecebedor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Class to send data:
public class cliente {
public static void main(String[] args) {
try {
Socket cliente = new Socket("192.168.1.7", 1412);
User user = new User();
user.setNome("Teste");
user.setMsg("oi cristiano");
user.setHora(new SimpleDateFormat("HH:mm:ss").format(new Date()));
ObjectOutputStream output = new ObjectOutputStream(cliente.getOutputStream());
output.writeObject(user);
output.flush();
output.reset();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The way the two classes are is working. The server can read the sent object and displays the properties in the console. But now comes the key point: What if I want to pass this data on to the user himself? That way, his socket will be closed. What if I wanted to listen to this client for other objects? How would I do it? I swear I read your comment about 3x, but I could not figure out how to do what I want. I'm sorry for ignorance.