Creating A TCP Client
Hi, This sample is to showing you how to create quikely a TCP client for UT2003. - Matthieu29 -
The "TCP" class:
It will send to an external progam, some text messages. I created this code to let's know in real time the status of the game by an external application.
For infos: For my personal usage, all messages get a "line feed" caracter at the end but, for general purpose, it's not needed.
The client have to connect to a server, so we set the server's port thanks to the default property variable "RemotePort". The server's address can be set too.
Class MyTcp extends TcpLink; var int RemotePort; // Port of the server to connect var char ServerAddress; // Address of the server to connect // Seems to be string in UT2004 var string LF; // Used for linefeed "\n" //------------------------------------------------------------------------------ // First function called (init of the TCP connection) Function InitTcpLinkEventLogger() { LF = Chr(10); // Line feed char "\n" Log("InitTcpLinkEventLogger: Will start TCP connection!"); Resolve(ServerAddress); // Resolve the address of the server } //------------------------------------------------------------------------------ Function Cclose() // Closes the current connection. { Close(); } //------------------------------------------------------------------------------ Function int SendText (coerce string Str) // Send the string "Str" + "line feed" car { local int result; result = super.SendText(Str$LF); // Call the super (send the text) Log ("SentText: " $Str$" , (Number of bytes sent: "$result$")"); return result; } //--EVENTS-- // Called when the IP is resolved Event Resolved( IpAddr Addr ) { Log("OK, Address resolved"); Addr.Port = remotePort; BindPort(); // In UnrealTournament, the CLIENT have to make a bind to create a socket! (Not as a classic TCP connection!!!) ReceiveMode = RMODE_Event; // Incomming messages are triggering events LinkMode = MODE_Text; // We expect to receive texts (if we receive datas) Open(Addr); // Open the connection Log ("Connected => Port :"$Addr.port$" Address :"$Addr.Addr); } //------------------------------------------------------------------------------ // If the IP was not resolved... Event ResolveFailed() { log("### Error, resolve failed!!!"); } //------------------------------------------------------------------------------ event Opened() { log("Ok, connection opened"); } //------------------------------------------------------------------------------ // If somebody has close the connection... Event Closed() { log("Connection closed"); } //------------------------------------------------------------------------------ // Called when a string is received Event ReceivedText (string Text) { // We have just received a string ! Log("Read string: "$Text$" Size : "$Len(Text)); } //------------------------------------------------------------------------------ defaultproperties { ServerAddress="127.0.0.1" // Remote server address remotePort=3850 // Remote port number }
Ok, now how to use it:
- Create a MyTCPLink class
- Call InitTcpLinkEventLogger()
- Call SendText("A beautifull string")
- Call Cclose()
ReceivedText() is called when you receive some datas (I didn't tested this part of code, but there is no reasons it dosen't works)
// Somewhere in a class..... var MyTcp ATcp; simulated event PostBeginPlay() { Super.PostBeginPlay(); // call the super class ATcp=Spawn(class'MyTcp'); // Instanciate MyTCP class ATcp.InitTcpLinkEventLogger(); // Init the connection } [...] // Somewhere in a function in this class... ATcp.SendText ("Hello word"); // The classic !