Solved

Perl socket connect send and wait for 2 packet

Posted on 2007-11-29
4
2,050 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Frameworks 1 56
inactive users 13 53
Image decoding from Camera 3 47
Adoquery sql  left join does not work 25 50
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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now