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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
Hmm is it clear what I mean?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
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.
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.
(;
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.
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?
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?
ASKER
1 socket = 1 OutputStream and Socket.getOutputStream() always returns the same instance?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
Appreciate the help.. (been working weekend cause of this)
ASKER
Is it not possible to edit previous posts... Spelled accidentally know instead of now...
arghhh.... (;
arghhh.... (;
:-)
ASKER
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.