Perl Tutorials - Herong's Tutorial Examples - 6.00, by Dr. Herong Yang
connect() - Establishing a Socket Communication
This section describes Perl built-in functions: socket(), bind(), listen(), accept(), and connect(), that can be used for the server program and client program to establish a socket communication.
In the previous section, we only discussed about how socket works with a communication link that has been established already. Now, let's see how two application programs can establish a communication link between them.
To establish a communication link, one application program must act as a server, create a server socket with a given port number, and set the server socket in the listen mode waiting for a connection request from other program.
With one program running as a server listening for a connection request at a specific port number, the other program can now create socket with a given local port number, the Internet address of the computer system where the first program is running, and the port number where the server socket is listening. At this time, a connect request will be send over to the server socket. The server socket should then accept the connect request and instantiate a socket object to complete communication link.
Perl offers several built-in functions to support socket communication:
socket() - Creates a socket handle for the specified communication domain, type and protocol. For example:
$domain = 2; # Internet domain $type = 1; # Sequenced, reliable, two-way connection, byte streams $proto = 6; # Transmission Control Protocol (TCP) socket(SOCK,$domain,$type,$proto);
bind() - Binds a socket handle to a local address that represents a port on the local system. For example:
$domain = 2; # Internet domain $host = pack('C4', 127,0,0,1); # localhost = 127.0.0.1 $port = 8888; $address = pack('S n a4 x8', $domain, $port, $host); bind(SOCK, $address);
Note that host IP address must be packed into a 4-byte number, then put it into the address structure with the port number using the following format codes:
S An unsigned short value. 16 bits n An unsigned short in "network" (big-endian) order. 16 bits a A string with arbitrary binary data, will be null padded. x A null byte.
listen() - Sets a socket handle to listen mode with the specified queue size of incoming connection requests. This function is only used by the server application. For example:
$queueSize = 5; listen(SOCK, $queueSize);
accept() - Waits for incoming connection requests on the specified socket handle, accepts the first connection request, creates a new socket handle, one end bound to the same local address as the specified socket handle, the other end bound to the remote address received in the connection request, and returns the remove address. If no error, the new socket handle is ready for data transmission. This function is only used by the server application. For example:
$address = accept(NEWSOCK,SOCK);
connect() - Sends a communication request to a remote address that represents a port on a remote system. When the remote system accepted the request, the specified socket handle will be bound to the remote address. If no error, the specified socket handle is ready for data transmission. This function is only used by the client application. For example:
$domain = 2; # Internet domain $host = pack('C4', 216,109,118,67); # www.yahoo.com = 126.96.36.199 $port = 80; $address = pack('S n a4 x8', $domain, $port, $host); connect(SOCK, $address);
The following diagram shows the steps involved in establishing an Internet socket communication link using the built-in functions:
Client System Server System Internet Address #a Internet Address #b Step Available Port #p Available Port #q 1 socket(SOCK,2,1,6); 2 bind(SOCK,#b+#q); 3 listen(SOCK,5); 4 accept(NEWSOCK,SOCK); 5 Socket(SOCK,2,1,6); (waiting) 6 bind(SOCK,#a+#p); (waiting) 7 connect(SOCK,#b+#q); (receiving request) 8 (estalishing the link) (establishing the link) 9 (SOCK is ready) (NEWSOCK is ready)
Table of Contents