Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   Sockets (http://www.programmingforums.org/showthread.php?t=7367)

ProjectX Dec 2nd, 2005 10:20 PM

Sockets
 
Hey guys its me again. Sorry if I am getting annoying with all the post i did but I need help.So I started to practice with sockets so i made a script that opens a port and listens on it.

:

#!C:/PERL/BIN/perl.exe -w
#
use IO::Socket;
my $sock = new IO::Socket::INET (LocalHost => '192.168.1.100',
                                LocalPort => '1337',
                                Proto => 'tcp',
                                Listen => 1,
                                Reuse => 1,);
die "Could not create socket: $!\n" unless $sock;
my $newsock = $sock -> accept();
while (defined(<newsock>)) {
      print $_;
}
close($sock);


and it gives me this error:
:

Name "main::newsock" used only once: possible typo at C:\DOCUME~1\User\MYDOCU~1\PERLPR~1\Sockets\Socket~1.PL line 11.
and I dont know how to fix it.

sykkn Dec 2nd, 2005 10:53 PM

you left the $ off of newsock in your while test .... also ... you can leave off the defined() call ... it mucks up the input anyway ...

:

while (<$newsock>) {
      print $_;
}


ProjectX Dec 2nd, 2005 11:10 PM

thanks alot syk

ProjectX Dec 8th, 2005 1:33 PM

is there a way to print the output of what the server will print. Like lets say i got this code
:

use IO::Socket;

$socket = IO::Socket::INET -> new (PeerAddr => "www.host.com", PeerPort => 80, Proto => "tcp") || die "Can't connect to the host";
print $socket "Hello, There!\n";


So is there a way to print on my screen what the www.host.com will print on his
like Unrecongnized commnad or just a plain window or Premission Denied.

swestres Dec 27th, 2005 9:58 AM

Sure, just put
:

while (<$socket>) {
  print;
}

at the end. Then all you have to do is to read RFC2616, learn some GUI programming and you've got yourself a webbrowser written in perl :)


All times are GMT -5. The time now is 2:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC