Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with IO::Socket

Reply
Thread Tools

Problem with IO::Socket

 
 
Martin Kissner
Guest
Posts: n/a
 
      02-16-2005
Hello group,

I am trying to use a script from the book "Networkprogramming with Perl"
by Lincoln D. Stein.

----
#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket;

my $server = shift;
my $fh = IO::Socket::INET->new($server);
my $line = <$fh>;
print $line;
----

It is supposed to read the first line from a remote server's welcome
message.

% ./lgetr.pl wuarchive.wustl.edu:daytime
is supposed to output the current date of that server.

% ./lgetr.pl wuarchive.wustl.edu:ftp
should print the welcome message of the ftp service on
wuarchive.wustl.edu

% ./lgetr.pl mail.hotmail.com:smtp
should print the welcome message of the smtp-server on mail.motmail.com

and so on.
In my case the script hangs infinitely without any output.
I have even tried my local smtp server wuth the sam result.

What could be wrong?
Any help will be appreciated.

Regards
Martin

--
perl -e 'print 7.74.117.115.116.11.32.13.97.110.111.116.104.101.1 14.11
..32.13.112.101.114.108.11.32.13.104.97.99.107.101 .114.10.7'
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-16-2005
Martin Kissner <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> What could be wrong?


I am not sure.

> Any help will be appreciated.


FWIW, the services you are trying

1) May not be enabled on the computers you have tried
2) May be behind a firewall
3) May require prior authentication

Sinan
 
Reply With Quote
 
 
 
 
Martin Kissner
Guest
Posts: n/a
 
      02-16-2005
A. Sinan Unur wrote :
> Martin Kissner <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>
> FWIW, the services you are trying
>
> 1) May not be enabled on the computers you have tried
> 2) May be behind a firewall
> 3) May require prior authentication


Thank you for your response, Sinan.

After I had written my answer (pointing out what I have tried to exclude
the reasons you mentioned) I tried using the port numbers instead of the
service name.
This finally worked and leads me to the next question:

Why doesn't the script (or the Perl interpreter) look up the portnumber
in /etc/services. Telnet works properly with given servicenames.

Regards
Martin

--
perl -e 'print 7.74.117.115.116.11.32.13.97.110.111.116.104.101.1 14.11
..32.13.112.101.114.108.11.32.13.104.97.99.107.101 .114.10.7'
 
Reply With Quote
 
phaylon
Guest
Posts: n/a
 
      02-16-2005
Martin Kissner wrote:

> Why doesn't the script (or the Perl interpreter) look up the portnumber in
> /etc/services. Telnet works properly with given servicenames.


- perl (lc) is not only for *ixoid systems.
- telnet is programmed to do so.
- perl can be programmed to do so.
- You can program perl to do so.

--
http://www.dunkelheit.at/
That is not dead, which can eternal lie,
and with strange aeons even death may die.
-- H.P. Lovecraft

 
Reply With Quote
 
chris-usenet@roaima.co.uk
Guest
Posts: n/a
 
      02-16-2005
Martin Kissner <(E-Mail Removed)> wrote:
> Why doesn't the script (or the Perl interpreter) look up the portnumber
> in /etc/services. Telnet works properly with given servicenames.


perldoc -f getservbyname

perl -e 'print join(", ", getservbyname ("telnet", "tcp")), ".\n"'

Chris
 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      02-16-2005
phaylon wrote :
> Martin Kissner wrote:
>
>> Why doesn't the script (or the Perl interpreter) look up the portnumber in
>> /etc/services. Telnet works properly with given servicenames.

>
> - perl (lc) is not only for *ixoid systems.
> - telnet is programmed to do so.
> - perl can be programmed to do so.
> - You can program perl to do so.


Well, to be honest, I am just beginning with networkprogramming with
Perl.
That's why I am reading the book and at this point the above does not
really help.

It is the second example in the book (the first one reads one line
from a file) and is supposed to work without any preparations and also
on windows and classic Mac OS.

Maybe perl can be programmed to do so (I am sure it can), but at this
point, I do not want to work out my own humble solution.

I would rather be interested if anyone out there could run the script
successfully without any changes and with servicenames instead of
portnumbers as it is suggested in the book.

Regrads
Martin

--
perl -e 'print 7.74.117.115.116.11.32.13.97.110.111.116.104.101.1 14.11
..32.13.112.101.114.108.11.32.13.104.97.99.107.101 .114.10.7'
 
Reply With Quote
 
phaylon
Guest
Posts: n/a
 
      02-16-2005
Martin Kissner wrote:

> I would rather be interested if anyone out there could run the script
> successfully without any changes and with servicenames instead of
> portnumbers as it is suggested in the book.


A better way is to use the docs. For IO::Socket::INET it is documented,
that

| If the constructor is only passed a single argument, it is assumed to be
| a PeerAddr specification.

A few lines up, we see

