Re: Non blocking socket query.
From: Alex Fraser (me_at_privacy.net)
Date: 04/08/05
- Next message: shakahshakah_at_gmail.com: "Re: Filtering output with sed or similar utility"
- Previous message: blackdog: "Re: IO question"
- In reply to: Lawrie: "Re: Non blocking socket query."
- Next in thread: David Schwartz: "Re: Non blocking socket query."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: shakahshakah_at_gmail.com: "Re: Filtering output with sed or similar utility"
- Previous message: blackdog: "Re: IO question"
- In reply to: Lawrie: "Re: Non blocking socket query."
- Next in thread: David Schwartz: "Re: Non blocking socket query."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|