TCP/IP Client.
When ScriptEZ.API based script file is running,
an identification area is appended
to the system menu of the console windows
as following:
<?php
// PHP 5.3.x for Windows can be downloaded here: http://windows.php.net
//////////////////////////////////////////////////////////////////////////////
// Author: SY
// Demo ScriptEZ.API based application which uses
facilities from RAD API:
// . TCP/IP usage
// . Threadpool usage
// . Echo() method usage which
displays always messages into a console window
// . ScriptEZ.API Remoting Control Interface usage by
NPTelnet.exe program
// (Connect to
ScriptEZ.API process with <processid (PID)> as connection name)
//
// Purpose: Simple TCP/IP Client receiving random
packets
//////////////////////////////////////////////////////////////////////////////
// Global variables
$ScriptEZ = null;
$hSocketClient = 0;
$Err_num = 0;
$hThreadPool = 0;
$onExit = 0;
$host_and_port = null;
$Second_counter=0;
// Events notification (callbacks) from ScriptEZ
component object
class
ScriptEZ_EventHandlers
{
// Remoting
Control Interface
function
AppEventHelp()
{
$AppEventHelp = " **** Supported
AppEvents ****" . "\n" .
"AppEventID = 3"
. "\t" . "to
exit" . "\n";
return $AppEventHelp;
}
function
onAppEvent($EventID)
{
global $onExit;
if($EventID
== 3)
{
$onExit = 3;
}
return
0;
}
////////////////////
// Socket
object notification (client side)
function
onConnectionState($hOwnerSocket, $state_desc, $error_number)
{
global
$ScriptEZ,$host_and_port;
$ScriptEZ->Echo("Connectio state: $state_desc");
if($error_number
== 0)
{
$ScriptEZ->SetConsoleTitle(" TCP/IP Client
connected($host_and_port)");
}
else
{
$ScriptEZ->SetConsoleTitle(" TCP/IP Client
discconnected");
}
return
0;
}
function onData($hOwnerSocket,
$raw_data, $data_size)
{
global $ScriptEZ;
$Data = $ScriptEZ->ConvertStringToBSTR($raw_data);
//$ScriptEZ->Echo($data_size . " " . $Data);
$ScriptEZ->Echo(" Received data size...:$data_size bytes");
$ScriptEZ->FreeBSTR($Data);
$ScriptEZ->FreeString($raw_data);
return
0;
}
////////////////////
// Task
notification for a thread pool
function onRun( $localStorage )
{
global $ScriptEZ,
$Err_num,
$Second_counter,
$onExit;
if(
$Err_num <> 0 || $onExit == 3)
{
return
0; // stop this task
}
$ScriptEZ->Sleep(100);
$Second_counter =
$Second_counter + 1;
if(
$Second_counter >= 10)
{
$ScriptEZ->PeekSocketClientObjectStatsOnReceivedData($localStorage);
$Total =
$ScriptEZ->SocketSplittedPackets + $ScriptEZ->SocketOverlappedPackets +
$ScriptEZ->SocketSimplePackets + $ScriptEZ->SocketWrongPackets;
if($Total<=0)
$Total = 1;
$splitted = round($ScriptEZ->SocketSplittedPackets/$Total*100.0,2);
$overlapped = round($ScriptEZ->SocketOverlappedPackets/$Total*100.0,2);
$simple = round($ScriptEZ->SocketSimplePackets/$Total*100.0,2);
$wrong = round($ScriptEZ->SocketWrongPackets/$Total*100.0,2);
$ScriptEZ->Echo(" ->
PooledThreadId....:$ScriptEZ->GetCurrentPooledThreadId\n" .
" Splitted packets..:$ScriptEZ->SocketSplittedPackets
($splitted%)\n" .
" Overlapped packets:$ScriptEZ->SocketOverlappedPackets
($overlapped%)\n" .
" Simple
packets....:$ScriptEZ->SocketSimplePackets ($simple%)\n"
.
" Wrong
packets.....:$ScriptEZ->SocketWrongPackets ($wrong%)\n\n");
$Second_counter = 0;
}
return
1; // continue this task
}
////////////////////
}
// Main function entry
function
{
global $ScriptEZ,
$hSocketClient,
$hThreadPool,
$onExit,
$host_and_port;
// create component object and event sink
class
$ScriptEZ = new COM("ScriptEZ.API");
$sink = new ScriptEZ_EventHandlers();
$ScriptEZ->CenterWindow(-1);
// install
event sink class
com_event_sink($ScriptEZ,
$sink, "_IAPIEvents");
// set up
Remoting Control Interface
$ScriptEZ->AppRegister();
// create
socket object from the component
$hSocketClient = $ScriptEZ->CreateSocketClientObjectEx();
$r = $ScriptEZ->EnableSocketObjectDataEncryption($hSocketClient,true,-1);
// input
dialoguing
$hStringTable = $ScriptEZ->MallocStringTable(2);
$ScriptEZ->AddStringTableItem($hStringTable,"localhost:12346");
$ScriptEZ->AddStringTableItem($hStringTable,"88.173.237.11:12346");
$host_and_port =
$ScriptEZ->InputComboBox(0,
"Enter your hostname and port, such like localhost:12346",
$hStringTable);
$ScriptEZ->FreeStringTable($hStringTable);
$array = explode(":",$host_and_port);
// connect
the server
$r = $ScriptEZ->ConnectSocketServerObject($array[0],$array[1],$hSocketClient);
// creating
thread pool with 4 threads for polling info
$hThreadPool = $ScriptEZ->CreateTaskQueueObject(4);
$r = $ScriptEZ->AddTask( $hThreadPool, $hSocketClient ) ;
// endless
loop
while(!$onExit)
{
com_message_pump(50);// dispatch COM events
}
// App
unregister before ending and release objects
$ScriptEZ->AppUnregister();
$ScriptEZ = null;
$sink = null;
}
// start now program here !
Main();
?>