本文共 6709 字,大约阅读时间需要 22 分钟。
The socket operation such as socket creation, binding and listening to the socket is performed by the IO::Socket::INET module.
The Perl code given below does the following:
#!/usr/bin/perl#tcpserver.pluse IO::Socket::INET;# flush after every write$| = 1;my ($socket,$client_socket);my ($peeraddress,$peerport);# creating object interface of IO::Socket::INET modules which internally does # socket creation, binding and listening at the specified port address.$socket = new IO::Socket::INET (LocalHost => '127.0.0.1',LocalPort => '5000',Proto => 'tcp',Listen => 5,Reuse => 1) or die "ERROR in Socket Creation : $!\n”;print "SERVER Waiting for client connection on port 5000";while(1){# waiting for new client connection.$client_socket = $socket->accept();# get the host and port number of newly connected client.$peer_address = $client_socket->peerhost();$peer_port = $client_socket->peerport();print “Accepted New Client Connection From : $peeraddress, $peerport\n ”;# write operation on the newly accepted client.$data = “DATA from Server”;print $client_socket “$data\n”;# we can also send the data through IO::Socket::INET module,# $client_socket->send($data);# read operation on the newly accepted client$data = <$client_socket>;# we can also read from socket through recv() in IO::Socket::INET# $client_socket->recv($data,1024);print “Received from Client : $data\n”;}$socket->close();
Also, refer to our earlier article to learn how to debug your perl code.
The Perl code given below does the following:
#!/usr/bin/perl#tcpclient.pluse IO::Socket::INET;# flush after every write$| = 1;my ($socket,$client_socket);# creating object interface of IO::Socket::INET modules which internally creates # socket, binds and connects to the TCP server running on the specific port.$socket = new IO::Socket::INET (PeerHost => '127.0.0.1',PeerPort => '5000',Proto => 'tcp',) or die "ERROR in Socket Creation : $!\n”;print “TCP Connection Success.\n”;# read the socket data sent by server.$data = <$socket>;# we can also read from socket through recv() in IO::Socket::INET# $socket->recv($data,1024);print “Received from Server : $data\n”;# write on the socket to server.$data = “DATA from Client”;print $socket “$data\n”;# we can also send the data through IO::Socket::INET module,# $socket->send($data);sleep (10);$socket->close();
Note: You can use using the perl-support.vim Plugin.
The Perl code given below does the following:
#!/usr/bin/perl#udpserver.pluse IO::Socket::INET;# flush after every write$| = 1;my ($socket,$received_data);my ($peeraddress,$peerport);# we call IO::Socket::INET->new() to create the UDP Socket and bound # to specific port number mentioned in LocalPort and there is no need to provide # LocalAddr explicitly as in TCPServer.$socket = new IO::Socket::INET (LocalPort => '5000',Proto => 'udp',) or die "ERROR in Socket Creation : $!\n”;while(1){# read operation on the socket$socket->recv($recieved_data,1024);#get the peerhost and peerport at which the recent data received.$peer_address = $socket->peerhost();$peer_port = $socket->peerport();print "\n($peer_address , $peer_port) said : $recieved_data";#send the data to the client at which the read/write operations done recently.$data = “data from server\n”;print $socket “$data”;}$socket->close();
The Perl code given below does the following:
#!/usr/bin/perl#udpclient.pluse IO::Socket::INET;# flush after every write$| = 1;my ($socket,$data);# We call IO::Socket::INET->new() to create the UDP Socket # and bind with the PeerAddr.$socket = new IO::Socket::INET (PeerAddr => '127.0.0.1:5000',Proto => 'udp') or die "ERROR in Socket Creation : $!\n”;#send operation$data = “data from client”;$socket->send($data);#read operation$data = <$socket>;print “Data received from socket : $data\n ”;sleep(10);$socket->close();
Also, refer to our earlier article to understand .
IO::Select module provides following two major functions:
To understand the usage of IO::Select, we are going to use this module in the tcpserver code.
The following code-snippet creates the object interface of IO::Socket::INET modules which internally creates a socket, binds and listens to a specified port address.
$socket = new IO::Socket::INET (LocalHost => '127.0.0.1',LocalPort => '5000',Proto => 'tcp',Listen => 5,Reuse => 1) or die "ERROR in Socket Creation : $!\n";$select = IO::Select->new($socket) or die "IO::Select $!";
The following code-snippet adds the descriptor to the list of select objects to get the descriptors ready.
@ready_clients = $select->can_read(0);foreach my $fh (@ready_clients) {print $fh "";if($fh == $socket) {my $new = $socket->accept();$select->add($new);}}
Following Perl code-snippets gets the list of descriptor that are ready to read.
@ready_clients = $select->can_read(0);foreach my $fh (@ready_clients) {if($fh != $socket) {chomp($data=<$socket>);print $data,"\n";}}
In the same way, we can do for the write operation on the socket.
When the connection is closed, you can remove the client socket descriptor for the select slit as shown below.
SIGPIPE signal gets generated when we try to send/receive data on the socket that is closed by the remote machine. So, we can assign the signal handler for SIGPIPE signal, which should remove of descriptor from the select list as shown below.
# $current_client is the global variable which has the recent file descriptor # on which the send/receive operation is tried.### Handle the PIPE$SIG{PIPE} = sub{####If we receieved SIGPIPE signal then call Disconnect this client functionprint "Received SIGPIPE , removing a client..\n";unless(defined $current_client){print "No clients to remove!\n";}else{$Select->remove($current_client);$current_client->close;}#print Dumper $Self->Select->handles;print "Total connected clients =>".(($Select->count)-1)."<\n";};
转载地址:http://dnhvi.baihongyu.com/