home PYTHONJAVA
 

Sockets, also known as "sockets", typically send requests to the network or "receive network requests" through "sockets" to allow communication between hosts or between processes on a computer.

In this chapter we will receive the Socket service in Perl for everyone.


Create a server

  • Use the socket function to create a socket service.

  • Bind the port using the bind function.

  • Use the listen function to listen on the port.

  • Use the accept function to receive client requests.

Create a client

  • Use the socket function to create a socket service.

  • Connect to the socket server using the connect function.

The following diagram demonstrates the communication process between the client and the server:



server socket function

socket function

In

Perl, we use the socket() function to create a socket with the following syntax:

socket( SOCKET, DOMAIN, TYPE , PROTOCOL );

Parameter resolution:

  • DOMAIN creates a socket-specific protocol set. For example:

    • AF_INET represents an IPv4 network protocol
    • AF_INET6 for IPv6
    • AF_UNIX represents a local socket (using a single file)

  • TYPE Socket types can be classified as SOCK_STREAM or SOCK_DGRAM depending on whether they are connection-oriented or non-connected

  • PROTOCOL should be (getprotobyname('tcp'))[2]. Specifies the transport protocol that is actually used.

So the socket function is called as follows:

use Socket # defines PF_INET and SOCK_STREAM

Socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[< /span>2]);

bind() function

Use bind() to assign an address to the socket:

bind( SOCKET, ADDRESS );

SOCKET A descriptor for a socket. ADDRESS is a socket address (TCP/IP) that contains three elements:

  • Address cluster (TCP/IP, AF_INET, may be 2 on your system)

  • Port number (for example, 21)

  • Network address (eg 10.12.12.168)

After using socket() to create a socket, only the protocol it uses is assigned, and no address is assigned. Before accepting connections from other hosts, you must first call bind() to assign an address to the socket.

Simple examples are as follows:

use Socket # defines PF_INET and SOCK_STREAM

$port = 12345; # listening port
$server_ip_address = "10.12.12.168";
Bind( SOCKET, pack_sockaddr_in($port, inet_aton($server_ip_address)))
   or die "Unable to bind port! \n";

or die Execute after the failed binding address.

Set the port to be immediately reusable by setting the setsockopt() option SO_REUSEADDR.

The pack_sockaddr_in() function converts addresses to binary format.

listen() function

When a socket is bound to an address, the listen() function will start listening for possible connection requests. However, this can only be used when there is a reliable data flow guarantee:

listen( SOCKET, QUEUESIZE );

SOCKET : A descriptor for a socket.

QUEUESIZE : is an integer that determines the size of the listening queue. When a connection request comes in, it will enter the listening queue; when a connection request is accepted by accept(), it will be removed from the listening queue; when the queue is full, The new connection request will return an error.

Once the connection is accepted, a return of 0 indicates success and an error returns -1.

accept() function

The accept() function accepts the requested socket connection. Returns a compressed network address if successful, otherwise returns FALSE:

accept( NEW_SOCKET, SOCKET );

NEW_SOCKET : A descriptor for a socket.

SOCKET : A descriptor for a socket.

accept() is usually used in an infinite loop:

while(1) {
   Accept( NEW_SOCKET, SOCKT );
   .......
}

The above example can listen to the client's request in real time.


Client function

connect() function

The

connect() system call sets up a connection for a socket with parameters for file descriptors and host addresses.

connect( SOCKET, ADDRESS );

Create an instance of a connection to the server socket:

$port = 21; # ftp Port
$server_ip_address = "10.12.12.168";
Connect( SOCKET, pack_sockaddr_in($port, inet_aton($server_ip_address)))
    or die "Unable to bind port! \n";

Complete instance

Next we will use a complete example to understand the application of all socket functions:

Server server.pl code:

Instance

#!/usr/bin/perl -w # Filename : server.pl use strict; use Socket; # Use port 7890 as the default my $port = shift || 7890; my $proto = getprotobyname('tcp') ; my $server = "localhost"; # Set local address # Create sockets, ports can be reused, create multiple connections socket(SOCKET, PF_INET, SOCK_STREAM, $proto ) or die "Unable to open socket $!\n"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "Unable to set SO_REUSEADDR $!\n"; # Bind the port and listen bind( SOCKET, pack_sockaddr_in($port, inet_aton ($server) )) or die "Unable to bind port$port! \n"; listen(SOCKET, 5) or die "listen: $!"; print "Access Launch:$port\n"; # Receive Request my $client_addr; while ($client_addr = accept(NEW_SOCKET, SOCKET )) { # send them a message, close connection my $name = gethostbyaddr($client_addr, AF_INET ); print NEW_SOCKET "I am from the server"; print "Connection recieved from $name\n"; close NEW_SOCKET; }

Open a terminal and execute the following code:

$ perl sever.pl
Access Launch:7890

Client client.pl code:

Instance

#!/usr/bin/perl -w # Filename : client.pl use strict; use Socket; # Initialize Address and Port my $host = shift || 'localhost' ; my $port = shift || 7890; my $server = "localhost"; #host address
# Create a socket and connect socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname ('tcp'))[2]) or die "Unable to create socket $!\n"; connect( SOCKET, pack_sockaddr_in($port, inet_aton ($server) )) or die "Unable to connect: port $port! \n"; my $line; while ($line = <SOCKET>) { print "$line\n"; } close SOCKET or die "close: $!";

Open another terminal and execute the following code:

$ perl client.pl
I am from the server





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.