Gossamer Forum
Home : General : Perl Programming :

Server Trouble

Quote Reply
Server Trouble
Hello, i'm trying to set up a basic webserver. But i'm having difficulty even getting started. Below is my code, which I set up on a machine and leave running. I then attempt to connect to the machine on a different machine using the web browser. I use the following format, machine_name.domain.ca : port. The error I get from the browser is "Server taking to long to respond" and it lists some possible reasons why. This makes it seem like it's partially working. Any help would be great. Thanks.

Code:
#!/usr/bin/perl -w

use strict;
use Socket;

# use assigned port as default
my $port = shift || 12901;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket( SOCKET, PF_INET, SOCK_STREAM, $proto )
or die "Can't open socket $!\n";

setsockopt( SOCKET, SOL_SOCKET, SO_REUSEADDR, 1 )
or die "Can't set socket option to SO_REUSEADDR $!\n";

# bind to a port, then listen
bind( SOCKET, sockaddr_in( $port, INADDR_ANY ) )
or die "Can't bind to port $port! \n";

listen( SOCKET, SOMAXCONN ) or die "listen: $!";

print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ( $client_addr = accept( NEW_SOCKET, SOCKET ) )
{
# send them a message, close connection
print NEW_SOCKET
"HTTP/1.1 200 OK\015\012" .
"Content-Type: text/plain\015\012" .
"\015\012" .
"Hello, This is a test\015\012";

close NEW_SOCKET;
}