Home php教程 PHP开发 Socket programming using Perl language

Socket programming using Perl language

Dec 16, 2016 pm 01:54 PM

Network programming is a mysterious and complex art, but of course it is also very interesting. The Perl language provides a wealth of TCP/ip network functions, all of which are directly derived from the socket library functions of the C language. Since the socket library functions of the Perl language and the C language are the same in form and usage, so I can use the Perl language for Socket programming, and of course I can also use the C language for Socket programming.
The following is a list of related socket library functions in the Perl language:
Instructions for using function prototypes
accept(NEWSOCKET, GENERICSOCKET) accepts the requested socket Connection. If successful, return the network address in compressed form; otherwise return FALSE.
Example:
if(!$Connect=accept(NEW,HANDLE))
{
die"Connectionfailed:$!
";
}
bind (SOCKET, NAME) establishes the binding between NAME and SOCKET, where NAME should be the compressed address corresponding to the correct type of socket.
If successful, return true; otherwise return false.
When using sockets for network programming, this function is very useful Important, because it establishes the association between the socket handle and an address on the network.
Example:
bind(SH,$SocketAddress);
connect(SOCKET,NAME) attempts to connect with the accept() function that has been called and is waiting to establish a connection. Another process has a conversation.
If successful, return true; otherwise return false.NAME should be the compressed address corresponding to the correct type of SOCKET handle
Example:
connect(SOCK,$address)||die"Can'tconnectwithremotehost:$!
";
gethostbyaddr(ADDRESS,TYPE) converts the compressed network address into a name and address that is easier for people to read and understand.
When only the IP address of the host is known, 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)
where, $name is the host name corresponding to the IP address, $alias is other aliases corresponding to $name, $addrtype is the type of network address, $length is the length of the address, and $address is a list of compressed IP addresses.
Example:
$PackedAddress=pack("C4",$IPAddr);
($name, $alias,$addrtype,$length,$address)
=gethostbyaddr($PackedAddress,2);
gethostbyname(NAME) is similar to the gethostbyaddr() function above, but here the host name is used as a parameter. The format of the returned information is completely Same.
Example:
$Host="stuff.com";
($name,$alias,$addrtype,$length,$address)
=gethostbyname($Host);
@IP=unpack("C4", $address[0]);
$HostIP=join(".",@IP);

The original code of the program to verify the email password
The following code works on 263.net and pop.netease under two operating systems. com's two POP3 servers were rigorously tested and proved to be successful.
First type:
Operating system: Windows98 Chinese version
WWW server: Apache1.3.9forWin
Perl interpreter: ActiveStateToolCorp's PerlforWin32, version5.005_03builtforMSWin32-x86-object
Second type:
Operating system: RedHatlinux6.1
WWW server: Apache1.3.6forLinux
Perl interpreter: version5.005_03builtfori386-linux

#!/usr/bin/perl
#test.pl
#Authorhomepage: http: //spot.126.com
usestrict;
useSocket;

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="Sna4x8";

($name,$aliases,$proto)=getprotobyname("tcp");
($name,$aliases, $port)=getservbyname($port,"tcp")
unless$port=~/^d $/;;
($name,$aliases,$type,$len,$thataddr)=gethostbyname($pop3server);

my$this=pack($sockaddr,$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"Unable to bind! :$!";
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"Receive error: $!";
exit(0);
}

if(substr($BUF,0,3)eq" OK")
{
}
else
{
print"POP3 server error!

";
exit(0);
}

my$BUFFER="USERzhangsan";
$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 "Receive error: $!";
exit(0);
}

if(substr($BUF,0,3)eq" OK")
{
}
else
{
print"No such account!

";
exit(0);
}

$BUFFER="PASS12345678 ";
$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 "Receive error: $!";
exit(0);
}

if(substr($BUF,0,3)eq" OK")
{
}
else
{
print "Wrong password!";
exit(0);
}

print The password is correct!

