-
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.
-
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:)...
-
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.