The examples below use Perl which has a simple Sockets API. Perl is a typeless interpreted language useful for creating complex command scripts. It uses a syntax similar to C language. Perl is already installed on System2, and you can download a freeware copy for your (e.g. MS Windows) machine from www.cpan.org.
use IO::Socket;
$server= IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "system2.newhaven.edu:80",
)
or die "cannot connect to port 80 at system2";
To create a server socket (passive open): use IO::Socket;
$server= IO::Socket::INET->new(
Proto => "tcp",
LocalPort => 9123,
Listen => 5 # Max queue of waiting clients
)
or die "can't serve";
After creating a socket and placing a reference to it in variable $server, the server program
can accept a client
connection, using the $server->accept() method.
This method awaits a client connection (at port 9123, in this
example) to the socket referenced by $server,
assigns a new vacant port number for the connection, then creates
and returns a reference to a new socket for this connection.
The original port (9123) remains available for further connections
from other clients.
After successfully establishing a connection, a line may be read from the socket object referenced (for example, by $server above, or $client below) using a Perl assignment statement with the socket object reference enclosed in < ... >:
$line= <$server>;The contents of a variable may also be sent to standard output using:
print "Received [", $line, "] from server."or, sent sent through a socket (e.g. $server) using:
print $server, "Received [", $line, "] from server."
use IO::Socket;
$server= IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "system2.newhaven.edu:80",
)
or die "cannot connect to port 80 at system2";
print $server "GET /index.html\n";
$server->flush();
while ($line= <$server>) { print $line; }
close $server; |
The command:
perl client.plprints the same output as does the command:
telnet system2.newhaven.edu 80 GET /index.htmlThe $server->flush() method is required to force the network layer to send the request to the server, before the client attempts to read the server's response in the while loop.
use IO::Socket;
$server= IO::Socket::INET->new(
Proto => 'tcp',
LocalPort => 9123,
Listen => 5 # Max queue of waiting clients
)
or die "can't serve";
while ($client= $server->accept())
{
$line= <$client>;
print $client "Received [", $line, "] from you.\n";
close $client;
}
close $server; |
the command:
perl server.plstarts a server listening at port 9123 which does not return to the command prompt. (It can be terminated by control-Z on MS Windows, and control-C on Unix.) While it is running on System2, for example, a web browser can access it at the Uniform Resource Locator (URL) system2.newhaven.edu:9123/index.html. Since this web server isn't listening at the usual HTTP port 80, the non-standard listening port (9123, in this example) must be specified with the phrase ":9123" in the URL given to the browser.
$result= `echo hi`;returns the standard output of the command echo hi to Perl variable $result, which gets the string "hi".
$mid= substr("spots",1,3);
$mid= substr("spots",1,-1);
both put "pot" into variable $mid.
Notice that negative arguments to substr (e.g. -1)
are relative to the end of the string.