|
Socket Communication
Part:
1
2
3
4
This chapter explains:
- What is a socket.
- How to establish a socket communication.
- A simple example of socket server application.
- A simple example of socket client application.
- Built-in network utility functions.
- Socket module.
What Is a Socket
Socket: An concept represents one end-point of a two-way communication
link between two programs running on the Internet network.
Internet Address: A unique number to identify each computer system on
the Internet.
Port: A number representing an entry point of on a computer system on
the Internet, where a two-way communication link can be established.
Local System: The computer system at this end of a two-way communication
link.
Remote System: The computer system at the other end of a two-way
communication link.
A two-way communication link on the Internet involves:
- Two computer systems, each has its own Internet address.
- A port number on each computer system that is used only by this
communication link.
- An application program on each computer system that uses this
communication link.
- A socket handle created in the application program that represents
this communication link.
- The application program can use the socket as a logical input or
output device to receive or send data from or to the application program
running at the other end of the communication link.
- The application program can also query the socket for information
about the communication link, like: The Internet address and port number
at this end of the communication link; The Internet address and port number
at the other end of the communication link.
The following diagram illustrates how an application program can use a socket
to talk with other application program running on another computer system
through a two-way communication link on the Internet:
Computer System I Computer System II
i/o |Address a| Internet |Address b| i/o
App. A<--->Socket|Port 1 |<-------------->| Port 2|Socket<--->App. B
Establishing 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.
(Continued on next part...)
Part:
1
2
3
4
|