Re: Non blocking socket query.

From: Alex Fraser (me_at_privacy.net)
Date: 04/08/05


Date: Fri, 8 Apr 2005 13:13:27 +0100


"Lawrie" <stroker_ace@hotmail.com> wrote in message
news:1112950886.797032.111320@l41g2000cwc.googlegroups.com...
[snip]
> Okay, let me see If I have got this.
>
> Inside my main loop I am doing two things:
>
> 1. Reading messages from input queue and adding them to each sockets
> output buffer (Each socket instance is wrapped in a structure with
> containing output buffer, status info etc).
>
> 2. Call select() with a short timeout. If any sockets are writeable
> write as much data as possible to the socket.
>
> Or in pseudo code:
>
> do {
>
> check_input_queue;
>
> if (message arrived) {
> add_message_to each_sockets_output_buffer;
> }
>
> call select() with small timout.
>
> if (any socket is writeable)
> {
> write_as_much_data_as_possible_to_socket;
> )
>
> } while (!fatal error)

Consider what happens if a large burst of messages occurs: you will fill the
socket buffers and then be limited by the select() timeout. It might be
better to have an inner loop getting (and buffering) messages until there
are none available or you have got a significant number, then call select()
etc:

do {
    for (msgs = 0; msgs < N; ++msgs) {
        check_input_queue;
        if (!message_arrived) break;
        add_message_to each_sockets_output_buffer;
    }
    select(...);
    ...
} while (!fatal_error);

I can't tell for sure if this is or is not an issue for you, but it should
be simple enough to experiment.

> I have set my sockets to write only using the shut function because I
> want to prevent remote applications sending data to me. Is there any
> way using select of detecting a remote disconnect?

The socket will be returned in all relevant fd_sets passed to select(). A
subsequent attempt to write to the socket will give you an error and/or
raise SIGPIPE unless the signal is set to SIG_IGN.

Note that if the remote application stops reading from its socket (eg
because it has blocked doing something else, or due to a bug), it will never
become writeable. If you need to know that this has happened in a given
amount of time, you'll need to keep track of when you last managed to send
any data. An excessive amount of buffered data can obviously be used as an
indicator that the reader isn't keeping up.

HTH,
Alex



Relevant Pages

  • Re: BeginReceive return zero length buffer when run ,and work correctly when use step by step debug
    ... Your async socket listens and receives 500 bytes (your total buffer size ... receiveing end, read first 4 bytes and store size of data to come ... If I made loop around the "beginRecv until it will return the ... But the data remains on the socket, until such time as it IS ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Communication speed between blocking call and non blocking cal
    ... You could use event based notification, ... When you use nonblocking I/O, the system may have to perform extra buffering ... data buffer until you say that you are done with it. ... I checked default socket buffer ...
    (microsoft.public.win32.programmer.networks)
  • Re: strange select() behavior
    ... sharing descriptors, might fill that interface buffer. ... What do you do when a blocking socket operation returns ENOBUFS? ... because it's a semantic change. ...
    (comp.unix.programmer)
  • Re: Can someone help me
    ... // contents from the server to the client public class AsynchNetworkFileServer {class ClientHandler {// constructor public ClientHandler(Socket socketForClient) {// initialize member variable socket = socketForClient; // initialize buffer to hold ... // contents of file buffer = new byte; // create the network stream networkStream = new NetworkStream; // set the file callback for reading ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] CONFIG_PACKET_MMAP should depend on MMU
    ... The entire data buffer is allocated as one contiguous lump in NOMMU-mode. ... +#ifndef CONFIG_MMU ... * find out where the socket buffers are so that NOMMU mmap can return the ...
    (Linux-Kernel)