Getting Started With MUD's
OK, I asked this question when I first started programming, at age 11... I can't stand reading my posts from then ;)
Anyways, I have played some MUD's now and would like to get into some Networking. I'm just about to market my current Opengl 2D game, just building hype with an ARG (Was in Game Informer, to lazy to dig up the issue number, just a form of media. A game spanning multiple medias basically) I'm working on a run 'n gun right now. Just sharing my experience. I would like to get into some networking, as that seems to be the industry's future. So I figure I'll start with a MUD.
I've done a simple little program with a tutorial that connects to a website and dumps you the header. I didn't really understand it that well... I would like to try Beeje's, but I am on windows, so I would be using winsock 2. I don't really want to use cygwin and use linux stuff instead of winsock. What would you suggest? Maybe point me on a path, an article to read, maybe some stuff to try and do on my own, ideas for practice programs to try out things I learn, etc.
So, any tips? Thanks in advance.
The FAQ has some links to Winsock tutorials :)
This one, for instance.
MUDs are fairly easy networking-wise, in that they pretty much just push ASCII strings back and forth (you can use any telnet client to play a MUD, for example). In other words, the clients are just dumb terminals and everything happens serverside.
After you've got the basic listening stuff set up (see the tutorial, and for gods sake don't create a new thread per client for this kind of server :P ), you just need to read the input from each one until you hit a newline (which will separate each command sent from the telnet client) and parse it however you want.
EDIT: Should also mention that, while it may be a bit much to parse, there's a lot of open source MUD servers you can download and look at. Good place to start reading about that in general: http://en.wikipedia.org/wiki/LPMud
[Edited by - Vinterstum on March 4, 2008 12:27:25 PM]
This one, for instance.
MUDs are fairly easy networking-wise, in that they pretty much just push ASCII strings back and forth (you can use any telnet client to play a MUD, for example). In other words, the clients are just dumb terminals and everything happens serverside.
After you've got the basic listening stuff set up (see the tutorial, and for gods sake don't create a new thread per client for this kind of server :P ), you just need to read the input from each one until you hit a newline (which will separate each command sent from the telnet client) and parse it however you want.
EDIT: Should also mention that, while it may be a bit much to parse, there's a lot of open source MUD servers you can download and look at. Good place to start reading about that in general: http://en.wikipedia.org/wiki/LPMud
[Edited by - Vinterstum on March 4, 2008 12:27:25 PM]
thanks vintersum. I am going to read that winsock article you linked to. I've skimmed that before but never really read it. I'll let everyone know how this all works out, when it happens. ;) (Us programmers and procrastinating, heh)
I don't want to make another thread, as this is probably something trivial. I read the winsock 2 for games thing thoroughly, and even understood everything :D
Now I went and I took all the functions and such and put them into a main function, and i'm getting a lot of errors. I even get the same errors when I run the source code that is in the zip. I am using Dev C++.
Here's the code:
now here's the compile log:
Normally I get all these errors when I forget to close up a loop with } but I don't see any open loops anywhere.
Now I went and I took all the functions and such and put them into a main function, and i'm getting a lot of errors. I even get the same errors when I run the source code that is in the zip. I am using Dev C++.
Here's the code:
#include <iostream>#include <string>#include <sstream>#define MAX_CLIENTS 5; // just used for clearness#define WIN32_MEAN_AND_LEAN#include <winsock2.h>#include <windows.h>using namespace std;int number_of_clients = 0;int main (){// Must be done at the beginning of every WinSock programWSADATA w; // used to store information about WinSock versionint error = WSAStartup (0x0202, &w); // Fill in wif (error){ // there was an error return 1;}if (w.wVersion != 0x0202){ // wrong WinSock version! WSACleanup (); // unload ws2_32.dll return 1;}SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket// Note that you should only bind server sockets, not client sockets// SOCKET s is a valid socket// WSAStartup has been calledsockaddr_in addr; // the address structure for a TCP socketaddr.sin_family = AF_INET; // Address family Internetaddr.sin_port = htons (5001); // Assign port 5001 to this socketaddr.sin_addr.s_addr = htonl (INADDR_ANY); // No destinationif (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR){ // error WSACleanup (); // unload WinSock return 1; // quit}// WSAStartup () has been called// SOCKET s is valid// s has been bound to a port using sockaddr_in sockif (listen(s,5)==SOCKET_ERROR){ // error! unable to listen WSACleanup (); return false;}// listening…// WSAStartup () has been called// SOCKET s is valid// s has been bound to a port using sockaddr_in sock// s is listeningSOCKET client[5]; // socket handles to clientssockaddr client_sock[MAX_CLIENTS]; // info on client socketswhile (number_of_clients < MAX_CLIENTS) // let MAX_CLIENTS connect{ client[number_of_clients] = // accept a connection accept (s, client_sock[number_of_clients], &addr_size); if (client[number_of_clients] == INVALID_SOCKET) { // error accepting connection WSACleanup (); return false; } else { // client connected successfully // start a thread that will communicate with client startThread (client[number_of_clients]); number_of_clients++; }}// SOCKET s is initializedchar buffer[11]; // buffer that is 11 characters bigsprintf (buffer, "Whatever…");send (s, buffer, sizeof(buffer), 0);shutdown (s, SD_SEND); // s cannot send anymore// you should check to see if any last data has arrived hereclosesocket (s); // closereturn 0;}
now here's the compile log:
Compiler: Default compilerBuilding Makefile: "C:\Dev-Cpp\MUD project\Makefile.win"Executing make...make.exe -f "C:\Dev-Cpp\MUD project\Makefile.win" allg++.exe -D__DEBUG__ -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include" -I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2" -I"include" -g3main.cpp: In function `int main()':main.cpp:67: error: expected primary-expression before "client_sock"main.cpp:67: error: expected `;' before "client_sock"main.cpp:67: error: expected primary-expression before ']' tokenmain.cpp:67: error: expected `;' before ']' tokenmain.cpp:69: error: expected `)' before ';' tokenmain.cpp:69: error: expected primary-expression before ')' tokenmain.cpp:69: error: expected `;' before ')' tokenmake.exe: *** [main.o] Error 1Execution terminated
Normally I get all these errors when I forget to close up a loop with } but I don't see any open loops anywhere.
Look at the definition of MAX_CLIENTS. You likely don't want a semicolon there.
I'm trying to move this follow-up question into General Programming as it just has to do with getting code compiling, not with networking. We'll see if I have the forum-fu to manage...
I'm trying to move this follow-up question into General Programming as it just has to do with getting code compiling, not with networking. We'll see if I have the forum-fu to manage...
Compiler: Default compilerBuilding Makefile: "C:\Dev-Cpp\MUD project\Makefile.win"Executing make...make.exe -f "C:\Dev-Cpp\MUD project\Makefile.win" allg++.exe -D__DEBUG__ -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include" -I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2" -I"include" -g3main.cpp: In function `int main()':main.cpp:72: error: `addr_size' undeclared (first use this function)main.cpp:72: error: (Each undeclared identifier is reported only once for each function it appears in.)main.cpp:81: error: `startThread' undeclared (first use this function)make.exe: *** [main.o] Error 1Execution terminated
#include <iostream>#include <string>#include <sstream>#define MAX_CLIENTS 5 // just used for clearness#define WIN32_MEAN_AND_LEAN#include <winsock2.h>#include <windows.h>using namespace std;int number_of_clients = 0;int main (){// Must be done at the beginning of every WinSock programWSADATA w; // used to store information about WinSock versionint error = WSAStartup (0x0202, &w); // Fill in wif (error){ // there was an error return 1;}if (w.wVersion != 0x0202){ // wrong WinSock version! WSACleanup (); // unload ws2_32.dll return 1;}SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket// Note that you should only bind server sockets, not client sockets// SOCKET s is a valid socket// WSAStartup has been calledsockaddr_in addr; // the address structure for a TCP socketaddr.sin_family = AF_INET; // Address family Internetaddr.sin_port = htons (5001); // Assign port 5001 to this socketaddr.sin_addr.s_addr = htonl (INADDR_ANY); // No destinationif (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR){ // error WSACleanup (); // unload WinSock return 1; // quit}// WSAStartup () has been called// SOCKET s is valid// s has been bound to a port using sockaddr_in sockif (listen(s,5)==SOCKET_ERROR){ // error! unable to listen WSACleanup (); return false;}// listening…// WSAStartup () has been called// SOCKET s is valid// s has been bound to a port using sockaddr_in sock// s is listeningSOCKET client[5]; // socket handles to clientssockaddr client_sock[MAX_CLIENTS]; // info on client socketswhile (number_of_clients < MAX_CLIENTS) // let MAX_CLIENTS connect{ client[number_of_clients] = // accept a connection accept (s, client_sock[number_of_clients], &addr_size); if (client[number_of_clients] == INVALID_SOCKET) { // error accepting connection WSACleanup (); return false; } else { // client connected successfully // start a thread that will communicate with client startThread (client[number_of_clients]); number_of_clients++; }}// SOCKET s is initializedchar buffer[11]; // buffer that is 11 characters bigsprintf (buffer, "Whatever…");send (s, buffer, sizeof(buffer), 0);shutdown (s, SD_SEND); // s cannot send anymore// you should check to see if any last data has arrived hereclosesocket (s); // closereturn 0;}
STILL no joy. Any ideas? thanks.
EDIT: Did some searching and got the addr_size working. I can't get the startThread error though. Any ideas on that? Thanks for all the help
Note that you haven't declared addr_size anywhere before you start to use it.
I suggest you start learning C/C++ programming before you start trying to create networked code. It's clear that your lack of C++ experience is getting in the way of the networking part.
I suggest you start learning C/C++ programming before you start trying to create networked code. It's clear that your lack of C++ experience is getting in the way of the networking part.
On a scale 1-5 as far as C/C++ skills go for me, I'd rate myself a 3.5. I never got into real advanced stuff like pointers or polymorphism, and such. I'd work more with pointers but I don't understand what they do. Anyways, I realized I forgot to make the int for the addr_size, I am on the thread thing. I'm not sure what this startThread function is, so I can't do any defines or ints, since it's a function... So, does anyone have any experience with this startThread?
Quote:Original post by brandonman
On a scale 1-5 as far as C/C++ skills go for me, I'd rate myself a 3.5. I never got into real advanced stuff like pointers or polymorphism, and such. I'd work more with pointers but I don't understand what they do. Anyways, I realized I forgot to make the int for the addr_size, I am on the thread thing. I'm not sure what this startThread function is, so I can't do any defines or ints, since it's a function... So, does anyone have any experience with this startThread?
Ah, this isn't a problem with your C/C++ skills my son (which are obviously not to blame -- one cannot be expected to understand such high-level concepts like pointers), but rather, with your copy-pasting skills. When you copied that code from the original source, you neglected to copy it completely! You forgot to copy the startThread function from that source.
I'd recommend using Ctrl-A next time to make sure you get everything.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement