5
I have a chat that exchanges messages (String
) to each other. Everything is working correctly. However, now I want to start sending objects via Socket
, like for example, a class that has some attributes set by me (e.g., name, IP, host name, time, message, etc).
My question is this::
When I only use String
, to receive the data, I do 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 inside threads and in LOOP. Until then, no secret. However, when I will do to read an object:
readObject = new ObjectInputStream(input);
User s = (User) readObject.readObject();
How do I check whether or not the client sent data to the server? Since that way, I don’t have for example, a "hasNext()
". 'Cause if I leave one while(true)
, with the instance of ObjectInputStream
outside, it doesn’t work, and if you leave it inside the loop it will be creating several instances. Is there any way to do this? I think I rolled around a little bit, but that’s it. I’ve looked for material on the net and all possible examples show without using loop with ONE connected client, even in sockets documentation shows only how to exchange messages (String
) and only one user..
EDITED:
I made a very basic example. Server object reading class:
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, it is working. The server can read the sent object and shows the properties in the console. But now comes the key point: What if I want to pass this data 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 understand how to do it I want. I’m sorry for the ignorance.
The problem is that I want to be "listening to the object arrive" and then bump into what I told you, where to instantiate? Where to proceed? I did a very basic test, I’ll post along with the code up there.
– Cristiano Bombazar
@Cristianobombazar I updated my answer according to what I could understand of your doubts. I hope I’ve helped, but it’s worth pointing out that writing a client/server protocol is quite complicated - subject for a whole book chapter - so I gave a super-simple example. You will probably run into more difficulties in the future, so I suggest opening up new questions every time you grab something. Also consider investigating existing client/server frameworks so as not to reinvent the wheel (but if your goal is just to learn, then it’s okay).
– mgibsonbr
Yeah, I want to do more for learning anyway. Now it’s late, tomorrow I’ll check better what you wrote. Thanks for the help.
– Cristiano Bombazar