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?

Edited 4 Years Ago by sha11e: n/a

Hmm a little of topic but maybe before continueing with your current server client have a look at the java RMI? a brief definition of it on oracle is:
The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. RMI provides for remote communication between programs written in the Java programming language.

so maybe google it and check it out its a lot easier, and also has asynchronous support

Edited 4 Years Ago by DavidKroukamp: n/a

Hmm a little of topic but maybe before continueing with your current server client have a look at the java RMI? a brief definition of it on oracle is:
The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. RMI provides for remote communication between programs written in the Java programming language.

so maybe google it and check it out its a lot easier, and also has asynchronous support

I'd rather use Threads.
Can you answer the other questions, then?
Like the data-type one?

I'd rather use Threads.
Can you answer the other questions, then?
Like the data-type one?

Well first of all your : In the above code you see "... != null", can it EVER be null?
Should I change that to something else? is correct it can be null when the end of the stream is reached. and as for your data type it is perfectly fine. there are many different ways just which one you prefer i guess? for example instead of a printstream you could use a dataoutputstream here are the two definitions for the methods respectively as you can see they do the same thing however they do have diferrent functionalities this should help to elaborate what i mean read the definitions for both methods:http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html and http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOutputStream.html

Edited 4 Years Ago by DavidKroukamp: n/a

Well first of all your : In the above code you see "... != null", can it EVER be null?
Should I change that to something else? is correct it can be null when the end of the stream is reached. and as for your data type it is perfectly fine. there are many different ways just which one you prefer i guess?

But when does it reach end of stream, then?
It waits until it gets a message - then it isn't null.

But when does it reach end of stream, then?
It waits until it gets a message - then it isn't null.

while waiting for the message the method will return null if no messages is being sent however when a message is sent data is read through the method and now returns a string and therefore not null. when all data is read the stream will be empty again thus representing null?

heres the definition for readline:
readLine

public String readLine()
throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs

the returns argument is of importance to this thread 'A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached'

Edited 4 Years Ago by DavidKroukamp: n/a

while waiting for the message the method will return null if no messages is being sent however when a message is sent data is read through the method and now returns a string and therefore not null. when all data is read the stream will be empty again thus representing null?

I used this code:

BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  
  String line = null;
  while( (line=is.readLine()) != null )
  {
   System.out.println(line);
  }
  if(line == null) System.out.println("Yea... it's null");

I never got out of the while loop unless I did a CTRL + Z which stopped the whole program.

I used this code:

BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  
  String line = null;
  while( (line=is.readLine()) != null )
  {
   System.out.println(line);
  }
  if(line == null) System.out.println("Yea... it's null");

I never got out of the while loop unless I did a CTRL + Z which stopped the whole program.

because your server is continueosly sending a message

while(true)
   {
   ios.println(scn.nextLine());
   if(1==2)break;
   }

because your server is continueosly sending a message

while(true)
   {
   ios.println(scn.nextLine());
   if(1==2)break;
   }

But it's a Scanner.nextLine()

If I don't spam messages, it shouldn't receive anything?

I commented out that part, I get it now.

Edited 4 Years Ago by sha11e: n/a

But it's a Scanner.nextLine()

If I don't spam messages, it shouldn't receive anything?

Hmm true although you are using a scanner when you enter your first command it will execute the while loop and never come out!

it'd be easier if i could see that entire Scanner method along with the while(true) {}.

but my suggestion would be to make it so that it reads and waits for your input and then sends the message, meaning put you scanner code in the while loop. also try and put a check like this:

String line=null;
while(true) {
if((line=scn.nextLine()) != null) {
   ios.println(line);
} else {
System.out.println("no input");
}
   if(1==2)break;
   }

Edited 4 Years Ago by DavidKroukamp: n/a

What is seo? Look for Program Seo Advice. SEO Techniques, Look for applications Seo, SEO for Beginners. Its simple to list your website. How Look for applications.Everyone likes a great tip, right? Here are 55 fast strategies for seo that even your mom could ...What is seo? Look for Program Seo Advice. SEO Techniques, Look for applications Seo, SEO for Beginners. Its simple to list your website. How Look for applications ...
SEO TIPS

Comments
junk spam
spamming
This question has already been answered. Start a new discussion instead.