The above is the content of Socket programming using Perl language. For more related articles, please pay attention to the PHP Chinese website (www .php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use socket programming in Python for data communication How to use socket programming in Python for data communication Oct 18, 2023 am 11:06 AM

Title: Socket Programming and Code Examples in Python Introduction: In the modern Internet era, data communication is everywhere. Socket programming in Python provides a simple and effective way to realize data transmission on the network. This article will introduce how to use Python's socket module for data communication, and provide specific code examples to help readers better understand and apply socket programming. 1. What is socket programming? Socket, that is, socket, is the implementation of

Develop efficient web crawlers and data scraping tools using Vue.js and Perl languages Develop efficient web crawlers and data scraping tools using Vue.js and Perl languages Jul 31, 2023 pm 06:43 PM

Use Vue.js and Perl languages ​​to develop efficient web crawlers and data scraping tools. In recent years, with the rapid development of the Internet and the increasing importance of data, the demand for web crawlers and data scraping tools has also increased. In this context, it is a good choice to combine Vue.js and Perl language to develop efficient web crawlers and data scraping tools. This article will introduce how to develop such a tool using Vue.js and Perl language, and attach corresponding code examples. 1. Introduction to Vue.js and Perl language

How to use C++ for network programming? How to use C++ for network programming? Nov 03, 2023 am 11:10 AM

With the development of the Internet, network programming has increasingly become an important part of computer science. As a powerful programming language, C++ also provides a lot of support for network programming. This article will introduce how to use C++ for network programming. Network Programming Overview Network programming is the process of writing programs for communication using computer networks. Network programming requires the use of network protocols (such as TCP/IP) for data transmission. In network programming, there are two main roles: client and server. The client refers to the program that initiates the request, and

How to use PHP's socket programming function? How to use PHP's socket programming function? Jun 03, 2023 pm 09:51 PM

PHP is a programming language widely used in web development and supports many network programming applications. Among them, Socket programming is a commonly used way to implement network communication. It allows programs to implement inter-process communication and transmit data through the network. This article will introduce how to use Socket programming functions in PHP. 1. Introduction to Socket Programming Socket (socket) is an abstract concept that represents an open port in network communication. A process needs to connect to this port in order to communicate with other processes.

Best practices for building high-performance web applications using Python and Perl Best practices for building high-performance web applications using Python and Perl Jun 17, 2023 am 08:09 AM

In today's digital age, web applications are becoming more and more common, making our lives more convenient and efficient. Python and Perl are two widely used programming languages, both of which are ideal for building web applications. However, to build high-performance web applications, you need to master some best practices. This article will introduce some best practices for building high-performance web applications in Python and Perl. Choosing the right web framework Choosing a suitable web framework is a critical step in designing high-performance web applications.

Develop system scripts and automation tools using Vue.js and Perl languages Develop system scripts and automation tools using Vue.js and Perl languages Jul 29, 2023 pm 04:57 PM

Using Vue.js and Perl language to develop system scripts and automation tools In the current software development environment, system scripts and automation tools have become important tools for developers to save time and improve efficiency. In this article, we will introduce how to develop system scripts and automation tools using Vue.js and Perl language, and provide some code examples. Vue.js is a popular JavaScript framework for building user interfaces. It adopts a component-based development approach, allowing developers to

Beginner's Guide to PHP: Socket Programming Beginner's Guide to PHP: Socket Programming May 21, 2023 am 08:31 AM

PHP Getting Started Guide: Socket Programming Socket programming refers to a way of communicating on a computer network. Through Socket programming, we can implement many types of network applications, such as chat rooms, online games, Internet calls, etc. In the field of Web development, Socket programming also plays a very important role, such as realizing real-time communication, push messages and other functions. In this way, we can make Web applications more colorful and interactive. PHP is a very

Socket programming and server-side writing in Go language Socket programming and server-side writing in Go language May 31, 2023 pm 11:40 PM

With the rapid development of the Internet, more applications require network communication, and socket programming has become an important programming method. The Go language is a programming language that has attracted much attention in recent years, and it also has unique advantages in the field of network programming. This article will introduce socket programming in Go language and how to write a simple server program. 1. Overview of socket programming Socket is an abstraction layer provided between the application layer and the transport layer, which allows applications to communicate over the network.

See all articles