Socket 的 Script 範例
此 Script 範例示範可能如何撰寫 Socket 用戶端。
Perl 範例
下列 Perl Script 範例會連接至 Socket,並傳送資料。 此範例是針對在 UNIX 上執行的代理程式撰寫的,其產品型號為
k00,而屬性群組稱為 SocketData。 #!/usr/bin/perl -w
# SocketTest.pl
# A simple Agent Builder Socket client using IO:Socket
#------------------------
use strict;
use IO::Socket;
# Initialize socket connection to the agent
#-----------------------
my $host = '127.0.0.1';
my $port = 0;
# This sample is for an agent with the k00 product code. The product code is
# used in the following line to find the file containing the port number to use.
open PORTFILE, "/tmp/k00_cps.properties" || die "Port file not found $!\n";
while (<PORTFILE>) {
if (/^CP_PORT=([0-9]+)/) {
$port = $1;
}
}
if ($port == 0) {
die "Could not find port to use to connect to agent.\n";
}
my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port,
Proto => 'tcp'); $sock or die "no socket :$!";
# The following call sends 2 rows of data to the agent. Each row contains 1
# String attribute and 3 numeric attributes.
syswrite $sock, "<socketData><attrGroup name=\"SocketData\"><in><a v=\"A message
from perl\"/> \<a v=\"1\"/><a v=\"2\"/><a v=\"123\"/></in><in><a v=\"More from
perl\"/><a v=\"456\"/> \<a v=\"123\"/><a v=\"789\"/></in></attrGroup>
</socketData>\n";
close $sock;