Forwarding client data

This is a discussion on Forwarding client data within the Networking/Device Communication forums, part of the General Programming Boards category; G'day, I'm writing a small proxy. Basically I spawn a thread for each client that connects, and use blocking sockets ...

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Forwarding client data

    G'day,

    I'm writing a small proxy. Basically I spawn a thread for each client that connects, and use blocking sockets (with timeouts) to service the client.

    However, with that approach I've run into a snag. Basically my pseudo code is:

    Code:
    while(client or server connection open)
    {
       buffer = read_from_client()
       send_to_server(buffer)
    
       buffer = read_from_server()      /* blocks here */
       send_to_client(buffer)
    }
    However, the server doesn't want to send me any data yet. Since it's waiting for the full client request. So the call basically blocks forever.

    I've thought of peeking at the server socket to see if there's any data to read, and skipping it if not. However, MSG_PEEK has bad performance. Even though I'm only targeting around 64 client threads at a given time.

    So what would be the suggested route here? I could peek, or multiplex between the client and server socket (i.e. with select())? But I'm sure that would hurt performance (each thread would then need to multiplex.

    [edit]
    MSG_PEEK isn't an option. It's recommended against using it in Winsock (I'm targeting Windows and Linux).
    select() or poll() aren't really options.
    Could I just use non-blocking sockets in a loop? Although that may mean I'm using a lot of CPU just to see if data arrived.
    [/edit]
    Thanks!
    Zac
    Last edited by zacs7; 02-16-2010 at 12:40 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > select() or poll() aren't really options.
    Why not?
    select() is available on both systems.

    The only downside to select() on windows is that it can ONLY be used for socket descriptors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    And why block? The only time blocking sockets are an option is when you are guaranteed fast turn-around on server accesses...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by jeffcobb View Post
    And why block? The only time blocking sockets are an option is when you are guaranteed fast turn-around on server accesses...
    So I *should* ditch the treading model and switch to non-blocking sockets?

    Quote Originally Posted by Salem
    The only downside to select() on windows is that it can ONLY be used for socket descriptors.
    And MSDN recommends against it, because performance is poor.
    Last edited by zacs7; 02-16-2010 at 06:02 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    But is it
    a) actually that bad in this case?
    b) a reason to tie yourself to their 'higher performance' solution?

    Or you could go threads and one blocking socket per thread, and let the threading mechanism sort out the waiting game. But no doubt that comes with baggage as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21