收集到一篇好文,共享之。

Socket Programming in PERL

Part 1:
Socket Programming in PERL

In this article, Rahul shows us how to create a client-server socket program in Perl and then demonstrates it by pinging the server.

What is a socket? Just another bit of computer jargon? Devling a little into networking history, it is a Berkeley UNIX mechanism of creating a virtual duplex connection between processes. This was later ported on to every known OS enabling communication between systems across geographical location running on different OS software. If not for the socket, most of the network communication between systems would never ever have happened.

Taking a closer look; a typical computer system on a network receives and sends information as desired by the various applications running on it. This information is routed to the system, since a unique IP address is designated to it. On the system, this information is given to the relevant applications which listen on different ports. For example a net browser listens on port 80 for information. Also we can write applications which listen and send information on a specific port number.

For now, let's sum up that a socket is an IP address and a port, enabling connection.

Part 2:Types of Sockets

There are just two types of sockets: connection oriented and connection-less. There are other types but this classification is fair enough to get started, a socket has a domain (UNIX or internet), a connection type (connection oriented or connection less) and a protocol (TCP or UDP).

A connection oriented or a stream socket is a reliable two way communication. If you send three packets, say 1, 2 and 3, they are received in the same order they were sent. They achieve this high level of transmission quality by using TCP for error free reliable communication. The ubiquitous telnet application uses stream sockets.

The connection-less sockets or stream-less sockets use IP for routing but use the UDP. They are connectionless, since the connection need not be open as in stream sockets, the packet formed is given a destination IP address and than transmitted. This method is mostly used for packet-to-packet transfer as in ftp applications.

How a Packet is Formed

We will use an example of UDP packet for some light stuff on networking theory. It's Data Encapsulation. A packet is formed by encapsulating the data in a header at each level as it passes through the layers (protocol stack). At the receiving end the headers are stripped off as the packet travels up the layers to get the data.

Basically, at each layer, the protocol adds a header to the payload to perform the required functionality.

Client Server Architecture

It's a client-server world, today. Just about everything on the network deals with client processes talking to server processes and vice versa. Take the ubiquitous telnet, for instance. When you connect to a remote host on port 23 with telnet (the client), a program on that host (called telnetd, the server) springs to life. It handles the incoming telnet connection, sets up a login prompt, etc. Note that the client-server pair can speak in streaming or stream-less, or anything else (as long as they are speaking the same thing).

Some good examples of client-server pairs are telnet/telnetd, ftp/ftpd.


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!