Server programming question

This is a discussion on Server programming question within the C++ Programming forums, part of the General Programming Boards category; There is a game toolkit I happen to like to use in my spare time when I'm bored, however it ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    66

    Server programming question

    There is a game toolkit I happen to like to use in my spare time when I'm bored, however it lacks online utility. It does however provide DLL support so I'm working on adding that functionality to the kit by creating a DLL that turns the game into a client and then I'm also working on a server for the client to connect to.

    I'm using WinSock for all of this as it's the only net protocol I've used so far in my meager experience however I've never done any real server programming, nothing more then just one client at a time.

    So my question is how do I support multiple connections? I've read many many files on Winsock, tutorials walk throughs guides etc, but I don't seem to understand how I can keep and control multiple connections short of hard coding 255 sockets or something. The first thing I tried was an array of sockets, which was fine and dandy except for some reason (I forget the error sorry!) it wouldn't compile with an array. Maybe I was doing it wrong, but I had the same problem with a vector.

    Now I'm thinking unless I did those wrong and I could use them I might try a linked list, which I'm certain would work but it would seem inefficient because players don't log on and off in any given sequence.

    So, can anyone tell me if there is a set in stone way to do this or if I was simply using arrays/vectors wrong? ( I can dig the code up if you need me to, but if anyone knows that you can for sure use arrays or vectors with winsock then I can just go over it my code a few more times myself I'm sure )

    PS: I haven't worked on this project in a few months but I want to start it up again which is why I'm asking these questions without much clearness. So I'm sorry about that! I'm about to leave for my night classes so I figured I'd just put this out there to see if anyone had answers and if not well I can always try to find a good sample code to look at ^^
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Read up on listen() / accept()
    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
    Join Date
    Oct 2007
    Posts
    66
    If I have this right now I'm going to feel pretty stupid.

    So I create a socket and listen() with it, then I accept incoming connections and get the actual socket that made the connection from the return value of accept() meaning I would have code like this:

    Code:
    socket s;
    
    ...
    
    s.listen();
    
    ...
    
    socket mySockets[5];
    
    ...
    
    mySockets[0] = s.accept();
    (and yeah I know that code wouldn't compile, but is that the general idea? I'm going to be testing it right now but if I make a mistake and implement it wrongly then it'd be nice to know if I just implemented it wrongly and had the right concept)
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Yes, each successive return from accept is another socket you store in your array.

    Then say add all those sockets to an FD_SET you pass on to select() to see what each client is up to, and react accordingly.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Horray things are starting to work in this program!

    I wonder how I missed something like this though when I was originally doing it, I practically lived in MSDN for a week trying to figure it out and never understood it...Then I read listen/accept up on msdn and it makes sense. :doh:

    Thanks a lot Salem, I might be able to finish my project now thanks to you ^_-

    (Which if I do I will definitely mention your name in the about section!)
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server side C++ question
    By twist2b in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2008, 06:09 PM
  2. winsock server question?
    By kocho in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2006, 12:52 PM
  3. Web Server in Router Network...
    By Aidman in forum Tech Board
    Replies: 15
    Last Post: 01-17-2003, 09:24 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

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