| The PeerAddr can be a hostname or the IP-address on the "xx.xx.xx.xx"
| form. The PeerPort can be a number or a symbolic service name. The
| service name might be followed by a number in parenthesis which is used
| if the service is not known by the system. The PeerPort specification
| can also be embedded in the PeerAddr by preceding it with a ":".

--
http://www.dunkelheit.at/

»Better to reign in hell than to serve in heaven«
-- John Milton, »Paradise Lost«

 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      02-16-2005
phaylon wrote :
> Martin Kissner wrote:
>
>> I would rather be interested if anyone out there could run the script
>> successfully without any changes and with servicenames instead of
>> portnumbers as it is suggested in the book.

>
> A better way is to use the docs. For IO::Socket::INET it is documented,
> that


This will not give me any information on the above question.
btw: I did read the docs for IO::Socket::INET.
>
>| If the constructor is only passed a single argument, it is assumed to be
>| a PeerAddr specification.
>
> A few lines up, we see
>
>| The PeerAddr can be a hostname or the IP-address on the "xx.xx.xx.xx"
>| form. The PeerPort can be a number or a symbolic service name. The
>| service name might be followed by a number in parenthesis which is used
>| if the service is not known by the system. The PeerPort specification
>| can also be embedded in the PeerAddr by preceding it with a ":".


| "The PeerPort can be a number or a service name."
I have used a service name. No contradiction to the docs so far.

And as sugessted in the last sentence I have embedded the service name
in the PeerAddr by preceding it with a ":".
The service is known by the system.

Farther, while finding the solution to my question in the first place, I
did some testing with unexpected results, which I did not want to
mention in my first post to keep it scarce and tidy.

The following script did not run either unless I didn't use use the
line, which is commented out below for creating the socket object.
The syntax for the constructor is taken from the docs.
The number in parantheses seems to be not used and the service name
seems to not work although known by the system.

-----
#!/usr/bin/perl

use IO::Socket;

my $fh = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
# my $fh = IO::Socket::INET->new(PeerAddr => 'localhost:25');
my $line = <$fh>;
print $line;
-----

Also

my $fh = IO::Socket::INET->new(PeerAddr => 'localhost',
PeerPort => 'PeerPort(25)');

did not work as construcor whereas

my $fh = IO::Socket::INET->new(PeerAddr => 'localhost',
PeerPort => '25');

did.

Sorry, but I still do not understand this; therefor I am still
interested if it works for anyone else.
If I have overseen something, please point me to it.

Regards
Martin

--
perl -e 'print 7.74.117.115.116.11.32.13.97.110.111.116.104.101.1 14.11
..32.13.112.101.114.108.11.32.13.104.97.99.107.101 .114.10.7'
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-16-2005
Martin Kissner <(E-Mail Removed)> wrote:
> Hello group,
>
> I am trying to use a script from the book "Networkprogramming with Perl"
> by Lincoln D. Stein.
>
> ----
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> use IO::Socket;
>
> my $server = shift;
> my $fh = IO::Socket::INET->new($server);


One should probably check for success:

my $fh = IO::Socket::INET->new($server) or die $@;

> my $line = <$fh>;
> print $line;
> ----
>
> It is supposed to read the first line from a remote server's welcome
> message.
>
> % ./lgetr.pl wuarchive.wustl.edu:daytime
> is supposed to output the current date of that server.


It gives me a connection refused message.

>
> % ./lgetr.pl wuarchive.wustl.edu:ftp
> should print the welcome message of the ftp service on
> wuarchive.wustl.edu


For me, it does do that.

> % ./lgetr.pl mail.hotmail.com:smtp
> should print the welcome message of the smtp-server on mail.motmail.com


the smtp-server on mail.hotmail.com doesn't seem to have a welcome
message.

> and so on.
> In my case the script hangs infinitely without any output.


The only one that hangs for me is hotmail, and given the behavior of that
server, this is not unexpected.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-16-2005
Martin Kissner <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Sorry, but I still do not understand this; therefor I am still
> interested if it works for anyone else.
> If I have overseen something, please point me to it.


I am not sure. Here is some info:

Microsoft Windows 2000 [Version 5.00.2195]
This is perl, v5.8.6 built for MSWin32-x86-multi-thread

C:\> perl -MIO::Socket::INET -e "print $IO::Socket::INET::VERSION"
1.27

I tried your script against a variety of hosts using the service name
'smtp'. The script worked as expected.

You will need to give some more information regarding the system on which
you are testing this in a clear and concise way.

Now, you can check if perl is able to do the mapping from service names
to port addresses on your system by running this script (from Perl docs):

#! /usr/bin/perl

use strict;
use warnings;

use Net::servent;
my $s = getservbyname(shift) || die "no service";
printf "port for %s is %s, aliases are %s\n",
$s->name, $s->port, "@{$s->aliases}";

__END__

On the system I mentioned above, this gives me:

C:\> perl s.pl smtp
port for smtp is 25, aliases are mail

C:\> perl s.pl chargen
port for chargen is 19, aliases are ttytst source

Exploring from here is an option.

Sinan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments