Thread: Best programming practices for multiple connections

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Best programming practices for multiple connections

    Hi folks,

    I am building a Client-Server application which I want it to be portable between Linux and Windows. So here is the idea:

    1. The server deamon gets installed and run on multiple computers.
    2. Then the client program connects to all of these remote machines and obtains couple of data structures and then closes the connection.

    Now, I want to be able to create the client program more like a desktop applet and run constantly, but it will connect to the servers at predefined delay, like once every 5 minutes.

    I am curious what would be the best way to do this? Upon testing of the first prototype, I am seeing that if a server does not respond immediately, due to network congestion, the client freezes waiting for connection to be established or data to be received. This essentially stops from making subsequent connections to the other servers and getting data.

    How can I avoid stalling the client if one of the connections fails? What would be recommended "refresh"? 60 seconds, 5 minutes?

    Any ideas and suggestions would be appreciated. Code examples would be entertained in my testing.

    Thanks in advance!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What the refresh is depends entirely on your application domain, i.e. what refresh time makes sense? An e-mail client may connect to the server every few minutes, an RSS client every hour, a program update test once a session ... you get the idea. In any case, you should make the delay user-configurable.

    As for the freezing, use connecting methods that do not block. If you want to do this portably, use Boost.Asio, at http://asio.sf.net/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Quote Originally Posted by CornedBee View Post
    As for the freezing, use connecting methods that do not block.
    Can you give me example of this? I guess this is what I am looking to find out.

    Also, thanks for the link! I will look into it!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Sorry, not enough experience with networking there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If you haven't found a solution yet:
    Non-blocking methods => Asynchronous or Overlapped sockets.

    I haven't dealt with Overlapped myself; to me, it seems more difficult to use properly, but it may be more suitable for you if you just want to attempt a connection without blocking, and have something happen when the operation completes. For Asynchronous, there are two methods - messages [use WSAAsyncSelect to put your socket in this mode], or events [use WSAEventSelect()]. Either of these two will put the socket into asynchronous mode and register for specific event notifications. You'll also want to look into the WSAxxxx family of functions, there's an equivalent for most of the standard socket functions (i.e. WSASocket, WSASend, WSARecv, etc.), which may play more nicely with asynchronous methods. I'd recommend a tutorial on asynch or overlapped sockets, but I don't have any offhand.

    Good luck.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I am seeing that if a server does not respond immediately, due to network congestion, the client freezes waiting for connection to be established or data to be received. This essentially stops from making subsequent connections to the other servers and getting data.
    set the O_NONDELAY(or O_NONBLOCK) flag on the socket. this should return immediately without blocking allowing you to possibly test for conections that are still open; upon which you can perform decisions on them (for instance you can close it if the connection's been active for more than 5 mins).
    I hope I've understood that part of your problem corretly.
    Last edited by WDT; 10-06-2007 at 10:00 PM. Reason: spelling errors
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Unfortunately there arent likely to be any robust solutions that dont involve OS specific interfaces. In windows, I would start multiple threads each handling a single server, and have each thread open a non-blocking connection to the server, then WaitForSingleObject() on the socket handle, when soemthing happens on the socket, either it recieves the connection or the connection times out, it will release the thread from the wait state and you can check the status of the operation and take appropriate action.

    I imagine you can do 'the same thing' in linux, although I dont know the interfaces to use.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Unless you have to connect to zillions of servers, I'd just multithread it and call it done. Programming using select() is prone to all sorts of errors. Multithread code is, too, but depending on what you're doing, threads could be massively simpler.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Location
    somewhere out there
    Posts
    7
    we have the same problem.. but mine in c...
    is that possible if i connected to the server then the server sends info immediately?
    the server makes the first move although the client requested for it..
    then the client sends a command to the server..
    does the command will be interrupted?
    or is it possible to use different port? for every request..
    port #1 for the server to send data
    port #2 for the clients command request...
    thanks.. links and tutorials will be appreciated..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Persistent connections
    By Niara in forum Networking/Device Communication
    Replies: 6
    Last Post: 09-25-2007, 05:27 AM
  2. Detecting Connections
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-16-2006, 10:12 AM
  3. Select handling more then 500 connections
    By Chronom1 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-27-2005, 03:20 PM
  4. WinXP Network Connections pop-up
    By DavidP in forum Tech Board
    Replies: 1
    Last Post: 10-02-2002, 05:36 PM
  5. Multiple Client Connections
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 04-06-2002, 11:04 PM