Solved

Perl socket connect send and wait for 2 packet

Posted on 2007-11-29
4
2,055 Views
Last Modified: 2013-12-25
Hello

I want a simple perl code example which will connect to an IP and port, sends a string packet like "Hello", then waits to receive 2 packets (without carriage return or line feed, I mean without enter), then it will send another packet like "bye" again.

I have a lot of codes but I cannot do it. I cannot wait for 2 message to receive. Please advice.

Thanks from now!
0
Comment
Question by:CSecurity
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Suhas .
ID: 20380106
use IO::Socket;

    my $sock = IO::Socket::INET->new('some_server');
    $sock->read($data, 1024) until $sock->atmark;


for more details go through:
http://search.cpan.org/~gbarr/IO-1.2301/IO/Socket.pm 
0
 
LVL 9

Accepted Solution

by:
Suhas . earned 500 total points
ID: 20380119
# Client Program
use IO::Socket::INET;
print ">> Client Program <<";

# Create a new socket
$MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',PeerAddr=>'localhost');

# Send messages
$def_msg="Enter message to send to server : ";
print "\n",$def_msg;
while($msg=<STDIN>)
{
    chomp $msg;
    if($msg ne '')
    {
        print "\nSending message '",$msg,"'";
        if($MySocket->send($msg))
        {
            print ".....<done>","\n";
            print $def_msg;
        }
    }
    else
    {
        # Send an empty message to server and exit
        $MySocket->send('');
        exit 1;
    }
}


#######################################

# Server Program
use IO::Socket::INET;
print ">> Server Program <<\n";

# Create a new socket
$MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp');

# Keep receiving messages from client
$def_msg="\nReceiving message from client.....\n";
while(1)
{
    $MySocket->recv($text,128);
    if($text ne '')
    {
        print "\nReceived message '", $text,"'\n";
    }
    # If client message is empty exit
    else
    {
        print "Cilent has exited!";
        exit 1;
    }
}


reference: http://www.codetoad.com/perl_socket_programming.asp
0
 
LVL 17

Author Comment

by:CSecurity
ID: 20380189
Thanks for your first comment, but it did not waited... Second example was irrelevant I think. there is nothing for waiting to receive a buffer, it's client and not server app
0
 
LVL 17

Author Comment

by:CSecurity
ID: 20380257
Thanks I found it. I'm using recv.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question