Legacy:Creating A TCP Client: Difference between revisions
From Unreal Wiki, The Unreal Engine Documentation Site
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 38: | Line 38: | ||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||
Function int SendText (coerce string Str) // Send the string "Str" + "line feed" | Function int SendText (coerce string Str) // Send the string "Str" + "line feed" char | ||
{ | { | ||
local int result; | local int result; | ||
Line 54: | Line 54: | ||
Log("OK, Address resolved"); | Log("OK, Address resolved"); | ||
Addr.Port = remotePort; | Addr.Port = remotePort; | ||
BindPort(); // In UnrealTournament, the CLIENT | BindPort(); // In UnrealTournament, the CLIENT has to make a bind to create a socket! (Not as a classic TCP connection!!!) | ||
ReceiveMode = RMODE_Event; // Incomming messages are triggering events | ReceiveMode = RMODE_Event; // Incomming messages are triggering events | ||
LinkMode = MODE_Text; // We expect to receive | LinkMode = MODE_Text; // We expect to receive text (if we receive data) | ||
Open(Addr); // Open the connection | Open(Addr); // Open the connection | ||
Log ("Connected => Port :"$Addr.port$" Address :"$Addr.Addr); | Log ("Connected => Port: "$Addr.port$" Address: "$Addr.Addr); | ||
} | } | ||
Line 68: | Line 68: | ||
Event ResolveFailed() | Event ResolveFailed() | ||
{ | { | ||
Log("### Error, resolve failed!!!"); | |||
} | } | ||
Line 75: | Line 75: | ||
event Opened() | event Opened() | ||
{ | { | ||
Log("Ok, connection opened"); | |||
} | } | ||
Line 83: | Line 83: | ||
Event Closed() | Event Closed() | ||
{ | { | ||
Log("Connection closed"); | |||
} | } | ||
Line 131: | Line 131: | ||
// Somewhere in a function in this class... | // Somewhere in a function in this class... | ||
ATcp.SendText ("Hello | ATcp.SendText ("Hello world"); // The classic ! | ||
</uscript> | </uscript> |
Latest revision as of 14:15, 6 August 2009
Hi, This sample is to showing you how to create quikely a Wikipedia: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" char
{
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 has 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 text (if we receive data)
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 world"); // The classic !