perl sample
chap 13. Internet socket
a, client_w.pl
#!/usr/bin/perl -w
use IO::Socket;
$socket = IO::Socket::INET->new
(
PeerAddr => '127.0.0.1',
PeerPort => 27777,
Proto => "tcp",
Type => SOCK_STREAM,
) or die "could not open port.\n";
print $socket "hello from the client\n";
close ($socket);
b, server_r.pl
#!/usr/bin/perl -w
use IO::Socket;
$server = IO::Socket::INET->new
(
LocalPort => 27777,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 5
) or die "could not open port.\n";
while ($client = $server->accept()) {
$line = <$client>;
print $line;
}
close ($server);
=================================
Notes: PerlInNut/13.2
a, The parameters for the new socket object determine whether it is a server or a client socket. Because we're creating a server socket, LocalAddr and LocalPort provide the address and port to bind to the socket. The Listen parameter gives the queue size for the number of client requests that can wait for an accept at any one time.
When the server receives a client request, it calls the accept method on the socket object. This creates a new socket object on which the rest of the communication can take place:
$new_sock = $sock->accept();
b, Type => SOCK_STREAM | SOCK_DGRAM
Specifies the type of socket. SOCK_STREAM indicates a stream-based socket connection, and SOCK_DGRAM indicates a message-based (datagram) connection.
Q:datagram = udp?
A:With sockets, you can do both virtual circuits (as TCP streams) and datagrams (as UDP packets)
Tuesday, February 9, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment