# Server or no server

## Recommended Posts

Asimov500    122
Hi all, I successfully set up a server for my game. Now the idea is that the user can make the game the server or client, or just play on the local machine. Now I got the server bit to work but my code for disabling it doesn't. The following is my code for the server. I am only showing relavent parts. SocketServer in(3000,5); Socket* s=in.Accept(); unsigned ret; This waits for a connection and then carries on Then this line sends my data to the client. string sendstring="Red Move y="+y+" x="+x; s->SendLine(sendstring); ok this works ok until I want to disable ie for if I need a local game or if I am the client. The following is what I tried. if (bserver==true) { SocketServer in(3000,5); Socket* s=in.Accept(); unsigned ret; } and for the send data if (bserver==true) { string sendstring="Red Move y="+y+" x="+x; s->SendLine(sendstring); } The problem is that this throws up an error in s->SendLine(sendstring); because it hasn't been defined. I thought that if the server was set to false then that line shouldn't be run. Anyway I was wondering if there was a way to get this to work. Asimov

##### Share on other sites
Sneftel    1788
Variables only last as long as the scope they're defined in. s goes away after the body of the first if-statement.

##### Share on other sites
MichaBen    481
Code between if statements is still compiled, so even if the code won't run, it's still compiled and needs to be valid. If use #ifdef rather then if, it will probably compile, but that means you have to build separated executables for server and no server version. If you want the same executable to be able to play with and without server, you need to move the definition of variable s outside the body of the if statement.

##### Share on other sites
Asimov500    122
Hi,

I understand what you mean, but I still have a problem.

The line that starts the listen mode is this

Socket* s=in.Accept();

This also sets the variable, but if I move it outside the if statement
it will wait for a client to log in to the server even if you were not intending to play a network game.

I had thought about a server and client version of the game, but most games these days let you choose whether you are server or client inside the game.

eg Battleship chess (Which is a great game I might add)

Asimov

##### Share on other sites
Trihex    242
Socket* s;if (bserver==true){   SocketServer in(3000,5);   s=in.Accept();   unsigned ret;}and for the send dataif (bserver==true){   string sendstring="Red Move y="+y+" x="+x;   s->SendLine(sendstring);}

Define s outside of the if...

##### Share on other sites
Asimov500    122
Hi Trihex,

After sending the last mail I thought I would be more specific.

When I add the line Socket* s;
out of the if statement and server is set to true, it waits for a client to connect and then my game shuts down.

I have no idea why.
However when server is set to false the game runs as normal.

Asimov

##### Share on other sites
Asimov500    122
Hi Trihex,

I take it all back your solution worked.
It was I who got it wrong. I put
socket* s;

outside of the if statement but what I forgot to change was this,
Socket* s=in.Accept();

After changing this to
s=in.Accept();

everything worked.
Thankyou very much.

My gameserver now works.
Next step is to make the client work.

Then to write the menu to give you the choice at the start of the game.

Anyway thankyou to all that replied.

Asimov

##### Share on other sites
fireVein    122
Quote:
 string sendstring="Red Move y="+y+" x="+x;SendLine(sendstring);

Another thing you may want to keep into consideration is bandwidth usage. I.e. use an ID number for commands and a table lookup to find what function to call.

Ex:
struct cmdDef {	std::string cmdName;	void (*cmdFunction)(Player *player, std::string args);};//command send to clientstruct clientCmdDef {	std::string cmdName;	std::string clientFunc;};//commands received from clientsconst struct cmdDef socketCmd [] = {	{"0", testCmd},	{"1", socketCmdRegister},	{"2", socketCmdLogin},	{"", 0}};//commands sent to clientconst struct clientCmdDef clientCmd [] ={        {"test", "0"},	{"MessageBox", "1"},	{"RegisterComplete", "2"},	{"loadLobby", "3"},	{"updatePlayerList", "4"},	{"updateFriendList", "5"},	{"UpdateRoomList",   "6"},	{"lobbyDoChat",		"7"},	{"JoinRoom",  "8"},	{"UpdateRoomInfoPlayerList", "9"},	{"RoomInfoDoChat", "10"},	{"LaunchGame", "11"},	{"GameUpdatePlayerList", "12"},	{"", ""}};