|
|
Socket Programming in Perl
|
| Article by: | Premshree Pillai (9/22/2003) |
|
| Summary: | This is the Perl implementation of converting an Infix expression to Postfix and vice-versa. |
|
| Viewed: 78225 times |
Rating (120 votes): |
|
4.3 out of 5 |
|
|
|
Socket Programming in Perl
Using sockets in Perl, it is possible to communicate to a server using a client
program. Here, I will explain how using sockets a client program can communicate
to a server. The client program is run at the client-side while the server
program runs in the Internet. For testing the program, you can run it on a local
server.
For this example, I will use the IO::Socket::INET Perl module
which is included with most Perl distributions. If you don't have the module,
get it from http://www.cpan.org/modules/index.html.
We will use the User Datagram Protocol (UDP) to connect to the server. The
module also supports the TCP protocol.
Client Program :
First of all we need to create the socket. The socket is created easily
by initializing a new instance of the IO::Socket::INET object like this :
MySocket, thus inherits all the properties and methods of the IO::Socket::INET
object. The various key-value pairs we have passed to the newly created
object are as follows :
- PeerPort = 1234; The PeerPort can be a number or symbolic service name.
- Proto = 'udp'; We use the UDP protocol. The TCP protocol is also supported.
If Proto is not provided, it will try to derive it from the service name
(PeerPort). If this fails, 'tcp' is assumed as the Proto.
- PeerAddr = 'localhost'. The PeerAddr can be a host name or an IP address.
Here we are tesing the programs locally so we have used 'localhost'. If the
server program is running in the Internet then you must use it's IP address or
domain name.
Apart from the key-value pairs we have passed, there are
many other parameters that can be passed to the object. We are not concerned of
them here. You can find details of accepted key-value pairs in the module's
documentation.
Now that we have created the socket, we can now send a
message to the server using the object's send() method :
Thus we have created the client program. Now let us create the server program,
that will display the messages that the client has sent.
Server
Program :
In the server program also, we create the socket in a
similar manner :
|
Select All Code
|
|
Notice that in the server program we have passed only two key-value pairs :
- LocalPort = 1234; this is the local host bind port and should be same as the
PeerPort value passed in the client program.
- Proto = 'udp'; Protocol used in communication with client, it should be same
as the Proto value passed in the client program.
Now after creating
the socket, we can now receive messages sent by the client using the object's
recv() method :
|
Select All Code
|
|
Here the first argument passed to the recv() method must be '$text', the second
argument passed indicates the maximum number of bytes the server should receive.
After receiving the message, it can be easily displayed using the $text
variable.
Thus we have created the server program. Now the client can
communicate with server using a socket. Note that for the server to receive
messages from the client, it(server) should be running while the message is
being sent by the client.
Client program code :
|
Select All Code
|
|
We have refined the client program so that it accept inputs from the keyboard
which is transmitted to the server. A double return(pressing enter/return twice)
will exit the client program.
Server program code :
|
Select All Code
|
|
We have refined the server program so that as and when the client sends a
message, the server keeps displaying it. When the client sends a blank message,
the server program terminates.
Thus we have learned how to communicate
to a server from a client using sockets.
© 2002 Premshree Pillai. Website: http://premshree.resource-locator.com
|
|
View highlighted Comments
User Comments on 'Socket Programming in Perl'
|
Posted by :
Unencoded.net at 11:56 on Thursday, November 30, 2006
|
big tnx 2 u. Great article!
| |
Posted by :
bhavna at 04:33 on Wednesday, January 10, 2007
|
I wrote a script in Perl which displays the Outlook Folders but now it is showing
all the folders along with their subjects but it is not showing the new messages.
i have done the same thing in VB.net .There it worked
but i m nt getting the syntax same as
' Get NameSpace and Logon.
Dim appNameSpace As Microsoft.Office.Interop.Outlook.NameSpace = app.GetNamespace("MAPI")
appNameSpace.Logon("Outlook", "bhavna123", False, True)
I got the syntax for
Dim appNameSpace As Microsoft.Office.Interop.Outlook.NameSpace = app.GetNamespace("MAPI")
i.e.my $namespace = $Outlook->GetNamespace("MAPI");
But the problem is I m nt getting the syntax as
appNameSpace.Logon("Outlook", "password", False, True)
Kindly please help me.
| |
Posted by :
naveen882 at 00:50 on Tuesday, November 20, 2007
|
Hi,
someone knows how to connect to IBM as400 database through perl script.pls give the sample code.Thanks,on advance
| |
Posted by :
anjaliraman at 22:59 on Tuesday, January 22, 2008
|
Hi,
I tried executing the same program as given in the example above using the proto as 'tcp'instead of 'udp'.
I found that in the server it was giving me the message - "Cilent has exited!". I suppose this socket programming does not work for "tcp" as the protocol.
Please provide me a working code for this program using tcp as a protocol.
Thanks
Anjali Raman
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| |