How to check two sockets at once (without the use of threads) - Java
I'm trying to make a simple instant messaging program where a server sits
on one computer and listens when other clients try to connect. I am able
to connect two clients to the server, and now I want them to communicate.
At first I tried this:
// Receives IMs from one client and sends it to the next.
private void recieveIMs() throws Exception {
// Loops forever
do {
// Gets a message from the 1st input stream and sends
// it out the 2nd output stream.
String mes = (String) inputStream.readObject();
outputStream2.writeObject(mes);
// Gets a message from the 2nd input stream and sends
// it out the 1st output stream.
String mes2 = (String) inputStream2.readObject();
outputStream.writeObject(mes2);
} while (true);
}
Basically it does what the comments say.
With this, they didn't run at the same time so each person would have to
alternate when instant messaging. After that, I tried threads, but without
a loop the program would keep going to the "closeServer" method which
would close all the streams and everything. I'm a beginner that just
watches java tutorials on Youtube, so any help would be helpful!
No comments:
Post a Comment