Link to home
Start Free TrialLog in
Avatar of jerra
jerra

asked on

Socket, InputStream, OutputStream help needed. What is exactly end of stream?

From a socket I use getInputStream & getOutputStream. Can I read and write to these streams at the same time using threads? I mean send data & receive using to separate threads?

My TCP server is waiting for incoming messages. Each message is prefixed with a header that contains info on how long the message is. The data itself coming in over the socket is of unknown length. There could be 1 or 500 messages back to back although less than 20 is more likely. I am currently trying to use the InputStream.read() method since the overall length is unknown. Question then, when this method gives -1 the documentation says it is the end of the stream. Does this mean that the connection is also gone?

The part of my program that handles incoming data should always be waiting for data over the this one and only socket. How do I achieve this? I can't keep on looping over the read method all the time can I?
I tried this but when the stream starts returning -1's the server starts looping so fast that there is a performance issue even though the loop do no work at all because of a check that the returned int must be greater that -1.
Am I doing something fundamentally wrong here?
The idea is that the server object getting the incoming data would create 1 thread for each complete message that is received. This message/thread is then self-sufficient and will continue on its own and eventually die.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jerra
jerra

ASKER

>Can I ask why?
Of course. That's the way it is implemented on the other end and I should use the same stream orientated way of reading and writing. My server will get the incoming messages (one or several backtoback), split them and eventually a reply is sent back. I am using threads to achieve this.
Avatar of jerra

ASKER

I have a incoming stream which I should read from all the time. I should keep on reading from this stream without any interruptions for e.g. processing the incoming data.
Hmm is it clear what I mean?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jerra

ASKER

>If you don't close the input stream, then you should just be able to loop, waiting for the next message. If there's nothing to read, the read call will simply block until there is something.

Okay that is how I thought it would work. I must have something wrong in my code then.
I'll keep the question open for now.
Btw what the heck are you doing with the points? Do you do this in order to maintain your knowledge and stay up to date?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Btw what the heck are you doing with the points?

Well i'm accumulating them for when I go on my next learning curves!

>>Do you do this in order to maintain your knowledge and stay up to date?

Yes, it helps when i'm not actually programming Java (I'm doing web stuff at the moment) to do just that.
Avatar of jerra

ASKER

>Take a look at these tutorials, they might help:
I'll have a look at them again. Have read them several times by now....
Anyways you'll have to wait for those points.

(;
btw, talking of points, i think you've possibly been a little over-generous in putting out 500 here. I'd be happy if you adjusted them downwards.
You'd have to post a Q on

https://www.experts-exchange.com/Community_Support/

to do this...

Tim.
Avatar of jerra

ASKER

>btw, talking of points, i think you've possibly been a little over-generous in putting out 500 here. I'd be happy if you adjusted them downwards.

I have not at least not from my point of view, I have been hitting my head against the wall for 2 days now with my socket problem. I'd rather not contact any admins for this. Rather split the points between you guys. Ok?

Finals questions:
If I use the Socket.getOutputStream() and then use the OutputStream.close() will that in fact be the same as
Socket.shutdownOutput() ? Will these two methods disable all output from this socket?
Avatar of jerra

ASKER

1 socket = 1 OutputStream and  Socket.getOutputStream() always returns the same instance?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jerra

ASKER

Thanks for your replies. I am a bit more confident know when it comes to streams and sockets.
Appreciate the help.. (been working weekend cause of this)
Avatar of jerra

ASKER

Is it not possible to edit previous posts... Spelled accidentally know instead of now...
arghhh.... (;
:-)