Perl socket problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Moree

    Perl socket problem

    I'm attempting to use a perl script to interface with my Visual Basic 6
    program using Winsock. I've got my program setup to connect and works
    great if i connect to another winsock program, but it acts funny
    connecting to my perl script. The Perl code is below:

    #!c:/Perl/bin/Perl.exe
    use IO::Socket;
    $server = IO::Socket::INE T->new(LocalAdd r => '10.40.0.10',
    LocalPort => '8777',
    Proto => 'tcp',
    Listen => 1,
    Reuse => 1);

    die "ERROR: $!\n" unless $server;

    print "Waiting on connections...\ n";
    while($client = $server->accept()){
    print "Connection made, reading data...\n";
    print <$client>;
    print "Connection closed...\n";
    }

    $server->close();

    All the program is supposed to do is open a socket on port 8777 for tcp.
    Listen for any incomming connections then read the one line of data
    comming in. I've read on the internet and in my perl in a nutshell book
    that the above code should work. But they all say the same thing. You
    must get the line of data and scan it for a character that lets the perl
    script that the line is done. Problem is, when the script runs, i get as
    far a connection made then it will do nothing until i close the VB Winsock.

    Anyone got a reader for sockets that will know when to stop reading? I
    haven't found any examples on the internet on how to do it, just that
    everyone says it can be done. I'd really appreciate the help!

    Daniel Moree
  • octoberdan
    New Member
    • Sep 2005
    • 18

    #2
    I ran it on windows XP and tested it with telnet... Make sure the VB client sends a newline... You made me late for psychology class :p
    Code:
    #!/usr/local/bin/perl
    
    use strict;
    use warnings;
    
    use IO::Socket;
    
    my $client;
    my $socket;
    my $message;
    
    $socket = IO::Socket::INET->new(
            "Proto" => "tcp",
            "LocalPort" => "8177",
            "Listen" => 1) or die "ERROR: $!\n";
    
    print "Waiting for connection...\n";
    
    # Blocks untill connection is made
    $client = $socket->accept();
    
    print "Connection received\n";
    
    # Blocks untill message is received
    $message = <$client>;
    
    print "Incoming message: $message\n";
    
    close $client;
    close $socket;

    Comment

    • Guest's Avatar

      #3
      Re: Perl socket problem

      [color=blue]
      > #!c:/Perl/bin/Perl.exe
      > use IO::Socket;
      > $server = IO::Socket::INE T->new(LocalAdd r => '10.40.0.10',
      > LocalPort => '8777',
      > Proto => 'tcp',
      > Listen => 1,
      > Reuse => 1);
      >
      > die "ERROR: $!\n" unless $server;
      >
      > print "Waiting on connections...\ n";
      > while($client = $server->accept()){
      > print "Connection made, reading data...\n";
      > print <$client>;
      > print "Connection closed...\n";
      > }
      >
      > $server->close();
      >
      > All the program is supposed to do is open a socket on port 8777 for tcp.
      > Listen for any incomming connections then read the one line of data
      > comming in. I've read on the internet and in my perl in a nutshell book
      > that the above code should work. But they all say the same thing. You
      > must get the line of data and scan it for a character that lets the perl
      > script that the line is done. Problem is, when the script runs, i get as
      > far a connection made then it will do nothing until i close the VB[/color]
      Winsock.[color=blue]
      >
      > Anyone got a reader for sockets that will know when to stop reading? I
      > haven't found any examples on the internet on how to do it, just that
      > everyone says it can be done. I'd really appreciate the help![/color]

      In my opinion you should use function $char=getc($ser ver) in the loop and
      analyze text you reach in this way.
      In my programs it works very well.





      Comment

      • robkeeney
        New Member
        • Jun 2006
        • 1

        #4
        >> Anyone got a reader for sockets that will know when to stop reading? I
        >> haven't found any examples on the internet on how to do it, just that
        >> everyone says it can be done. I'd really appreciate the help![/color]

        >In my opinion you should use function $char=getc($ser ver) in the loop and
        >analyze text you reach in this way.
        >In my programs it works very well.
        See: http://poe.perl.org/?POE_RFCs/Window..._compatibility

        The problem is that non-blocking sockets don't work on NT. All sockets are forced to block and wait for a new-line or EOF. Using getc() and testing for some end marker is one way around it, but using sysread() works better. It reads as many chars as are in the buffer without blocking.

        Ex.

        $retries = 30;
        $bytes_to_read = 1024;
        do {
        while ( sysread( $socket, $data, $bytes_to_read ) == $bytes_to_read )
        {
        $xyz .= $data;
        }
        last if $xyz =~ /endmarker/;
        sleep 1;
        } while --$retries;

        This will try to read from the socket for about 30 seconds or until the end marker is found.
        BTW, DO NOT try to use read() or recv() on the socket! See the perl docs on sysread() for more info.

        Comment

        Working...