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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Sockets Problem (Networking)



Aug 17, 2002, 3:21 PM

Post #1 of 2 (797 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.



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

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

$servername = "";
$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>";
print "Socket Failed<br>";
connect(SOCK, $serveraddr) || print "Connection Failed : $!<br>";

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

Many Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


Aug 17, 2002, 4:44 PM

Post #2 of 2 (795 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