CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Socket Connection To Remote Server

 



shelato
New User

Mar 30, 2010, 5:57 PM

Post #1 of 7 (2887 views)
Socket Connection To Remote Server Can't Post

I have a microcontroller with a built in server connected to my home router. I am able to send it messages from an asp.net webpage with vb code that looks like this:

*************************

Dim s As Socket = Nothing

Dim address As IPAddress = 75.133.140.33

address = IPAddress.Parse(url)

Dim port As Integer = 8246

Dim endPoint As New IPEndPoint(address, port)

Dim
bytesSent As [Byte]() = ascii.GetBytes("message")

Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)

tempSocket.Connect(address, 8246)


If
tempSocket.Connected Then

tempSocket.Send(bytesSent, bytesSent.Length, 0)

End if

tempSocket.Close()

************

I've been trying to write a perl socket routine but can not make it connect. I've been trying different version of this code:

use IO::Socket;

my $sock = new IO::Socket::INET (PeerAddr => '75.133.140.33', PeerPort => '8246', Proto => 'tcp');

die "Could not create socket: $!\n" unless $sock;

$sock->send("message");

close($sock);

Can anyone give me some pointers? I've been googling "perl socket connection" and it turns up a lot of information, but it doesn't seem to apply directly to what I'm trying to do. When I use my asp.net website with vb routine above, I'm able to send the message "FWD" to my microcontroller and monitor the receipt of the message with a control panel that shows the incomming message. When I use the perl socket connction above, I can not get a response. I'm also able to use telnet.

Best regards,



Dwight



MetalGear
Novice


Mar 30, 2010, 10:54 PM

Post #2 of 7 (2880 views)
Re: [shelato] Socket Connection To Remote Server [In reply to] Can't Post

you are trying to send the string "message" or any string stored in variable message?

if you are sending FWD stored in variable message...then use $message, instead of "message".


7stud
Enthusiast

Apr 1, 2010, 5:55 AM

Post #3 of 7 (2869 views)
Re: [shelato] Socket Connection To Remote Server [In reply to] Can't Post

I don't see anything obviously wrong with your script. For kicks, you might trying using syswrite() instead of send(). However, my book says that they are pretty much equivalent when using a tcp connection (it says send() is used for udp).

And IO::Socket objects have autoflush turned on by default, so I don't see how it could be a buffering problem.

A potential problem would occur if your server was trying to read input in a line oriented fashion. That means your server would be looking for newlines to tell it when to stop trying to read data. I looked at the VB definition for Send and it seems to be similar to perl's syswrite(), i.e. it doesn't automatically add a newline to the output. That does raise the question: how does your server know when to stop trying to read data?


Quote
I'm able to send the message "FWD" to my microcontroller and monitor the receipt of the message with a control panel that shows the incomming message. When I use the perl socket connction above, I can not get a response.


...you mean a "message"? If your control panel doesn't show a message coming in, that sounds like you are using the wrong ip address. But I guess if your control panel only shows a message has arrived after the server stops trying to read incoming data, your server could be blocking and never finish its read.

You might want to try writing a perl server script on your home pc, and then try to get your web page script to connect to it. That might highlight some issues for you.


(This post was edited by 7stud on Apr 3, 2010, 7:52 AM)


shelato
New User

Apr 3, 2010, 4:47 PM

Post #4 of 7 (2843 views)
Re: [7stud] Socket Connection To Remote Server [In reply to] Can't Post

Thanks 7Stud,

Running from the command line on my pc was a very good suggestion. I was able to see some of the errors, correct them and then make the script run and communicate with my server. But....it hangs up when I run it from a web server. I'm able to make this script work on my PC and communicate with my microcontroller server. The IP address I'm using is that of my home router/modem (the address below is not the real address) and the port has been forwarded to the microcontroller server:

#!/usr/local/bin/perl
use IO::Socket;
print "Content-type: text/html\n\n";
my $sock = new IO::Socket::INET (
PeerAddr =>'75.133.145.33',
PeerPort =>8246,
Proto =>tcp
)
or die print "Could not create socket: $!\n" unless $sock;
$sock->syswrite("FWD.");
close($sock);
print "Test Complete\n";

However, when I try to run this script from a web server, it hangs up and does not complete. There is no internal error, it just keeps trying to load.

If I substitue a dot com address from one of my web sites and port 80, the script finishes and prints "Test complete". I don't understand why it can't connect to my IP address. It does work when I run it from my pc, but not from the server. Any other ideas?

Here is the script that will finish, but it doesn't help me because I need it to connect to my IP address, not a dot com addresss:

#!/usr/local/bin/perl
use IO::Socket;
print "Content-type: text/html\n\n";
my $sock = new IO::Socket::INET (
PeerAddr =>'vusofa.com',
PeerPort =>80,
Proto =>tcp
)
or die print "Could not create socket: $!\n" unless $sock;
$sock->syswrite("FWD.");
close($sock);
print "Test Complete\n";




Thanks again,



Dwight


shelato
New User

Apr 3, 2010, 6:16 PM

Post #5 of 7 (2839 views)
Re: [shelato] Socket Connection To Remote Server [In reply to] Can't Post

One more piece of info. When I use this script, it dies after the 2 second timeout and gives me the error "Bad file descriptor ". Any idea what it means?

#!/usr/local/bin/perl
use IO::Socket::INET;
print "Content-type: text/html\n\n";
use constant DESTINATION => '75.130.144.33:8246';
my $sock = new IO::Socket::INET(Proto=>'tcp',PeerAddr=>DESTINATION,Timeout=>2)
or die print "Could not create socket: $!\n" unless $sock;
$sock->syswrite("FWD.");
close($sock);
print "Test Complete\n";

Again, it runs okay from the command line on my pc, but errors from the web server.



Dwight


7stud
Enthusiast

Apr 4, 2010, 9:27 AM

Post #6 of 7 (2807 views)
Re: [shelato] Socket Connection To Remote Server [In reply to] Can't Post

Search google for 'code tags'. Read the first 10 results and come back and post what you have learned.


shelato
New User

Apr 4, 2010, 12:57 PM

Post #7 of 7 (2797 views)
Re: [7stud] Socket Connection To Remote Server [In reply to] Can't Post

This would be the code that returns the error "Could not create socket: Bad file descriptor " when I run it from a remote web server. If I substitute a web address like vusofa.com:80 in place of the IP address, it finishes with "Test Complete". I also tried registering the IP address with a DNS and public dot com, but I still end up with a Bad file descriptor error. Both do work from my pc running perl from the command line.

Would the host of the server block sockets using IP addresses. I tried it on two different hosted servers but got the same bad file descriptor error.


Code
  

#!/usr/local/bin/perl
use IO::Socket::INET;
print "Content-type: text/html\n\n";
use constant DESTINATION => '75.133.143.33:8246';
my $sock = new IO::Socket::INET(Proto=>'tcp',PeerAddr=>DESTINATION,Timeout=>2)
or die print "Could not create socket: $!\n" unless $sock;
$sock->syswrite("FWD.");
close($sock);
print "Test Complete\n";





dwight

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives