Thread: Writing a server in c++

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

    Writing a server in c++

    Hello everyone!

    Well, I'm currently writing a VOIP server in c++; and recently I've ran into a problem.

    I'm having trouble thinking the 'c++ way'. What do I mean by that?

    Its hard to explain, but everytime I start coding, I write C-style code. I don't use classes/inheritance/etc because I never seem to know when its the right thing to do, and when not. I have no technical problems (because Ive already written ftp and irc servers in C) but its the object oriented design that gives me headaches.

    I looked at tons of server source code, but they all seem to be written in C and not C++ (with the exception of filezilla).

    If anyone could point out some tips/links/open source programs, that could help out, Id be greatful

    Just for example, I currently have a "server" class, a "user" class, a "header" class (http-like header) and a "config" class. (Probably going to have a socket class aswell, but thinking of using Boost asio instead). And I dont really like it...

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I'm having trouble thinking the 'c++ way'. What do I mean by that?
    the "OOP way".

    I currently have a "server" class, a "user" class, a "header" class (http-like header) and a "config" class.
    and of course many more. if i were writing a game, for example, starting from a "game" class doesnt really help. sure, there may exists a "game" class eventually, but it is useless without anything inside of it (obviously). similarly, your "server" class wont do much without anything in it. therefore, you will use object composition to mix many different types/objects adding interactions between these types, and defining this collection of types and functions as a "server".

    also, a server is useless if there isnt a "client" to request services from it. note that "client" and "user" are probably two different things. an (end)-user uses the client only as the gateway to your server. therefore, you could define an interface for the client to say how it interacts with the server. you can implement one or more different clients, i.e. a graphical one, a text-based one, a windows one, a unix one, an android one, etc. of course you just start with one, but using the interface allows the client to change and be implemented and presented in different ways.

    i dont know anything about VoIP, but i know theres obviously a protocol it uses. as an example, say you can use either TCP or UDP. if this was the case, you would have another interface to handle this... basically you want to be able to "talk" and "listen" (i.e. the interface). so both the server and clients use this interface to communicate--one connection might use TCP, another might use UDP; it wont matter to the rest of the program, because all of this behaviour is defined in the "connection" interface that both sides use to communicate. also, say another even more reliable protocol was invented, you could add another implementation of this interface to allow this protocol to be used, without making any other changes to the rest of the system.

    Probably going to have a socket class aswell, but thinking of using Boost asio instead
    another key concept in OOP is object reuse. no point reinventing the wheel if you can use something that already exists and does a good job.

    And I dont really like it...
    if you dont like the way your system is turning out, or if things seem unnatural or unintuitive, those should raise some questions about the design of your system. that is, its probably not designed correctly (opposed to you subjectively not liking it).

    this may or may not be of any use to you but i gave it a shot (with simplified examples).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    One thing you can try is this: Write a document that describes exactly what your server will do and how it will do it. Be as detailed as possible.
    When you're done, start picking out all the nouns -- those are good candidates for classes. Then start picking out all the verbs -- those are good candidates for member functions in your classes.

    For example:

    "The FTP Client connects to the FTP Server on TCP port 21 and sends the following commands to send or receive files..."

    Obviously you'd want a little more detail than that, but just from that first sentence you can see you need a File, FTPClient & FTPServer class, and Connect(), Send() & Recieve() functions in the FTPClient class and Send() & Recieve() functions in the FTPServer class.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 10:33 AM
  3. constantly reading and writing to a tcp server
    By liri in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-22-2008, 08:57 AM
  4. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  5. Web Server in Router Network...
    By Aidman in forum Tech Board
    Replies: 15
    Last Post: 01-17-2003, 10:24 PM