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:
Sockets Problem (Networking)

 



benchivers
Novice

Aug 17, 2002, 3:21 PM

Post #1 of 2 (515 views)
Sockets Problem (Networking) Can't Post

I am trying to make a program which will try and connect to a remote computer or server, I can successfully create a new socket for the client, but when the client tries to connect an error returns "Connection Refused". My program code is display on this post, any ideas anyone?

I am quite new to networking in perl, and the program I am trying to create is a CGI script to run on a server, if the user can successfully connect to the server I want to print "Connection Successful" if a server is unavailable to connect to I want to print "Connection Unsuccessful".

Any help would be most helpful.

Code:



#!/usr/bin/perl

use Socket;
print "Content-type: text/html\n\n";

my ($server, $port, $proto, $serveraddr);

$servername = "www.supersitespro.com";
$port = 2001;
$port = getservbyname($port, 'tcp');
$server = gethostbyname($servername) || dienice("Unable to get host by name : $!");
$proto = getprotobyname('tcp');

$serveraddr = sockaddr_in($port, $server);

if (socket(SOCK, PF_INET, SOCK_STREAM, $proto)){
print "Socket OK<br>";
}
else{
print "Socket Failed<br>";
}
connect(SOCK, $serveraddr) || print "Connection Failed : $!<br>";
close(SOCK);

sub dienice{
my($msg) = @_;
print "$msg";
exit;
}



Many Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


Paul
Enthusiast

Aug 17, 2002, 4:44 PM

Post #2 of 2 (513 views)
Re: [benchivers] Sockets Problem (Networking) [In reply to] Can't Post

Do you actually need to use sockets or are you just trying to check if the server is reachable?

If so I'd use Net::Ping or LWP::UserAgent

 
 


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

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