 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hal Vaughan Guest
|
Posted: Sun Mar 19, 2006 10:12 pm Post subject: Sockets and Selectors -- Is Data One Way? |
|
|
I'm using some sockets and selectors. I read from one and write to the
other. Here's where I'm confused: when I write to the second, I would have
expected that data to be outgoing, as in an OutputStream, but once I write
data to that socket (using SocketChannel.wrtie(ByteBuffer), the Selector
keeps returning a key to read from that socket and when I try to read,
there is nothing to read.
I've also had this happen when a ServerSocket (or actually a
ServerSocketChannel) creates a new Socket (or SocketChannel) from
SocketChannel.accept(). When I first send test data through, it works, but
after it has been idle for a while, the Selector keeps providing a key to
read from the socket, but there's nothing to read. I solved it with the
SocketChannel created by ServerSocketChannel.accept() by closing it once
there was no more data so ServerSocketChannel had to accept() again if more
data came in. (Isn't that time consuming and likely to lead to lost data
-- forcing it to open a new socket after every pause in data?)
I can post source code, but on this topic, I've found when I've been posting
the source (which is long), I'm not getting responses and when I post the
questions, I am more likely to get help. (I have no idea why!)
Thanks for any help on this.
Hal |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 21, 2006 11:12 pm Post subject: Re: Sockets and Selectors -- Is Data One Way? |
|
|
My guess is that the socket is getting closed unexpectedly. I think
when the socket is shut down or closed, the selector will select it
with the OP_READ bit ready. Attempting to read from it will return -1,
and you are expected to close the socket at that point. It's only a
guess, though. I could try to look at code if you wanted to send any. |
|
| Back to top |
|
 |
Hal Vaughan Guest
|
Posted: Thu Mar 23, 2006 8:12 pm Post subject: Re: Sockets and Selectors -- Is Data One Way? |
|
|
Thanks for the reply, and sorry it took me, as the one asking for help, to
get back here. I've been trying a lot of different things, including other
programs and such. It is very hard to find good info on this, either in
Java, or in other languages. I'm beginning to think the number of
programmers that actually work with with networking, sockets, and
communication, must be rather small. I'm sure there are people who open
sockets for communication, but not for anything more than just retrieving
files or e-mail or something where the protocols are all laid out.
mandelin (AT) cs (DOT) berkeley.edu wrote:
| Quote: | My guess is that the socket is getting closed unexpectedly. I think
when the socket is shut down or closed, the selector will select it
with the OP_READ bit ready. Attempting to read from it will return -1,
and you are expected to close the socket at that point. It's only a
guess, though. I could try to look at code if you wanted to send any.
|
I'm not getting any -1 lengths, all 0. I also added in debugging lines to
see if the socket was half closed in either direction, and didn't get
anything. I've seen one or two books that include some info, including one
with a port forwarder, but they don't get too detailed and generally
present an example or two and that's it. The one with the port forwarder
does not use Selector.select() and, instead, used a loop to wait for data,
which I figure is VERY time consuming on the CPU and want to avoid.
Am I right that when I write data to a socket, it is outbound, and should
not be seen at all by my program after it has been sent out?
And here's another point that confuses me. I thought once a connection was
opened, the socket should stay open until it was no longer needed. From
what I've seen, at least with server sockets, you open with accept(),
handled the data that is there, and when there's no stream, close the
socket and wait for a new one to be created when there's more incoming
data. But that doesn't tell me what to do in that case when there's been
no incoming data, and you need to send data back to the client you were
connected to a second ago.
Hal |
|
| Back to top |
|
 |
Dave Mandelin Guest
|
Posted: Mon Mar 27, 2006 7:12 pm Post subject: Re: Sockets and Selectors -- Is Data One Way? |
|
|
Hal Vaughan wrote:
| Quote: | Thanks for the reply, and sorry it took me, as the one asking for help, to
get back here. I've been trying a lot of different things, including other
programs and such. It is very hard to find good info on this, either in
Java, or in other languages. I'm beginning to think the number of
programmers that actually work with with networking, sockets, and
communication, must be rather small. I'm sure there are people who open
sockets for communication, but not for anything more than just retrieving
files or e-mail or something where the protocols are all laid out.
|
That may be. I am not an expert on networking myself, but I have
successfully programmed network applications before, including a
client/server VoIP application just this week, so I am confident we can
get your program working.
One general point to keep in mind is that minor variations in drivers
and network stack implementations can have suprising effects, so that
may be what you are seeing. But your application seems fairly standard,
so I think it should not be affected by such things.
| Quote: | mandelin (AT) cs (DOT) berkeley.edu wrote:
My guess is that the socket is getting closed unexpectedly. I think
when the socket is shut down or closed, the selector will select it
with the OP_READ bit ready. Attempting to read from it will return -1,
and you are expected to close the socket at that point. It's only a
guess, though. I could try to look at code if you wanted to send any.
I'm not getting any -1 lengths, all 0. I also added in debugging lines to
see if the socket was half closed in either direction, and didn't get
anything. I've seen one or two books that include some info, including one
with a port forwarder, but they don't get too detailed and generally
present an example or two and that's it. The one with the port forwarder
does not use Selector.select() and, instead, used a loop to wait for data,
which I figure is VERY time consuming on the CPU and want to avoid.
|
Yes. I believe the reasonable alternatives for a Java server are
multithreading and select. I think multithreading is simpler to
program, but select may be higher performance. I'm not really sure
about the performance, though.
If the length is 0, the socket is not actually ready for reading. My
guess is that there is a bug in the code causing the read code to be
run even though select, by its proper semantics, has not actually
indicated that data is ready. If you send code I will read it for such
errors.
| Quote: | Am I right that when I write data to a socket, it is outbound, and should
not be seen at all by my program after it has been sent out?
|
Yes.
| Quote: | And here's another point that confuses me. I thought once a connection was
opened, the socket should stay open until it was no longer needed. From
what I've seen, at least with server sockets, you open with accept(),
handled the data that is there,
|
Sounds fine so far.
| Quote: | and when there's no stream, close the
socket and wait for a new one to be created when there's more incoming
data.
|
This I don't quite understand. What does "when there's no stream" mean?
| Quote: | But that doesn't tell me what to do in that case when there's been
no incoming data, and you need to send data back to the client you were
connected to a second ago.
|
I don't quite understand the question, but in a typical TCP
client/server setup, you keep the sockets connected as long as the
server may need to send information back to the client.
--
Need to get from a Foo object to a Bar object in Java?
Ask Prospector: http://snobol.cs.berkeley.edu
Want to play tabletop RPGs over the internet?
Check out Koboldsoft RPZen: http://www.koboldsoft.com |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|