By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,367 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,367 IT Pros & Developers. It's quick & easy.

Socket Connection To Mico Controller

P: 4
Hi,

I am able to open a socket connection to a micro controller, connected to a lynksys router, using visual basic code on a windows web site. Now I'm trying to open a socket connection using Perl on a linux server. I'm not having any luck at all. I'm not completely new to perl programs, but most of my experience is in simply installing and modifying freeware programs. Can anyone tell me what's wrong with this code?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use IO::Socket; 
  3. my $sock = new IO::Socket::INET (
  4.     PeerAddr => '24.179.20.129',  #not real address
  5.     PeerPort => '59004', 
  6.     Proto => 'tcp', 
  7.     ); 
  8.  
  9. or die "Could not create socket: $!\n" unless $sock; 
  10. print $sock "R1R0"; 
  11. close($sock);
  12. print "Content-type: text/html\n\n";
  13. print "Hi there!\n";
  14. exit;
  15.  
I get an Internal Server Error

if I comment out the "or die" line and the "print $sock" line it will run without an error and print "hi there", but the socket obviously does not do anything.

What I'm trying to do is send the command R1R0 to a micro controller which in turn controls a toy robot. I'm able to do it with visual basic, but not perl.

Thanks!!!

Dwight
Mar 16 '08 #1
Share this Question
Share on Google+
6 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi,

I am able to open a socket connection to a micro controller, connected to a lynksys router, using visual basic code on a windows web site. Now I'm trying to open a socket connection using Perl on a linux server. I'm not having any luck at all. I'm not completely new to perl programs, but most of my experience is in simply installing and modifying freeware programs. Can anyone tell me what's wrong with this code?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use IO::Socket; 
  3. my $sock = new IO::Socket::INET (
  4.     PeerAddr => '24.179.20.129',  #not real address
  5.     PeerPort => '59004', 
  6.     Proto => 'tcp', 
  7.     ); 
  8.  
  9. or die "Could not create socket: $!\n" unless $sock; 
  10. print $sock "R1R0"; 
  11. close($sock);
  12. print "Content-type: text/html\n\n";
  13. print "Hi there!\n";
  14. exit;
  15.  
I get an Internal Server Error

if I comment out the "or die" line and the "print $sock" line it will run without an error and print "hi there", but the socket obviously does not do anything.

What I'm trying to do is send the command R1R0 to a micro controller which in turn controls a toy robot. I'm able to do it with visual basic, but not perl.

Thanks!!!

Dwight
I would question whether you need the "or" before the die statement. You are saying to "die, unless this is true", so I don't think you need it.

I think the "or" is only needed if you put it at the end of something like an open.

Regards,

Jeff
Mar 16 '08 #2

P: 4
Thanks Jeff. Taking the "or" out did not help. I still get a 500 Error. I can make the following "work", at least it does not return an error. It makes me think there is something wrong with my use of an IP Address in the PeerAddr line. Also, I can not find an error in the server error log.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!/usr/local/bin/perl
  4. use IO::Socket; 
  5. my $sock = new IO::Socket::INET (
  6.     #PeerAddr => '24.179.20.999', 
  7.     #PeerPort => '59004', 
  8.     PeerAddr => 'www.shelato.com', 
  9.     PeerPort => 'http(80)', 
  10.  
  11.     Proto => 'tcp', 
  12.     ); 
  13. die "Could not create socket: $!\n" unless $sock; 
  14. print $sock "R1R0"; 
  15. close($sock);
  16. print "Content-type: text/html\n\n";
  17. print "Hi there!\n";
  18. exit;
  19.  
  20.  
  21.  
The project I'm working on can be seen at the home page in my profile. I'm able to open a winsock using VB from my laptop, and I can open a socket using VWD Experss on a windows server with ASP.net 3.5. But I really want to move my robot control panel over to a Linux server and use Perl. I'm fairly confident my receiving router and port forwarding is working since these other programs work.
Mar 16 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Thanks Jeff. Taking the "or" out did not help. I still get a 500 Error. I can make the following "work", at least it does not return an error. It makes me think there is something wrong with my use of an IP Address in the PeerAddr line. Also, I can not find an error in the server error log.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!/usr/local/bin/perl
  4. use IO::Socket; 
  5. my $sock = new IO::Socket::INET (
  6.     #PeerAddr => '24.179.20.999', 
  7.     #PeerPort => '59004', 
  8.     PeerAddr => 'www.shelato.com', 
  9.     PeerPort => 'http(80)', 
  10.  
  11.     Proto => 'tcp', 
  12.     ); 
  13. die "Could not create socket: $!\n" unless $sock; 
  14. print $sock "R1R0"; 
  15. close($sock);
  16. print "Content-type: text/html\n\n";
  17. print "Hi there!\n";
  18. exit;
  19.  
  20.  
  21.  
The project I'm working on can be seen at the home page in my profile. I'm able to open a winsock using VB from my laptop, and I can open a socket using VWD Experss on a windows server with ASP.net 3.5. But I really want to move my robot control panel over to a Linux server and use Perl. I'm fairly confident my receiving router and port forwarding is working since these other programs work.
Just something I noticed, but your use statement says "use IO::Socket;", but in your "new" declaration, you are specifying "IO::Socket::INET". If you look at the page for that module, the use statement should also contain that module, including the INET piece.

So, it looks like you are specifying one module and trying to use another. I would change my use statement to be:

Expand|Select|Wrap|Line Numbers
  1. use IO::Socket::INET;
  2.  
And if it isn't already installed, then install it before executing the script. The reason I say this is IO::Socket::INET inherits all of the methods and variables from IO::Socket, but it isn't used unless you specify its use statement.

Regards,

Jeff
Mar 16 '08 #4

P: 4
Hi,

Any other ideas? I know it's a little odd to be communicating over the internet with a robot. I'm not sure where else to go for help on the perl socket connection.

Thanks again,

Dwight
Mar 18 '08 #5

P: 4
Just something I noticed, but your use statement says "use IO::Socket;", but in your "new" declaration, you are specifying "IO::Socket::INET". If you look at the page for that module, the use statement should also contain that module, including the INET piece.

So, it looks like you are specifying one module and trying to use another. I would change my use statement to be:

Expand|Select|Wrap|Line Numbers
  1. use IO::Socket::INET;
  2.  
And if it isn't already installed, then install it before executing the script. The reason I say this is IO::Socket::INET inherits all of the methods and variables from IO::Socket, but it isn't used unless you specify its use statement.

Regards,

Jeff
Sorry jeff, I'm reading from a bberry tonight and missed your reply, will try these changes tomorrow. Thanhs!!!
Mar 18 '08 #6

P: 89
Just something I noticed, but your use statement says "use IO::Socket;", but in your "new" declaration, you are specifying "IO::Socket::INET". If you look at the page for that module, the use statement should also contain that module, including the INET piece.

So, it looks like you are specifying one module and trying to use another. I would change my use statement to be:

Expand|Select|Wrap|Line Numbers
  1. use IO::Socket::INET;
  2.  
And if it isn't already installed, then install it before executing the script. The reason I say this is IO::Socket::INET inherits all of the methods and variables from IO::Socket, but it isn't used unless you specify its use statement.

Regards,

Jeff
use the Type key as SOCK_STREAM or Listen key with que number.

may try this also:

Expand|Select|Wrap|Line Numbers
  1. use IO::Socket::INET;
  2. my $sock = new IO::Socket::INET ( '24.179.20.999:59004' );
  3.  
this will take tcp as default and SOCK_STREAM as default.
If not try to print the $@ to know the error completly.
Mar 18 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.