Re: NIO



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.
>


.



Relevant Pages

  • Socket communication on multihomed box
    ... I'm building a kind of server which is supposed to handle ... The thing is that connection MUST always stay open. ... as for sockets, I'm using native syscalls, not MFC. ... Another thing is that is should work both under Linux and Windows. ...
    (microsoft.public.win32.programmer.networks)
  • Socket communication on multihomed box
    ... I'm building a kind of server which is supposed to handle ... The thing is that connection MUST always stay open. ... as for sockets, I'm using native syscalls, not MFC. ... Another thing is that is should work both under Linux and Windows. ...
    (comp.programming)
  • Re: Problem with a Socket server program opening/accepting many connections and the GC is running.
    ... I'm not quite sure what you mean by "slowed down my server by adding a 10ms ... to handle the connection as quickly as possible? ... other clients requests are being serviced. ... >> available sockets. ...
    (microsoft.public.dotnet.framework.performance)
  • My server sometimes goes deaf to certain hosts
    ... For some reason my server is sporadically ignoring incoming ... they get "connection refused". ... I'm wondering if somehow these Unix sockets I've created might ...
    (comp.os.linux.development.apps)
  • TCP Socket Question
    ... Earlier today in my C++ class, our instructor introduced us to sockets. ... string then closes the connection. ... This server and has been used by 1 users. ...
    (comp.unix.programmer)