Re: NIO
- From: "Remon van Vliet" <remon@xxxxxxxxxxxx>
- Date: Thu, 16 Jun 2005 14:05:07 +0200
Well, this is not how you're supposed to do it. Like you said, the socket is
non-blocking, which means that it will return from your .write() right away,
and after that calls the close(). The proper way to do this is this :
1) establish a connection using a non-blocking socket connect()
2) wait for finishConnect() to return true
3) At this point you can send data, do so once the OP_WRITE key is selected
after a select() call
4) Write all data (in a secure way, meaning keep track of what you send and
make sure you send it all)
5) Once this is done you can close the connection
Now, be sure you actually need non-blocking sockets to begin with. For a
limited number of connection blocking sockets are much easier to work with.
Use non-blocking sockets only when you wish to manage multiple connection
with one thread.
Remon
<technical0@xxxxxxxxx> wrote in message
news:1118839375.363009.304100@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
> I have a little test server and client application.
> The client connects to the server, sends 10 bytes, then exits and the
> socket closes.
> When the server tries to read these bytes I get an error:
> "An existing connection was forcibly closed by the remote host"
> The sockets are non-blocking, so I guess it makes sense that the
> connection might be closed before all the data is either sent from the
> client, or read on the server. However, I'd like to ensure that all the
> data is sent before the close takes effect.
> I'm using SocketChannels to send the data, then doing a
> SocketChannel.close(). I think I need something like "linger" but doing
> SocketChannel.socket().setSoLinger(...) doesn't seem to help.
> Any ideas?
> Thanks.
>
.
- References:
- NIO
- From: technical0@xxxxxxxxx
- NIO
- Prev by Date: Problem with deleteAll() method of Form object. (J2ME)
- Next by Date: Re: Help with searching numbers
- Previous by thread: Re: NIO
- Next by thread: Re: NIO
- Index(es):
Relevant Pages
|