I recently ran into a situation where I need to have many many raspberry pi's running all over the country and I need to "talk" to them from a central computer to tell them when to run a particular program I have pre-loaded on them. However the people that will be "hosting" them if you will are very very NON-computer savvy people, so I really can't have them configure their routers to set up VNC or the like connections, so I thought, I can solve this with "Sockets" right? :-) So what I want to do is have the pi's talk to my computer and say "I'm on and running, what do you want me to do?" and then I can send back to them what to do. Problem is I can't get the dang things to connect. :-( I have my router set to forward port 7777 to my computer and I have a no-ip service that hosts a name for my router.

But every time the connection times out and won't connect . What am I doing wrong? Here is my code:

Server Code

use IO::Socket::INET; # auto-flush on socket $| = 1; # creating a listening socket my $socket = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '7777', Proto => 'tcp', Listen => 5, Reuse => 1 ); die "cannot create socket $!\n" unless $socket; print "server waiting for client connection on port 7777\n"; while(1) { # waiting for a new client connection my $client_socket = $socket->accept(); # get information about a newly connected client my $client_address = $client_socket->peerhost(); my $client_port = $client_socket->peerport(); print "connection from $client_address:$client_port\n"; # read up to 1024 characters from the connected client my $data = ""; $client_socket->recv($data, 1024); print "received data: $data\n"; # write response data to the connected client $data = "ok"; $client_socket->send($data); # notify client that response has been sent shutdown($client_socket, 1); } $socket->close();

Client Code

#!/usr/bin/perl use IO::Socket::INET; $| = 1; my $socket = new IO::Socket::INET ( PeerHost => 'theaddressofmycomputer.com', PeerPort => '7777', Proto => 'tcp', ); die "Cannot connect to the server $!\n" unless $socket; print "Connected to the server!\n"; my $req = "Hello World\n"; my $size = $socket->send($req); print "Sent data of length $size\n"; shutdown($socket, 1); my $response = ""; $socket->recv($response, 1024); print "Recieved response: $response\n"; $socket->close();
~~~~~~~~~ I'm unique, just like everybody else! ~~~~~~~~~
UPDATE!!!

OK, I got it working, thank you to everyone for all your help, it was the windows firewall. I simply had to create an inbound and outbound rule allowing communication on the port I want and it worked like a charm!


In reply to Socket programming Help by Kelicula

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.