Game Development Community

Send()" using TCPObject

by game4Rest · in Torque Game Builder · 01/14/2006 (8:02 pm) · 9 replies

Our network programmer made his own Server using C++. I was able to connect to that Server by just
modifying the T2D Alpha slightly. But having some problem with sending data accross the established connection.
please see the below. It is from example/common/client/client.cs.

function connectToServer(%ip)
{
if(%ip $="")
%ip = "192.168.0.6:33000"; //just for test....

%buffer="test"; //what to send
%conn = new TCPObject(TCPCon); //TCP connection

%conn.connect(%ip); //It was successful.
%conn.send("test:"@%buffer@"\n"); //Server failed to receive anything
}

Isn't it enough or am I missing something to make it successful?

Please help me.

Thanks in advance.

Hong Jin

#1
01/15/2006 (12:09 am)
If I was not specific in my question, I'm sorry about it.

As far as I know, when TCP connection is established between Server and Client, minimum requirements for sending and receiving data is fulfilled. Moreover, when I checked out if the game engine has the valid socket number and data that I sent, it had no problem. In fact, I doubt if my script code has problem.

But our server programmer says he gets nothing. Even though I've been reading engine code regarding TCPObject, it is not easy to find out the cause of this problem. It is mainly because I'm not accustomed to
network programming.

So, please someone give me any idea.

Thanks in advance.

Hong Jin
#2
01/15/2006 (3:37 am)
After suffering a headache for 3 days, I found the answer.

For anyone who is curious for the answer;

The reason for my problem is because I tried to send data too early. As I am not a network programmer, I can't tell the exact reason. But maybe I sent the data before the server is ready to accept it. When I move the function "send()" to another file, I was able to make success to send my data across the TCPConnection.

Too many problem I come up with. I think I'm getting older making game.

Good Luck to everyone.

Hong Jin
#3
01/15/2006 (3:40 am)
Good info, I ran into a similar problem years back and never did figure it out.
Glad to see you did it.
#4
01/15/2006 (11:57 am)
You'll definitely want to wait until you get a callback on the onConnected function of your TCPCon object before sending anything. As it is, there is no buffering built into the send() command to queue up the data and send it when/if the connection is made.

You'll additionally want to catch the various callbacks for connection problems, such as onConnectFailed, onDNSFailed, etc. in order to gracefully exit out of the 'waiting to connect' state that the user would otherwise get stuck in forever.

As an added precaution, I always create a schedule wrapped around the whole thing that bails if nothing happens after 30 or so seconds, in case there's weird elongated lag but no specific failure.
#5
01/15/2006 (5:47 pm)
Dear Luke,

Thank you very much for useful information.
I'll keep that in my mind when working on network.

Hong Jin
#6
01/16/2006 (11:52 am)
Sorry I didn't catch this request earlier, but Luke's advice is excellent. Basically, the standard tactic for this type of thing is to create your socket to your external server early in the startup/boot process of your game, and then by the time you need it, it should be up and ready for you.

Of course, do make sure that you safety check, i.e. something like:
(all TorqueScript here)
$externalServerConnectionAddress = "1.2.3.4:5555";
function initExternalServerConnection()
{
$externalServerConnection = newTCPObject (ExternalServerConnection);
$externalServerConnection.isConnected = "false";
$externalServerConnection.connect($externalServerConnectionAddress);
}

TCPObject::onConnected(%this)
{
%this.isConnected = "true";
}

then you can wrap your sends anyway you like, one method would be:

if ($externalServerConnection.isConnected $= "true")
{
$externalServerConnection.send("my send string");
}

That's probably not the best solution, but it would work.
#7
01/17/2006 (1:37 am)
Dear Stephen,

Thanks a lot for your advice.

By the way, our server programmer wants me to send every packet keeping certain format. He wants the packet format to be "Packet Size(2Bytes)"+"Packet Type(2Bytes)"+"Buffer(valiable)"+"endMark(2Bytes)".

So, when I try this way,

%packetSize = 16;
%packetType = "chat";
%buffer ="chat message";
%endMark = 0X3A;
ExternalServerConnection.send(%packetSize @ %packetType @ %buffer @ %endMark);

I have difficulties to keep data sizes of "Size", "Type", "endMark" to be fixed as he wants. How can it be possible to keep the data size? Is it beyond the script capability?

Please, give me any advice.

Thank you in advance.

Hong Jin
#8
01/17/2006 (10:50 am)
He wants you to change the underlying TCP protocol information (which is fine), which will need to be done in the c++ implementation of the underlying platform specific net code.

Torque's TCPObject() (and related platform code) conforms to the standard RFC for TCP/IP sockets, but without being intimately familiar with the relavent RFC (it's been a while!), I can't say if this particular request is unusual or not. In any case, he's talking about the packet header information, not the contents of the packet--and script can only change the contents of the packet.
#9
01/17/2006 (5:08 pm)
Understand!

Stephen, thanks a lot.

Hong Jin