Minimal networking example - Hello Network
by Tom Bentz · in Torque Game Builder · 12/06/2006 (9:56 pm) · 29 replies
After going through the TGB boot camp I came away with the basics of networking knowledge among other things. The checkers tutorial was over my head so I went about creating the most basic example you will find. A 'Hello Network' example. It is less than 50 lines of script (without comments). I am happy to be giving back to the community!
REQUIREMENTS!!!!!!
-To test this on only one computer you will need a debug version of TGB
-I had problems with my firewall so I needed to disable it. You might also.
How to use this example
Copy the hellonetwork folder to your TGB/games directory then follow the steps below.
1. Launch two versions of TGB (one must be a debug build if using one computer)
2. In the console on your server run 'startserver();'. You should see a connected message (the server computer connects as a client to itself).
3. In the console on your client run 'connect("SERVERIP");". Where SERVERIP is the ip address of the server. YOU NEED THE QUOTES AROUND THE IP ADDRESS. You should see a connected message.
4. In the console on the server run 'serversayhi("NAME")'. Where NAME is your name or whatever.
5. Check the console on the client and you should see "NAME (server) says hi!"
6. In the console on the client run 'clientsayhi("NAME")'. Where NAME is your name or whatever.
7. Check the console on the server and you should see "NAME (client) says hi!"
The simplechat.cs script file contains all the basic stuff that should get you going creating your server and client calls.
Download hellonetwork
Tom Bentz
digitalblur entertainment
whoosier daddy?
REQUIREMENTS!!!!!!
-To test this on only one computer you will need a debug version of TGB
-I had problems with my firewall so I needed to disable it. You might also.
How to use this example
Copy the hellonetwork folder to your TGB/games directory then follow the steps below.
1. Launch two versions of TGB (one must be a debug build if using one computer)
2. In the console on your server run 'startserver();'. You should see a connected message (the server computer connects as a client to itself).
3. In the console on your client run 'connect("SERVERIP");". Where SERVERIP is the ip address of the server. YOU NEED THE QUOTES AROUND THE IP ADDRESS. You should see a connected message.
4. In the console on the server run 'serversayhi("NAME")'. Where NAME is your name or whatever.
5. Check the console on the client and you should see "NAME (server) says hi!"
6. In the console on the client run 'clientsayhi("NAME")'. Where NAME is your name or whatever.
7. Check the console on the server and you should see "NAME (client) says hi!"
The simplechat.cs script file contains all the basic stuff that should get you going creating your server and client calls.
Download hellonetwork
Tom Bentz
digitalblur entertainment
whoosier daddy?
#22
I want to keep the above post so other people can get some of the information, but the ip problem was user error. That's not even a valid IP. I should have connected to 192.168. 1. 197.
secondly.. to followup on the whole:
cheers!
off to bed with me!
-nic
07/03/2008 (12:16 am)
Never mind; I figured out what my timeout problem was (wrong ip). I want to keep the above post so other people can get some of the information, but the ip problem was user error. That's not even a valid IP. I should have connected to 192.168. 1. 197.
secondly.. to followup on the whole:
initBaseClient(); initBaseServer();requirement. Those can be added manually, or you can change your /common/commonConfig.xml to have "1" for useNetworking.
cheers!
off to bed with me!
-nic
#23
does not work for me. I had to change useNetwork to 1 in commonConfig.xml to make it work.
Does anybody know which port(s) TGB uses?
Thanks.
07/03/2008 (2:21 pm)
Using initBaseClient(); initBaseServer();
does not work for me. I had to change useNetwork to 1 in commonConfig.xml to make it work.
Does anybody know which port(s) TGB uses?
Thanks.
#24
If you are having problems getting a connection, it's not a port issue I wouldn't think. If you are in windows open up a command prompt
windows->run [type]cmd then press enter
then when you have a window up type ipconfig on your server. Look for "IP Address ...... ". Mine looked like this:
hope this helps.
07/03/2008 (11:39 pm)
Hi Christian. That snipit was merely mentioning the fix in the functions above, but yeah, it does the same thing if you just change the "useNetwork" config file as I mentioned.If you are having problems getting a connection, it's not a port issue I wouldn't think. If you are in windows open up a command prompt
windows->run [type]cmd then press enter
then when you have a window up type ipconfig on your server. Look for "IP Address ...... ". Mine looked like this:
C:\Documents and Settings\nic>ipconfig
Windows IP Configuration
Ethernet adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.1.197
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
Ethernet adapter Local Area Connection 2:
Media State . . . . . . . . . . . : Media disconnected
C:\Documents and Settings\nic>This will show you what ip you should be using in the "connect("");" function when you run it on your clienthope this helps.
#25
thanks for the reply. I do not have problems connecting locally. I was asking for the ports in order to know which ports I need to open up in the firewall. I want to test that in a real scenario.
- Server behind a firewall
- Clients connecting over the internet
Is it possible to configure TGB to just use port 80?
Christian
07/03/2008 (11:56 pm)
Hi,thanks for the reply. I do not have problems connecting locally. I was asking for the ports in order to know which ports I need to open up in the firewall. I want to test that in a real scenario.
- Server behind a firewall
- Clients connecting over the internet
Is it possible to configure TGB to just use port 80?
Christian
#26
I've modified the source code of this example as per Nic's advice, have opened up two instances of the same project, but when I run 'connect("10.1.1.2");' on the client instance, I get the following response:
That is then followed by an error box that reads: "Your connection to the server timed out."
I have double checked that I am using the correct ip address. Can anyone see what I'm doing wrong?
08/02/2008 (12:38 am)
Another newbie trying to work out basic networking...I've modified the source code of this example as per Nic's advice, have opened up two instances of the same project, but when I run 'connect("10.1.1.2");' on the client instance, I get the following response:
==>connect("10.1.1.2");
connecting to server
Loading compiled script C:/......../client/client.cs
Loading compiled script C:/......../client/message.cs
Loading compiled script C:/......../client/serverConnection.cs
Loading compiled script C:/......../client/chatClient.cs
Adding a pending connection
Sending Connect challenge Request
Common/gameScripts/client/client.cs (58): Unable to find function onDisconnectThat is then followed by an error box that reads: "Your connection to the server timed out."
I have double checked that I am using the correct ip address. Can anyone see what I'm doing wrong?
#27
-nic
08/03/2008 (10:53 pm)
Hmm, ok. It's calling onDisconnect because it's failing. The fact that you don't have that function setup isn't your primary problem. Your IP you are trying to set up looks a little funky. Are you connecting to a local address? Can you show me your ipconfig? Also, you have to have your server set up so you can connect to it. Have you started the server before trying to connect to it?-nic
#28
function onClientConnected(%client)
{
echo("=== client connected! " @ %client.name);
}
This doesn't get called on the server side when a client connects.
However, this function does get called:
function clientCmdServerMessage(%message)
{
if (getWord(%message,1) $= "ClientJoined") {
echo("client joined!");
}
echo("onServerMessage = " @ %message);
}
Unfortunately there doesn't seem to be any information about the client from this function. Does anyone know why onClientConnected() isn't being called?
12/03/2008 (4:26 pm)
When a client connects, I want a callback to be sent to the server. I think it should be:function onClientConnected(%client)
{
echo("=== client connected! " @ %client.name);
}
This doesn't get called on the server side when a client connects.
However, this function does get called:
function clientCmdServerMessage(%message)
{
if (getWord(%message,1) $= "ClientJoined") {
echo("client joined!");
}
echo("onServerMessage = " @ %message);
}
Unfortunately there doesn't seem to be any information about the client from this function. Does anyone know why onClientConnected() isn't being called?
#29
===================client side=================
//this is a command run on the client that tells what command to run on the server.
commandToServer('ExampleServerCommand',%exampleData1,%exampleData2);
===================server side================
This will be run on the server. Note that the first variable is always the client, which allows you to respond directly back to the client that called the command.
serverCmdExampleServerCommand(%client,%exampleData1,%exampleData2)
{
echo("got the following data!",%exampleData1,%exampleData2);
}
I typed that in manually so there might be a spelling mistake.. but that's the basic idea.. let me know if your still confused.
12/07/2008 (7:04 pm)
To get a command to call on a server use the following example as a reference===================client side=================
//this is a command run on the client that tells what command to run on the server.
commandToServer('ExampleServerCommand',%exampleData1,%exampleData2);
===================server side================
This will be run on the server. Note that the first variable is always the client, which allows you to respond directly back to the client that called the command.
serverCmdExampleServerCommand(%client,%exampleData1,%exampleData2)
{
echo("got the following data!",%exampleData1,%exampleData2);
}
I typed that in manually so there might be a spelling mistake.. but that's the basic idea.. let me know if your still confused.
Torque Owner Nic Biondi
Default Studio Name
function startserver() { echo("creating server"); initBaseServer(); createServer(true); }It also looks like we have to call initBaseClient before starting the client. as in://Tell this client to connect to the server function connect(%ip) { echo("connecting to server"); initBaseClient(); connecttoserver(%ip); }now my question is.. what ip to use to connect.
server said this:
so I connected with this:
and this:
I am getting a server time-out message.
==>connect("69.64.50.217"); connecting to server Adding a pending connection Sending Connect challenge Request common/gameScripts/client/client.cs (58): Unable to find function onDisconnectI know that I don't have onDisconnect in there.. but it's timing out as the root problem, not that.no luck yet. noob alert! (me)