I'm trying to make a cllient-server chat that only needs to be able to handle 1 connection.
First, I have a data-type question:
1.
At the moment I am using this code to get messages
BufferedReader ins = new BufferedReader(new InputStreamReader(sock.getInputStream()));
And this code to send messages
PrintStream ios = new PrintStream(sock.getOutputStream());
I have seen different data-types being used, at first I used DataInputStream but that didn't work very well for me.
Are these 2 ways good ways to get and send data, or should I be using something else?
And then I have a question about how to make my code to be able to send and receive messages at the same time
2.
At the moment, The server can only send messages and the client can only receive them.
It seems I must use Threads but what should be in the run function and how should I change my client.java and server.java to compensate?
//This is how I send messages on server right now:
PrintStream ios = new PrintStream(sock.getOutputStream());
while(true)
{
ios.println(scn.nextLine());
if(1==2)break;
}
//And this is how I receive them on my client:
BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line = null;
while( (line=is.readLine()) != null )
{
System.out.println(line);
}
3. In the above code you see "... != null", can it EVER be null?
Should I change that to something else?