Intro to using the IO NET lib for TCP.pl
Jump to navigation
Jump to search
Contents
Intro_to_using_the_IO_NET_lib_for_TCP.pl Overview
This lesson covers how to import perl modules and use the IO::INET perl module. This module gives us a means to use TCP/UDP networking to make our application network enabled to communicate with other applications remotely.
Intro_to_using_the_IO_NET_lib_for_TCP.pl Code
Save the code below in a file called Intro_to_using_the_IO_NET_lib_for_TCP.pl:
#############################################################################
#
# Example: intro_to_using_IO_NET_lib.pl
#
# Okay, Sink or Swim lesson lol. This is going to cover:
#
# - How to import a lib.
# - How to use the IO::INET lib to program network sockets.
#
# For details about the inet lib see:
#
# http://perldoc.perl.org/IO/Socket/INET.html
#
# I'll cover the basics but I recommend reading that as well.
#
# basically we will use this lib to create an 'object' using a 'new'
# constructor. The keywords that can be used in the constructor are
# as follows:
#
# PeerAddr - Remote host address <hostname>[:<port>]
# PeerHost - Synonym for PeerAddr
# PeerPort - Remote port or service <service>[(<no>)] | <no>
# LocalAddr - Local host bind address hostname[:port]
# LocalHost - Synonym for LocalAddr
# LocalPort - Local host bind port <service>[(<no>)] | <no>
# Proto - Protocol name (or number) "tcp" | "udp" | ...
# Type - Socket type SOCK_STREAM | SOCK_DGRAM | ...
# Listen - Queue size for listen
# ReuseAddr - Set SO_REUSEADDR before binding
# Reuse - Set SO_REUSEADDR before binding (deprecated, prefer ReuseAddr)
# ReusePort - Set SO_REUSEPORT before binding
# Broadcast - Set SO_BROADCAST before binding
# Timeout - Timeout value for various operations
# MultiHomed - Try all addresses for multi-homed hosts
# Blocking - Determine if connection will be blocking mode
#
# To add to this, This class inherits all the features of IO::Socket :-)
#
#############################################################################
###
#### First thing we must do is import the lib. We do this with the keyword
#### 'use'.
###
use IO::Socket::INET;
###
#### First let's create a sock to connect to this machine on a port
#### that is listening - port 25. We will do this with the simple
#### <ip>:<port> notation. If a protocol isn't specified, it assumes tcp.
###
print "Connecting to 127.0.0.1:25...";
$sock = IO::Socket::INET->new('127.0.0.1:25') or die "Couldn't connect to SMTP service :-( \n\n";
print "connected!\n\n";
###
#### Following the unix mentality of "Everything is a file." the network socket can be treated.
#### as a file handle. putting "print <fileHandle> <Msg>" will write the message to whatever the
#### handle is attached to.
###
$data = <$sock>;
print "DATA FROM SERVER: $data";
###
#### Now our turn. Let's send a HELO command back!
###
$response = "HELO derp\n";
print "DATA TO SEND: $response";
###
#### Following the unix mentality of "Everything is a file." the network socket can be treated.
#### as a file handle. putting "print <fileHandle> <Msg>" will write the message to whatever the
#### handle is attached to.
###
print $sock "$response";
###
#### And let's get Our Response.
###
$data = <$sock>;
print "DATA FROM SERVER: $data";
###
#### We could respond with a QUIT command and the server would disconnect from
#### us.
###
$response = "QUIT\n";
print "DATA TO SEND: $response";
print $sock "$response";
$data = <$sock>;
print "DATA FROM SERVER: $data";
print "\n [*] Done Son!\n\n";
###
#### Or We can kill it ourselves using the close() function.
###
#print "\nDisconnecting from server\n\n";
#$sock->close();
#print "\n [*] Done Son!\n\n";
###
#### And for shit's and giggles. Let's try to connect to a non-existing port.
#### This is where the "or die" part comes in. if it couldn't then it will error,
#### and throw our message. then exit the program.
###
print "Connecting to 127.0.0.1:666...";
$sock = IO::Socket::INET->new('127.0.0.1:666') or die "Couldn't connect to port 666 :-( \n\n";
print "connected!\n\n";
###
#### Just in the off chance it was there. we close the socket.
###
$sock->close();
Intro_to_using_the_IO_NET_lib_for_TCP.pl from a Shell
[user@localhost]$ perl Intro_to_using_the_IO_NET_lib_for_TCP.pl
[user@localhost]$
Code Breakdown
To be Continued