Socket programming in Perl

Source: Internet
Author: User
Tags perl interpreter
Network programming is a mysterious and complex art, and of course it is also very interesting. The PERL language provides a wide range of TCP/IP network functions, all of which are directly derived from the C language socket library functions.
Because the socket library functions of Perl and C languages are the same in terms of type and usage, socket programming is performed in Perl, and socket programming is performed in C language.
The following is a list of socket library functions in Perl:
Function prototype instructions
Accept (newsocket, genericsocket) accepts the request's socket connection. If the request succeeds, a compressed network address is returned; otherwise, false is returned.
Example:
If (! $ Connect = accept (new, handle ))
{
Die "Connection Failed: $!
";
}
BIND (socket, name) establishes the binding between name and socket, where name should be the compression address corresponding to the correct socket type.
If the operation succeeds, true is returned; otherwise, false is returned.
When using socket for network programming, this function is very important because it establishes a connection between the socket handle and an address on the network.
Example:
BIND (Sh, $ socketaddress );
Connect (socket, name) tries to talk to another process that has called the accept () function and is waiting to establish a connection.
If the request succeeds, true is returned. Otherwise, false. Name is returned, which indicates the compressed address of the correct socket handle type.
Example:
Connect (sock, $ address) | die "can't connect with remote host: $!
";
Gethostbyaddr (address, type) converts a compressed network address to a name and address that is easier to read and understand.
When you only know the IP address of the host, you can use this function to query the host name and other network information. It returns a list containing the following information:
($ Name, $ alias, $ addrtype, $ length, $ address)
$ Name indicates the host name corresponding to the IP address, $ alias indicates other aliases corresponding to $ name, $ addrtype indicates the network address type, and $ Length indicates the address length, $ address is a list of compressed IP addresses.
Example:
$ Packedaddress = pack ("C4", $ ipaddr );
($ Name, $ alias, $ addrtype, $ length, $ address)
= Gethostbyaddr ($ packedaddress, 2 );
The gethostbyname (name) function is similar to the preceding gethostbyaddr () function. However, the host name is used as a parameter. The returned information is in the same format.
Example:
$ Host = "stuff.com ";
($ Name, $ alias, $ addrtype, $ length, $ address)
= Gethostbyname ($ host );
@ IP = unpack ("C4", $ Address [0]);
$ Hostip = join (".", @ IP );

Original code of the program used to verify the email password
The following code is used to strictly test the POP3 servers 263.net and pop.netease.com in two operating systems, proving that they are successful.
First:
Operating System: Windows 98 Chinese Version
WWW server: Apache 1.3.9 for win
Perl interpreter: Perl for Win32, version 5.005 _ 03 built for MSWin32-x86-object of ActiveState tool Corp
Second:
Operating System: Red Hat Linux 6.1
WWW server: Apache 1.3.6 for Linux
Perl interpreter: Version 5.005 _ 03 built for i386-linux

#! /Usr/bin/perl
# Test. pl
# Author homepage: http://spot.126.com
Use strict;
Use SOCKET;

My $ pop3server = "263.net ";
My $ Port = 110;

$ | = 1;
Print "Content-Type: text/html

";
Print "POP3
";
Print"
";

My ($ A, $ name, $ aliases, $ proto, $ type, $ Len, $ thataddr, $ thisaddr, $ I );
My $ af_inet = 2;
My $ sock_stream = 1;
My $ sockaddr = "s n A4 X8 ";

($ Name, $ aliases, $ PROTO) = getprotobyname ("TCP ");
($ Name, $ aliases, $ port) = getservbyname ($ port, "TCP ")
Unless $ Port = ~ /^ D + $ /;;
($ Name, $ aliases, $ type, $ Len, $ thataddr) = gethostbyname ($ pop3server );

My $ this = pack ($ fig, $ af_inet, 12345, $ thisaddr );
My $ that = pack ($ sockaddr, $ af_inet, $ port, $ thataddr );

My $ mysocket = socket (S, $ af_inet, $ sock_stream, $ PROTO );
If ($ mysocket)
{
}
Else
{
Print "cannot open socket: $! ";
Exit (0 );
}

My $ mybind = BIND (S, $ this );
If ($ mybind)
{
}
Else
{
Print "cannot be bound! : $! ";
Exit (0 );
}

My $ myconnect = connect (S, $ that );
If ($ myconnect)
{
}
Else
{
Print "connection error: $! ";
Exit (0 );
}

My $ Buf = "";
My $ senderip = Recv (S, $ Buf, 596,0 );
If ($ senderip)
{
}
Else
{
Print "receiving error: $! ";
Exit (0 );
}

If (substr ($ Buf, 0, 3) eq "+ OK ")
{
}
Else
{
Print "POP3 server error!

";
Exit (0 );
}

My $ buffer = "User zhangsan ";
$ Buffer. = CHR (13 );
$ Buffer. = CHR (10 );

My $ senval = Send (S, $ buffer, 0 );
If ($ senval)
{
}
Else
{
Print "sending error: $! ";
Exit (0 );
}

My $ Buf = "";
My $ senderip = Recv (S, $ Buf, 4096,0 );
If ($ senderip)
{
}
Else
{
Print "receiving error: $! ";
Exit (0 );
}

If (substr ($ Buf, 0, 3) eq "+ OK ")
{
}
Else
{
Print "this account is not available!

";
Exit (0 );
}

$ Buffer = "Pass 12345678 ";
$ Buffer. = CHR (13 );
$ Buffer. = CHR (10 );

My $ senval = Send (S, $ buffer, 0 );
If ($ senval)
{
}
Else
{
Print "sending error: $! ";
Exit (0 );
}

$ Buf = "";
My $ senderip = Recv (S, $ Buf, 196, 0 );
If ($ senderip)
{
}
Else
{
Print "receiving error: $! ";
Exit (0 );
}

If (substr ($ Buf, 0, 3) eq "+ OK ")
{
}
Else
{
Print "Incorrect password! ";
Exit (0 );
}

Print password is correct!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.