Thread: Simple game over network

  1. #1
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101

    Simple game over network

    Hi there.

    I am attempting to write a simple game over a network. Have you ever seen the game 'Anagrammatic' before? Well it's that but simpler. Basically, a client connects to a server and the server pairs the client with another client in order for them to play against each other. Just as if you connected to a game server to play checkers with somebody.

    The problem I'm having is I would like to use select() do deal with the clients, but I'm still having a few difficulties understanding the function. I understand it blocks a specified amount of time until data can be read from/written to a file descriptor. But I'm not sure how to implement the 2 player nature of the game. For example, say I do:

    Code:
    while(1) {
       if(select( fdmax+1,&read_fs,NULL,NULL,NULL)==-1){
           perror("select() failed:");
           exit(1);
       }
    
       for(i = 0; i <= fdmax; i++) {
          if(FD_ISSET(i, &read_fs)) {
            /* ???? */
          }
       }
    
    }
    And i is the listening socket. Say I need to pair that new connection with one already waiting for an opponent, how would I go about that? And how would I keep the client who connected who hasn't got someone to play yet? Do I create an opponent struct and fill it in when two clients are paired or something? I'm guess I'm just generally confused as how to go about the mutliplayer aspect of the project.

    Thanks for any advice.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    ya select() function can be implemented for the same. u have to put the fdes(max. value) + 1 as the 1st arg and then u can use the macro FD_SET(), FD_ISSET() for the fdesc which are in readfs...then keep on checking for every fdesc whether that is set or not using FD_ISSET()...

    also if u want to exclude any fdesc from the readfds set, u can use exceptfds and set all those fdesc there...

  3. #3
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    OK, I think I have the gist of it. I played about with Beej's selectserver.c example and managed to create a server that pairs two clients together and they can exchange messages. Fairly simple. It's quite hard to see what's going on, and I have still not seen a great select() tutorial on the web, even Beej's is hard to get around I thought as he goes from one simple example to one large example. Anyway I think I've got the hang of it. For those interested, see the attached code for my little 2 person chat system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a simple Windows game
    By ejohns85 in forum C Programming
    Replies: 1
    Last Post: 05-22-2009, 12:46 PM
  2. Simple Network Game Programing...
    By IndioDoido in forum Networking/Device Communication
    Replies: 4
    Last Post: 11-07-2007, 06:56 AM
  3. my new (simple) game
    By dune911 in forum C Programming
    Replies: 14
    Last Post: 03-16-2002, 03:20 PM
  4. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 09:24 PM
  5. Replies: 1
    Last Post: 11-06-2001, 02:15 PM