Solved

Perl socket connect send and wait for 2 packet

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

809 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