SPUG: IO::Socket Question

Andrew Crum akcrum at hotmail.com
Wed Aug 9 21:07:24 CDT 2000


My working socket solution without using IO:: is shown below.
Maybe something in it can be borrowed from...
- Andrew Crum
=================
#! /usr/bin/perl -w

use strict;
use FileHandle;
use Socket;

my $rhost = "111.222.333.444";
my $rport = "555";
my $request  = "xxx";
my $reply_line;

# connect
print "connect\n", " socket: $rhost:$rport\n";
socket(SOCKET, PF_INET, SOCK_STREAM, 6);
connect(SOCKET, pack('Sna4x8', AF_INET, $rport, (gethostbyname($rhost))[4]))
or die "couldn't connect to $rhost:$rport: $!\n";
SOCKET->autoflush();
#SOCKET->errmode("return");       #possibly needed to actually wait for a 
response(?)

# send
print "request:\n", "  $request\n";
print SOCKET $request . "\n\n";

# receive
print "reply lines:\n";
while ( ! (($reply_line = <SOCKET>) =~ /^\W*$/) ) {
  print "  ", $reply_line;
}

# close
close SOCKET;
print "done\n";
===========================

----Original Message Follows----
From: Daryn Nakhuda <daryn at marinated.org>
To: spug-list at pm.org
Subject: SPUG: IO::Socket Question
Date: Wed, 9 Aug 2000 18:15:35 -0700 (PDT)

Hi, I'm trying to use IO::Socket to open a socket connection (duh) and
communicate w/ a server.  A typical session would look like:

ME: hello
SERVER: hello

the code looks like this:

$sock = IO::Socket::INET->new(
         PeerAddr => "thehost",
         PeerPort => theportnumber,
         Proto => "tcp")
   or die "Error connecting: $@\n";

$sock->autoflush(1);
print $sock "Hello\n";
print <$sock>;
close ($sock);

This sends the first "Hello" okay, but hangs waiting for the response. I'm
assuming it's a buffering problem, but autoflush(1) didn't seem to help.
Any suggestions for making this work?

thanks.

Daryn


________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For full traffic, use spug-list for LIST ; otherwise use spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For full traffic, use spug-list for LIST ; otherwise use spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list