Connecting Tech Pros Worldwide Forums | Help | Site Map

Perl socket

Newbie
 
Join Date: Aug 2007
Location: Croatia
Posts: 3
#1: Aug 27 '07
I cant find mistake in following code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Socket;
  4.  
  5. $host=<STDIN>;
  6. chomp($host);
  7. $port=<STDIN>;
  8.  
  9. $paddr=sockaddr_in($port, inet_aton($host));
  10. $proto=getprotobyname('tcp');
  11.  
  12. socket(SOKET, PF_INET, SOCK_STREAM, $proto) or die "$!";
  13.  
  14. connect(SOKET, $paddr);
  15.  
  16. if(connect(SOKET, $paddr)){
  17.     print "1\n";
  18. } else {
  19.     print "0\n";
  20. }
  21.  

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#2: Aug 27 '07

re: Perl socket


First, you don't have the "use strict" and "use warnings" pragmas at the beginning of your code. Without those there to do away with the little errors, most people won't look at your code.

Second, you haven't exactly told us what error you are receiving.

Third, you may want to also chomp() the input for the $port variable.

Lastly, please place any and all code that you put in your post into code tags. Doing so takes some of the work off of our friendly neighborhood moderators that have to clean up posts. (If you don't know how to use code tags, simply look at the "Reply Guidelines" next to your posting message window.)

Regards,

Jeff
Newbie
 
Join Date: Aug 2007
Location: Croatia
Posts: 3
#3: Sep 1 '07

re: Perl socket


Quote:

Originally Posted by numberwhun

Lastly, please place any and all code that you put in your post into code tags. Doing so takes some of the work off of our friendly neighborhood moderators that have to clean up posts. (If you don't know how to use code tags, simply look at the "Reply Guidelines" next to your posting message window.)

Sorry, I am new on this forum.

and about program: it just print "0".
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#4: Sep 1 '07

re: Perl socket


Now mind you, I have zero experience working with sockets, but I notice in your code that you are doing the 'connect' and then using the same connect statement in an if as a test. Are you trying to see if its connected?

I don't think that that will work the way you have it because I doubt that the connect statement returns anything beyond the connection. I think a better way to do this would be to say:

Expand|Select|Wrap|Line Numbers
  1. connect(SOKET, $paddr) or die "Could not connect:  $!";
  2.  
That will do the connection for you, but if it doesn't work, it will run the die statement and the "$!" will print the error that was produced.

Regards,

Jeff
Newbie
 
Join Date: Aug 2007
Location: Croatia
Posts: 3
#5: Sep 1 '07

re: Perl socket


No errors. So... I made it?
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#6: Sep 1 '07

re: Perl socket


Quote:

Originally Posted by perln00b

No errors. So... I made it?

I would say.

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#7: Sep 1 '07

re: Perl socket


I would also chomp() $port to be safe:

$port=<STDIN>;
chomp($port);
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#8: Sep 1 '07

re: Perl socket


Quote:

Originally Posted by KevinADC

I would also chomp() $port to be safe:

$port=<STDIN>;
chomp($port);

Yeah, that was the third thing I suggested. Hopefully he did it.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#9: Sep 2 '07

re: Perl socket


Quote:

Originally Posted by numberwhun

Yeah, that was the third thing I suggested. Hopefully he did it.


Ahh.... I missed that.
Reply


Similar Perl bytes