• Post Reply Bookmark Topic Watch Topic
  • New Topic

Continuously Reading From Socket's InputStream  RSS feed

 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I'm writing a really simple 1 on 1 chatting program thing using java.net.Socket and reading each other's messages by
using writeUTF() and readUTF() of java.io.DataOutputStream and java.io.DataInputStream. Thing is, I wanna write a thread for
both sides to continuously read from their respective socket's input streams while ignoring the lack of data coming through like when one
user is not sending a message or something. I've written a dumbed down version of this that only reads one message from only one side
and another one that sends a file, both of which work fine, I guess.

I'm using java.util.Scanner for user input, if that's acceptable, for I am not too familiar with Java's other readers. Also, I just started
teaching myself about java.net.Socket, so I may not be too familiar other than the basics like how to set up a connection and how
to send data using the getOutputStream() member function and stuff like that.

Thank you~
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if you've seen the Oracle tutorial yet -- Lesson: All About Sockets -- if you haven't then you should definitely work through it before you carry on.

As for your idea of having the client and the server each read data continuously from the other, I don't think that's a good architecture. In a real-life application each of them should know in advance what input they are expecting, and then read just that input. For that to happen you need to design the communications protocol for the application. This would describe the kinds of data to be sent -- and each kind would be a finite number of bytes described by a format -- and the conditions under which each of the partners should expect to receive each kind of data.

Having each partner just read stuff continuously sounds good, because you don't have to think about what the application does too much. But in real life you're going to run into technical issues like how you can tell when the application ends and how you can distinguish that from the network connection dropping.

As for the Reader to use: by asking that you've already decided that the communication is going to be text only, no binary non-text data. Which is quite likely fine for a chat application as long as you don't plan to include file transfer as one of the features. The only firm requirement is that the Reader shouldn't do any buffering, otherwise the data from the partner might not fill the buffer and then you've got a deadlock. So BufferedReader is definitely out. As an antique programmer I'm not that familiar with Scanner but if it doesn't do buffering then it's okay to use it.
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then how do real time chatting programs like Skype work? In context of 1 : 1 chatting, they do seem to continually get text data from each client as it sent from the opposite client.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how they work. It may appear that they continually get data from each other, which is because they do. However I'm certain that they have a communications protocol which dictates how they do that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!