Thread: TCP Concurrent Server

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    6

    TCP Concurrent Server

    Hello guys!

    I'm having a little bit of struggle understanding how complex Ca /C++ concurrent servers should work. I have to do a project for faculty and I have a list of projects from where I can choose but I have no idea how should I implement any of them.

    For example, I can understand how simple concurrent servers work, like when a client connects, I accept that connection on a child process, I deal with that client and I close the connection.

    The problems start to appear when my clients won't be disconnected after doing a single action and need to interact between them.

    What kind of data structures should I use so that the clients can communicate between them. Should I keep a global array with their descriptors or how can I assure that the server remains concurrent.

    For example, a chat application. A client is accepted by a child but obviously they will need to send messages to another clients in different rooms. Or a chess game, a child will accept 1 client but the game won't start until we have 2 of them.What do I do then? And if I wait for a second client, what process will be the server which will assure the communication between them?

    I know I sound like a retarded person, but it's getting really frustrating. I passed exams for C#, C++, Java, Php, Html, Css, Sql and others but it's the 3rd year when I'm trying to do something with linux programming and I fail, reason why I can't get my Computer Science license.29 passed exams and this is stopping me. Probably it's a psychological blockage but I really need to understand at least in theory how this should work. Any links or explanation would be very helpful. Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    select(2) - Linux manual page
    With the select system call, you can observe the state of multiple file descriptors at once.

    In the case of a server you describe, you have two classes of descriptor
    - a single descriptor which is the socket used to listen and accept new connections
    - zero or more descriptors which are your current client set.

    A good way to hold all the client descriptors is in a 'fd_set'.

    The basic steps are
    - create a temporary fd_set from the client descriptor fd_set and the listening socket descriptor.
    - call select. Use a timeout if you have other things to do if there is no network activity.
    - use the select result to figure out which descriptors need attention

    For a new incoming connection, you need to (at a minimum) add the new descriptor to client_fdset.
    - You may also need to allocate some instance data, say if it were a chess server, you'd create a new game object.
    If the remote disconnects, remove it from client_fdset.

    For each client fd, call a handler function, passing in the fd (and your instance data pointer) to read the message and update it's state.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    poll(2) - Linux manual page

    I've personally found poll to be a bit easier to work with. It does basically the same thing, but you don't need to deal with the fd_set thing, if you would prefer a simple array. It's also convenient, if you're working in C++, because you can use std::vector<struct pollfd>::data() as the first argument.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tcp chat multiple concurrent clients and 1 server to accomodate file transfer also.
    By Pulock2009 in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-17-2015, 02:50 PM
  2. EPOLL echo server not concurrent
    By lantzvillian in forum C Programming
    Replies: 1
    Last Post: 03-07-2011, 11:48 AM
  3. RPC concurrent server in C...???
    By mr_m4x in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 11:10 PM
  4. Concurrent C
    By kris.c in forum C Programming
    Replies: 0
    Last Post: 04-13-2007, 07:00 PM
  5. Concurrent server problems
    By Qwerty in forum Linux Programming
    Replies: 0
    Last Post: 06-13-2003, 11:52 AM

Tags for this Thread