Solved

Perl socket connect send and wait for 2 packet

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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 …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

687 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