|
Socket Communication
Part:
1
2
3
4
(Continued from previous part...)
In the telnet window, type in the following text:
Fish, I love you and respect you very much.
But I will kill you dead before this day ends.
.
The text will be reversed and returned back from the ReverseEchoer.pl:
Welcome to Reverse Echo Server.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
.sdne yad siht erofeb daed uoy llik lliw I tuB
.
Note that the telnet program is not displaying text you typed in.
It only displays the text received from ReverseEchoer.pl program.
SocketClient.pl - A Simple Socket Client Application
Instead of using the telnet program, we can use the following program, SocketClient.pl,
to communicate with ReverseEchoer.pl:
#- SocketClient.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
$domain = 2; # Internet domain
$type = 1; # Sequenced, reliable, two-way connection, byte streams
$proto = 6; # Transmission Control Protocol (TCP)
socket(SOCK,$domain,$type,$proto);
$host = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
$port = 1024;
$address = pack('S n a4 x8', $domain, $port, $host);
bind(SOCK, $address);
print STDOUT "Client host: ",join('.',unpack('C4', $host)),"\n";
print STDOUT "Client port: $port\n";
$sHost = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
$sPort = 8888;
$sAddress = pack('S n a4 x8', $domain, $sPort, $sHost);
connect(SOCK, $sAddress);
print STDOUT "Server host: ",join('.',unpack('C4', $sHost)),"\n";
print STDOUT "Server port: $sPort\n";
select(SOCK); $| = 1; select(STDOUT);
while ($m=<SOCK>) {
print STDOUT $m;
$m = <STDIN>;
print SOCK $m;
}
close(SOCK);
exit;
Note that:
- Setting $| to 1 is to turn on auto flush to the default file handle. In order to
turn on auto flush to SOCK, I have to use select() function to switch default file
handles.
Now, run ReverseEchoer.pl, then run SocketClient.pl in a separate command
window. If you type in the same text as in the previous test, you will get
the following output:
Client host: 127.0.0.1
Client port: 1024
Server host: 127.0.0.1
Server port: 8888
Welcome to Reverse Echo Server.
Fish, I love you and respect you very much.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
But I will kill you dead before this day ends.
.sdne yad siht erofeb daed uoy llik lliw I tuB
.
Network Utility Functions
getprotobyname($protoName) - Returns the protocol number of the specified protocol name
in scalar context, and ($protoName,$aliases,$protoNumber) in list context.
getservbyname($serviceName,$protoName) - Returns the port number of the specified
network service name and protocol name in scalar context, and
($serviceName,$aliases,$portNumber,$protoName) in list context.
getservbyport($portNumber,$protoName) - Returns the network service name of the specified
port number and protocol name in scalar context, and
($serviceName,$aliases,$portNumber,$protoName) in list context.
gethostbyname($hostName) - Returns the packed IP address of the specified
host name in scalar context, and ($name,$aliases,$addrtype,$length,@addrs) in list
context. The host name can also be specified in IP address format.
gethostbyaddr($packedHostAddress) - Returns the host name of the specified
packed IP address in scalar context, and ($name,$aliases,$addrtype,$length,@addrs)
in list context.
(Continued on next part...)
Part:
1
2
3
4
|