I'm really new to socket programming and I have the following code (don't mind the client, still needs to be worked on). I used it as a web proxy for firefox and went to " And while it "worked", my command prompt kept on beeping with garbled characters (i assume from the Google image on the homepage) and it took a good 10 minutes to load the google homepage. then i got stuck and never closed the connection.
Q1: is there a limit to the muber of bytes/chars that can be sent at a time? (that's why my cmd$ kept on beeping when FF was loading the image?)
Q2: why is it taking so long?
Q3: how can I implement using tabs (thread? i read it's slow)
#!c:\bin\perl -w
use IO::Socket;
my $proxy_port=9000;
my $hostname=`hostname`;
chop $hostname;
# socket
$sock = new IO::Socket::INET (LocalHost => $hostname,
LocalPort => $proxy_port,
Proto => 'tcp',
Listen => 5,
Reuse => 1
) || die "Listening Socket could not be created: $!" unless $sock;
while (1) {
# Accept connection request
print "Waiting for connection request....\n\n";
$client_sock = $sock->accept() || die "accept $!";
# Create child process
if (fork) {
wait;
next;
}
$clientaddr = getpeername ($client_sock);
($port,$iaddr) = sockaddr_in($clientaddr);
$clientname = gethostbyaddr ($iaddr, AF_INET);
print "Received Connection Request from: ".inet_ntoa($iaddr)."\n";
$firstline = <$client_sock>;
print "*********** HTTP request ************\n";
print $firstline."\n";
($modifiedfirstline,$remote_host,$remote_port,$method)=analyze($firstline);
$server_sock=connect_server($remote_host,$remote_port);
print $server_sock $firstline;
while (<$client_sock>) {
print $_;
next if (/Proxy-Connection:/);
print $server_sock $_;
last if ($_ =~ /^[\s\x00]*$/);
}
if ($method eq "POST") {
$data = <$client_sock>;
print $data;
print $server_sock $data;
}
print "HTTP Request sent. Here's the response: \n\n";
server_response($server_sock, $client_sock);
close $client_sock;
close $server_sock;
exit;
}
sub analyze {
($request)=@_;
if ($request =~ m#(GET|POST|HEAD) [^/:]+):?(\d*)#) {
$method=$1;
$remote_host=$2;
$remote_port=$3;
print "Recognized HTTP request \n";
}
print "Received $method request for remote_host $remote_host at port
$remote_port...\n";
# Remove remote hostname from URL
$request =~ s/http:\/\/[^\/]+//;
return $request,$remote_host,$remote_port,$method;
}
sub connect_server {
($remote_host,$remote_port) = @_;
if ($remote_port !~ /^\d+$/) {
$remote_port = (getservbyname($remote_port, "tcp"))[2];
$remote_port = 80;
}
$server_sock = new IO::Socket::INET (PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => 'tcp') || die "Server Socket could not be created: $!";
print "Connected to server $remote_host at port $remote_port";
return $server_sock;
}
sub server_response {
($server_sock, $client_sock) = @_;
while ($line = <$server_sock>) {
print $line;
print $client_sock $line;
}
print "Finished receiving...\n";
}
Q1: is there a limit to the muber of bytes/chars that can be sent at a time? (that's why my cmd$ kept on beeping when FF was loading the image?)
Q2: why is it taking so long?
Q3: how can I implement using tabs (thread? i read it's slow)
#!c:\bin\perl -w
use IO::Socket;
my $proxy_port=9000;
my $hostname=`hostname`;
chop $hostname;
# socket
$sock = new IO::Socket::INET (LocalHost => $hostname,
LocalPort => $proxy_port,
Proto => 'tcp',
Listen => 5,
Reuse => 1
) || die "Listening Socket could not be created: $!" unless $sock;
while (1) {
# Accept connection request
print "Waiting for connection request....\n\n";
$client_sock = $sock->accept() || die "accept $!";
# Create child process
if (fork) {
wait;
next;
}
$clientaddr = getpeername ($client_sock);
($port,$iaddr) = sockaddr_in($clientaddr);
$clientname = gethostbyaddr ($iaddr, AF_INET);
print "Received Connection Request from: ".inet_ntoa($iaddr)."\n";
$firstline = <$client_sock>;
print "*********** HTTP request ************\n";
print $firstline."\n";
($modifiedfirstline,$remote_host,$remote_port,$method)=analyze($firstline);
$server_sock=connect_server($remote_host,$remote_port);
print $server_sock $firstline;
while (<$client_sock>) {
print $_;
next if (/Proxy-Connection:/);
print $server_sock $_;
last if ($_ =~ /^[\s\x00]*$/);
}
if ($method eq "POST") {
$data = <$client_sock>;
print $data;
print $server_sock $data;
}
print "HTTP Request sent. Here's the response: \n\n";
server_response($server_sock, $client_sock);
close $client_sock;
close $server_sock;
exit;
}
sub analyze {
($request)=@_;
if ($request =~ m#(GET|POST|HEAD) [^/:]+):?(\d*)#) {
$method=$1;
$remote_host=$2;
$remote_port=$3;
print "Recognized HTTP request \n";
}
print "Received $method request for remote_host $remote_host at port
$remote_port...\n";
# Remove remote hostname from URL
$request =~ s/http:\/\/[^\/]+//;
return $request,$remote_host,$remote_port,$method;
}
sub connect_server {
($remote_host,$remote_port) = @_;
if ($remote_port !~ /^\d+$/) {
$remote_port = (getservbyname($remote_port, "tcp"))[2];
$remote_port = 80;
}
$server_sock = new IO::Socket::INET (PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => 'tcp') || die "Server Socket could not be created: $!";
print "Connected to server $remote_host at port $remote_port";
return $server_sock;
}
sub server_response {
($server_sock, $client_sock) = @_;
while ($line = <$server_sock>) {
print $line;
print $client_sock $line;
}
print "Finished receiving...\n";
}