I am going to add a multiplayer feature in my latest game but cannot find a good example of how to get any socket programming working with Winsock and Allegro 4 in Windows (MSVC).
Is there anyone who have made a small game or program and is willing to show it here or send it to me? Even better if it is (fairly) simple to understand and just shows what is needed to include, link to and basically how to code it to get it to work.
All help would make me go from
to
My advice would be to not use sockets directly but something like this: http://enet.bespin.org/
Sockets are not hard either, but using a wrapper like this simply saves you a few days of working out details already solved there.
Wow that library is incredible easy to build!!! It has a MSVC project file and a Code::Block project file... What you have to do is just open it and compile it...
It's incredible how ENet events are so similar to Allegro events. I also was looking for a networking library I think this is great.
Edit: I have just found an example, I haven't read it but supposedly shows how to create a chat, (Using ENet)
I was looking around for some example programs that use enet
but like others I couldn't find one. So I tried to make one myself.
This is a simple chat program. Multiple clients can join the server
and write messages. Messages will be received by all clients
(including the one that sent it).
Most of the code is a direct copy/paste from the enet tutorial. The
code is definitely not perfect but is suitable for a beginner to look
around in.
server.cpp
client.cpp
Thanks! I'll check it out as soon as I have more time. Hopefully it works without any issues "straight out of the box".
I wrote a simple sockets program recently. The program can act as either a server or a client:
[bamccaig@krypton sockets]$ ./a.out --help
./a.out { -h | --help }
./a.out [ -H | --host=HOST ] [ -P | --port=PORT ]
[ -s | --server ] [ -v | --verbose ]
-h, --help
Show this message.
-H, --host=HOST
Specify the host. This can be either an IP address (IPv4 or
IPv6) or a domain name. For the client, this is the server
to connect to. For the server, this is the interface to
bind to.
-P, --port=PORT
This is the port to connect (client) or bind (server) to.
-s, --server
Specifies to act as a server (bind and listen). The
default behavior is to act as a client (connect).
-v, --verbose
Be verbose.
[bamccaig@krypton sockets]$
[bamccaig@krypton sockets]$ ./a.out --host=127.0.0.1 --port=1337 --server Binding to '127.0.0.1' on port 1337... client '127.0.0.1' connected... client (127.0.0.1): This was a triumph. client (127.0.0.1): I'm making a note here. HUGE SUCESS. server: client '127.0.0.1' closed connection. ^C [bamccaig@krypton sockets]$
[bamccaig@krypton sockets]$ ./a.out --host=127.0.0.1 --port=1337 Connecting to host '127.0.0.1' on port '1337'... > This was a triumph. server (127.0.0.1): OK. > I'm making a note here. HUGE SUCESS. server (127.0.0.1): OK. > quit [bamccaig@krypton sockets]$
It doesn't do a whole lot yet, but it might help to get an idea for how sockets work. Currently the server always responds with "OK." and the "protocol" expects the client to talk, then the server to talk, back and forth (i.e., it's single-threaded, except for the server forking for client connections, with blocking sockets).
The code won't run in Windows though. Most of the socket programming should (with some preprocessor workarounds), but fork certainly will not.
It might run in Cygwin though, but no promises.
The best way to learn socket programming is Beej's Guide to Network Programming. That's how I learned (both times).
The basic order of things for a client is:
Convert user-friendly address string and port to address data structure.
Create socket.
Connect to server using socket and address structure.
Send and receive data.
The basic order of things for a server is:
Convert user-friendly address and port to address data structure.
Create socket.
Bind to address using socket and address structure (technically a client can bind before they connect if they want a specific interface and port, but if they don't bind then connecting will bind for them).
Listen for incoming connections.
Accept incoming connection.
Send and receive data.
If you use blocking sockets then you basically need threads to enable the server to handle multiple clients (and listen for more) and, if desired, for the client also, to send and receive simultaneously. If you use non-blocking sockets (which are a bit more work to setup) then you check instead whether there are new connections, whether there's data to send or receive, etc. No guarantee that it'll be useful to you, but here it is anyway. Again, it won't build in Windows (except for maybe Cygwin) so don't even try to build it.
--- ENet ---
Yes, I just compile it and it works fine.
I have just edited the code above, because I miss a "}" at the end. I took that example from those e-mail list with a bunch of <<<<< all over the place.
link to "libenet.a" if you did a static library or .dll depending. Also remember to link to "ws2_32.lib" and "winmm.lib" and include the "include" folder in your "search directory" and yes works straight out of the box.
Here it's... I think is better than Windows Messenger (just because I compile it)
Is there anyone who have made a small game or program and is willing to show it here or send it to me?
Ah man, you've brought me back.
http://www.allegro.cc/forums/thread/600909
It's almost been 2 years since I built that. Mm.... when is the next speedhack?
Oo, I should port this to the iPhone...
Wow decepto you have just posted a gold mine there...
One of those gold mines I read say this:
Lets look at the properties of each:
TCP:
Connection based
Guaranteed reliable and ordered
Automatically breaks up your data into packets for you
Makes sure it doesn’t send data too fast for the internet connection to handle (flow control)
Easy to use, you just read and write data like its a file
UDP:
No concept of connection, you have to code this yourself
No guarantee of reliability or ordering of packets, they may arrive out of order, be duplicated, or not arrive at all!
You have to manually break your data up into packets and send them
You have to make sure you don’t send data too fast for your internet connection to handle
If a packet is lost, you need to devise some way to detect this, and resend that data if necessary
The decision seems pretty clear then, TCP does everything we want and its super easy to use, while UDP is a huge pain in the ass and we have to code everything ourselves from scratch. So obviously we just use TCP right?
Wrong.
Using TCP is the worst possible mistake you can make when developing a networked game! To understand why, you need to see what TCP is actually doing above IP to make everything look so simple!
And I just wanted mention that ENet (UDP) handle most of those things for you!.
@Dustin: Do you need a dedicated server to run your sever game? because I see still working...
@Dustin: Do you need a dedicated server to run your sever game? because I see still working...
Yeah and the server is still up. Every so often it emails me an error. Kind of cool to know people are still playing it.
Update: http://momeme.com/traingame/
Hahahahahahaha so you're happy knowing people are getting errors from your game??
What company are you using? isn't that expensive or you have it for other purposes too.
I read somewhere that RakNet allows you to do some networking even if you don't have a dedicated server. I think I saw it in the video tutorial that is in their webpage. How can it be?. Something like reading and writing files?
PS: Did you know that your game has a lot of potential? but it's screaming for graphics. I'll download the update.
You can do peer to peer connections -- but that's lame.
I use fluid hosting and pay $52/mo for a VPS with 4 ip addresses.
I run a bunch of domains out of it. Here's the list (although some are no longer active):
Here's how many resources I get for that price:
I can upgrade my resources a lot more before needing to move onto something more serious.
Hahahahahahaha so you're happy knowing people are getting errors from your game??
I made the game in 72 hours, I'm fine with it having glitches.
Update: There are cheaper places than Fluid Hosting but I stay here because their uptime is good. I went through 4 cheaper ones that were too unstable.
Oh I see... And if I want to use my own computer as a game server? I have a dynamic IP so isn't possible, at least I use something like no-ip.com, but they give me a url non an IP numer...
Yeah no-ip works.
You'll have to worry about your router settings then. I would turn on the DMZ (De-Militarized Zone) on your router and set your computer's local ip as the DMZ destination.
I always wonder what can I do with a Dedicated Server (besides having a webpage or a game server)... For example can I install a 3D render program an let it doing rendering processes all the time?
Or they won't allow me to do that?
PS: I thought that that link was an update for the game
Enet looks like quake 2 server/client broadcasting
AMCERASOLI: You can pretty well do anything legal with a dedicated server, but using it for 3D rendering would probably not pan out very well. With a VPS you get a set allotment of processing time. Places like Slicehost will allow you to use any "spare" CPU time that nobody else is using.
I run a simple web server with Slicehost. I like their setup a lot. It isn't super cheap, and the performance of my 256MB slice isn't super awesome, but it has been extremely reliable and consistent. In the last ~18 months there's been one hardware failure at 3am which was fixed by 4am and I received about 4 status updates via email informing me of the progress of the whole situation. I was impressed.
Our dedicated server at corenetworks has a better CPU than my desktop, so it would be ideal for e.g. rendering a blender movie (if i had the server all for myself, that is). The biggest benefit would be that the fan noise of my desktop would be kept down
Oh nice, yes because I was trying to justify having a dedicated server, currently I have no need but if could let it rendering some graphics, having my webpage, and a game server (when my game is ready) I might rent one, but what I need for my game is just a online score table... and my webpage is currently hosted for only 25 E/Year so... I don't need it so far.
Now talking about other thing...
I'm studding the way to have an online score table (the best 12 players) using HTTP, it's that possible?. So far the problem that I have found is that I could upload the info but then the program (client) should download the 12 files which contain the name of the players and the scores and then compare if the score of the client is better that those downloaded form the server and if it is, save it with the specified number position, let's say "3.dat" then upload that file to the server overwriting the one that is there... But if there are 100 people playing the game and doing that process I think would be a little messy and discoordinated, but I could do it I don't know...
I was reading this http://www.example-code.com/vcpp/http.asp but I have no idea I haven't finished my game and this would be the final step if it's possible...
You should use a database that supports atomic operations to store the score info. Then your application would lock the hiscores table, compare the new score to the lowest in the table, and if necessary replace it (i.e., UPDATE or DELETE + INSERT; it's semantics mostly). Then generate the Web page dynamically using an application server technology (PHP, ASP.NET, CGI, etc.). You could even use HTTP POST and an application server technology to handle the submission of a score. It would just be a matter of connecting to your Web server and writing some bare minimum HTTP text into your socket. Or you could write your own server, or use many other alternative server technologies... Of course, whatever solution you use, it runs the risk of leaving the server vulnerable to hackers if you don't do a good job. 
You don't have to use a database, but it's the more reliable way aside from writing a complex platform-dependent atomic solution yourself.
I din't explain well myself, but you gave me a good idea (show the score on a webpage too), Or maybe I'm not understanding you. I want to show the table inside the game. For that reason the game should download the files then compare and then upload just one file if it is necessary (a better score) and overwritten the old one. Now it comes to mind another existential problem? what happens if someone gets exactly the same score than another one that is already on the list? should overwrite it?, or should stay there since was the first one?.
I din't explain well myself, but you gave me a good idea (show the score on a webpage too), Or maybe I'm not understanding you. I want to show the table inside the game. For that reason the game should download the files then compare and then upload just one file if it is necessary (a better score) and overwritten the old one. Now it comes to mind another existential problem? what happens if someone gets exactly the same score than another one that is already on the list? should overwrite it?, or should stay there since was the first one?.
I would do as bamccaig suggested and run an SQL database (SQLite, MySQL, Postgres, etc) on your dedicated server. In your game, you would use an SQL library to connect to your SQL database and 'SELECT' the highscores from the highscores table. Your game wouldn't have to download/upload any files.
As far as overwriting highscores that are the same... that is purely up to you. Personally, if I was playing a game and saw that my score matched the lowest score in the top 10 I would not be expecting to enter my name since I've always viewed the scores as 'Must-be-better-than' to place and not 'must-be-better-than-or-equal'
That sounds cool. But can I do that without a dedicated server? because that was my point. Or is going to be alway necessary a dedicated server?. Can't I use FTP or something?.
I want to show the table inside the game.
Oh, OK. I did misunderstand you. I thought you wanted to show the scores on a Web site. Nevertheless, I'd still encourage an RDBMS solution.
Now it comes to mind another existential problem? what happens if someone gets exactly the same score than another one that is already on the list? should overwrite it?, or should stay there since was the first one?.
I'd agree with Samuel Henderson. You should have to beat a high score to make the list, not just match it.
That sounds cool. But can I do that without a dedicated server? because that was my point. Or is going to be alway necessary a dedicated server?. Can't I use FTP or something?.
Assuming you want high scores to be shared by all players you need to centralize the high scores table. For this you basically need a centralized server. I'm not sure exactly what you mean by a "dedicated" server, but I don't think it matters; it's not necessary. You could use a VPS instead, or your own desktop if it is accessible from the Internet (which can be dangerous, but I digress). As for FTP, I don't think FTP is a reliable way to update the high scores table. I don't use FTP myself, so I could be wrong, but I doubt there's any way for FTP to atomically lock a file, download it, and sometime later update it... I doubt it anyway. In either case, it's a rather sloppy way to do it, IMHO.
I'm beginning to think that you're currently using some Web space provided for cheap with only FTP access and no direct access to the server. I don't think that you'll be able to achieve a reliable solution with a setup like that. FTP is just not a very good way to do this. I'm currently paying $11/month for a VPS with more than enough power for this. If that's too much then you're probably SOL unless you can convince somebody else with a server to spare you a database.
You should have to beat a high score to make the list, not just match it.
IMO that means you don't have a fine enough granularity for the score. It's still possible to match a score, but highly unlikely.
[EDIT]
I suppose a devastatingly simple game like Pong would be exempt.
By a dedicated server I mean that... a dedicated server... 
a VPS is some kind of dedicated server too, but I don't have neither of two.
The thing is I don't want to pay 16 E/month for a dedicated server just to show an online score of my game (inside the game
) which is going to be sold (hopefully) for just 1.99 E... so... I was looking the way to do this trow FTP or something like that... For that reason I was saying that the game should download all the file inside a determined path, compare if the current score (client) is better than those downloaded and if it's upload the file and overwrite the old one...
The file just contain a name and a score... and are saved like this: 1.dat , 2.dat , 3.dat etc...
I know that 1.dat contains the better score so if the client beat that score, the game sends a new 1.dat file overwriting the old one. This should be done constantly... But I know there must be a hide problem I haven't think much of it.
You should have to beat a high score to make the list, not just match it.
Yes I think I'm going to do that, it's the less controversial option.
You could use a VPS instead, or your own desktop if it is accessible from the Internet
I can't because I have a dynamic IP, and I can't use No-IP or something similar.
I'm beginning to think that you're currently using some Web space provided for cheap with only FTP access and no direct access to the server.
Oh Yhea
, I could use MySQL but that doesn't change anything.
If that's too much then you're probably SOL unless you can convince somebody else with a server to spare you a database.
I'm currently paying $11/month for a VPS with more than enough power for this.
If that's too much then you're probably SOL unless you can convince somebody else with a server to spare you a database.
I'm currently paying $11/month for a VPS with more than enough power for this.
If that's too much then you're probably SOL unless you can convince somebody else with a server to spare you a database.
I'm currently paying $11/month for a VPS with more than enough power for this.
Hm... I think I might have one ahahahahahaha....
Edit: I have a better Idea, to relieve the task of the game, I can just send the file (from the game) each time the player reach a new better score, this way I delete the possibility of hackings. Then from my computer I run another program which load the files and order them, and in the game show the first 12 but in the web page I could show all the people that have played my game! COOL!.
Oh Yhea
, I could use MySQL but that doesn't change anything.
That actually changes everything... If you have access to an Internet-accessible MySQL database (or server with MySQL and a Web-accessible Web server) then just use the database.
It will work so much more reliably than any kind of FTP solution.
I have a better Idea, to relieve the task of the game, I can just send the file (from the game) each time the player reach a new better score, this way I delete the possibility of hackings. Then from my computer I run another program which load the files and order them, and in the game show the first 12 but in the web page I could show all the people that have played my game! COOL!.
I'd still opt for the RDBMS solution... That sounds like it will be very ugly and hard to manage. Imagine how unhappy your users will be if their awesome high scores aren't recorded properly. Especially if you make them pay for the game.
That actually changes everything... If you have access to an Internet-accessible MySQL database (or server with MySQL and a Web-accessible Web server) then just use the database. It will work so much more reliably than any kind of FTP solution.
Hmm.. I didn't know that, thanks again man! I'm going to study that.
If you can't access the MySQL database from the Internet (i.e., it may only listen on the loopback interface) then you can always write a Web application interface using PHP, for example. Then just make simple HTTP requests to the server and have your PHP code process them. That, of course, depends on your server supporting PHP (or another server-side technology), but if you have MySQL then you almost certainly have PHP too.
I'll check out that, there is a way to know if their accept connection from internet besides talking directly with them?, there isn't another term besides loopback, you know they use to use more commercial words... Because at the moment I have no MySQL but if I want I can activate it(paying a little bit more)...
They have this:
Aruba.it has also activated the offer for the MySql support: technically it is an DBMS (Database Management System) or rather, a management system for Database, practically it is a fast Database with high performance, that allows you to create dynamic websites with backend MySql.
The MySQL service offers a maximum of 5 databases and an overall disk space of 100Mb.
The service can be used only by domians hosted by our hosting services or Dedicated Servers. You can increase the disk space with (100Mb) additional packages of 100Mb each.
The predefinited interaction language is PHP, but it is possible, thanks to the MyODBC drivers, to interact with it also by ASP pages, too.
What do you think?
If you already have php there, you don't need a DBMS for such a simple task. Just store the scores in a file and use PHP to lock it when you want to update it.
Pseudocode:
[edit] Disclaimer: I omited error checking for simplicity
You can also use a SQLite or BerklyDB (bdb) database instead of mysql. Both are file backed, so you don't need a DB server.
Seems great, but my hosting provider doesn't support SQLite, only MySql and Microsoft SQL server.
I have just a simple hosing service. For that reason I was talking about FTP, I'm going to study the PHP and HTTP possibility though, this is just to be able to offer an online table in-game at the first moment, but if the game success I might rent a VPS and use ENet and I would add another online options. Thanks.
Seems great, but my hosting provider doesn't support SQLite
Its a php module, you could probably upload it yourself and just use it. Same with BDB. Both are based on (more or less) simple files.
Its a php module, you could probably upload it yourself and just use it. Same with BDB. Both are based on (more or less) simple files.
I doubt he could install the module if it isn't already installed. If he could then he could do a lot more than he's letting on.
Of course, there is a chance that the sqlite module is installed and just not mentioned by his host. There's also a small chance that he could convince his host to install the sqlite module.
insert_score_submited_by_player($score_array, $POST[$score]); // I don't actually remember how to acess post variables :P
It's $_POST or $_REQUEST if you don't care whether it's GET or POST.
Seems great, but my hosting provider doesn't support SQLite
Are you sure? It comes bundled with PHP5, and you'd have to explicitly disable for it not to work.
Yes I spoke directly with them, and they have no support for SQLite, and it seems it's going to be that way for a loooong time...
Well, actually I haven't but I saw a post in their web page but now seeing again they seems to have suport for SQLite... The problem was they took a lot of time to pass from 4 to 5 and the internet is plagued by people complaining... They have support for:
libxml xsl xmlwriter dom xmlreader xml wddx tokenizer session pcre SimpleXML SPL PDO SQLite standard Reflection posix pdo_sqlite pdo_mysql mysqli <--- WTF mysql mime_magic mhash mcrypt json imap iconv hash gettext gd ftp filter exif date curl ctype calendar bz2 bcmath zlib cgi pdf ionCube Loader
But know they're complaining why they don't support PHP-SOAP or something... I don't even know what it's and don't what to know to be honest 
But I'm going to try to do what Oscar Giner suggested me, I'm going to use something like libCurl to send a request and process it.
Send the actual score of the player, if it's better run the script to erase the old one from the list (the file) and retrieve the "socres.dat" file, and if isn't just retrieve the "scores.dat" file anyway to keep the list actualized. Retrieving the list is what I don't know how to do it, I'm new in all this but I'm reading...
A library like libCurl should keep me away from sockets, headers and things like that, right?
MySQLi is just a more enhanced version of the MySQL drivers that supports bindings and procedures and such.
I just recently started using CodeIgniter and am really liking it. They have a great community, as well.
And they have a lot of documents too, even videotutorials.
I have another idea... What if I set up a server on my machine, and constantly send my IP address to my current host server, then the game would connect to the host server read the file called something like "what_is_your_IP_now.dat" and then connect with my dedicated server at home...

This way I could start learning ENet insted of PHP and libCurl
{"name":"Screen_shot_2011-04-14_at_1.17.58_AM.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/4\/0479217ec8bb51f3b851b05a7b598c9a.png","w":443,"h":566,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/4\/0479217ec8bb51f3b851b05a7b598c9a"}
Green are all the tracks that you can undo. Yellow is the last thing you placed, and therefore, the latest in the undo queue.
You add new track by touching and dragging. Just the beginning and end of the drag are used to calculate a run of track.
I'm going to work on the network code soon, once I get a bit more UI.
Here's a cropped full res shot:
{"name":"Screen_shot_2011-04-14_at_1.23.46_AM.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/9\/c9ce513d5293b68a0134253756252aa1.png","w":636,"h":404,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/9\/c9ce513d5293b68a0134253756252aa1"}
YHEAAAAA!!! Nice man!!!
Try to make it commercial, you know good graphics, sound, etc... That game has potential and being online even more.
You have the iPhone SDK? I'm starting to think that I'm the only one that don't have it...
Wow, this thread took off like a rocket. Fun to see that I'm not all alone.
yhea... for each question you have I think there are at least 1.000 K people wondering the same...
I have checked again and no I have not SQLite, would be dangerous if I show people the result from phpinfo()?.
Not really, no.
I have checked again and no I have not SQLite
That list you posted has pdo_sqlite in it. Its there. Note, it is not a SERVER based SQL system. No server required, just a simple bit of PHP code (or C/C++, or Perl, or Python) to access it.
I don't know, that list was from another guy too... My PHP version is 5.2.12 check out this... http://62.149.131.186/ver.php
But there is no problem, I'm not going to use SQLite nor some database language, I don't need those features for such simple task. I could use the CGI too I think they allow be to upload binaries since I have a "cgi-bin" folder, but I'm going to use PHP.
Let's see. I still need to read a lot, there is a lot of people that say that PHP wins against the CGI but since I already know C++ I would prefer to use it, but I don't know...
Basically I don't know what I'm talking about
... So... I also found a guy asking the same question at gamedev.com and they recommended to use PHP:
I'm looking to setup a PHP script that will take a highscore along with some type of key to validate it. The application will then call the script and upload the highscores.
Or if there is any other solution available, that would be great. Maybe even running remote MySQL queries directly from the application....
No that would be less than ideal.
This is because the user would be able to detect the MySQL login / password and put in their own data, which would be bad.
Using a PHP (or even a CGI program written in C++, maybe, so it can reuse code from the app itself) script to update high scores, list servers etc, is definitely the way to go.
It's much easier to code this sort of high-score updater or server-list manager in PHP than it would be in C++ (well, if you know PHP anyway).