?
Solved

Perl socket connect send and wait for 2 packet

Posted on 2007-11-29
4
Medium Priority
?
2,064 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Progress
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

765 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