LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2006, 04:51 PM   #1
AbsoluteMonkey
Member
 
Registered: Mar 2004
Location: Boston
Distribution: Fedora Core 10
Posts: 58

Rep: Reputation: 15
Perl sockets and http posting


So I'm trying to create a perl script that opens a socket on port 5555 and my web browser (internet explorer) then connects to it and the perl script connects to another server which is defined in the script. Then outputs the data from the browser socket to the other socket and any data from the other socket to the browser socket. Basically a "go between". Here's my code:
Code:
#!/usr/bin/perl -w
# serverfork.pl - a server that forks a child
# process to handle client connections
use IO::Socket;
use Sys::Hostname;
use POSIX qw(:sys_wait_h);
sub REAP {
    1 until (-1 == waitpid(-1, WNOHANG));
    $SIG{CHLD} = \&REAP;
}
$SIG{CHLD} = \&REAP;
my $sock = new IO::Socket::INET(
                   LocalPort => 5555,
                   Proto     => 'tcp',
                   Listen    => SOMAXCONN,
                   Reuse     => 1,
                   Blocking  => 0);
$sock->autoflush(1);
$sock or die "no socket :$!";
my($new_sock, $buf, $kid);
while ($new_sock = $sock->accept()) {
    # execute a fork, if this is
    # the parent, its work is done, 
    # go straight to continue
    next if $kid = fork;
    die "fork: $!" unless defined $kid;
    # child now...
    # close the server - not needed
    #close $sock;
    $needed = true;
    if($kid){
    }else{
	    while ($needed) {
			my $outSock = new IO::Socket::INET (
				PeerAddr => 'XXX.XXX.XXX.XXX',
				PeerPort => '80',
				Proto => 'tcp',
			); die "Could not create socket: $!\n" unless $outSock;
			$done = 0;
			$post = 0;
			print "______________REQUEST_________________\n";
			while($done == 0) {
				print "Reading...\n";
				$buf = <$new_sock>;
				print "Read.\n";
				@header = split(/ /, $buf);
				print $outSock $buf;
				print $buf;
				if($header[0] eq "POST"){
					$post = 1;
				}
				if($buf =~ /^\r\n/ && $post == 0){
					$done = 1;
					print "Done get: $done\n";
				}if($buf =~ /^\r\n/ && $post == 1){
					print "Post Data: ".<$new_sock>;
					$done = 1;
					print "Done post: $done\n";
				}
			}
			print "______________RESPONSE_________________\n";
			while (($buf = <$outSock>)) {
				print $new_sock $buf;
				#print $buf;
			}
			print "______________END_________________\n";
			if(!$outSock){
				close($outSock);
				$needed = false;
			}
	    }
    }
    #exit;
} continue {
    print "Closing\n";
    # parent closes the client since
    # it is not needed
    close $new_sock;
}
The problem I am having is that it blocks or something whenever there is any post data. I try and read the post data and it just hangs. I've tried everything I can think of, like setting non-block but I believe it's set by default, but setting it did nothing.
I tried sysread() to read the amount of bytes that the http request says the content length is. That didn't work....or maybe I didn't do it right.
 
Old 08-13-2006, 08:58 PM   #2
carcassonne
Member
 
Registered: Jul 2005
Distribution: Fedora6 x86_64
Posts: 118

Rep: Reputation: 15
Quote:
Originally Posted by AbsoluteMonkey
So I'm trying to create a perl script that opens a socket on port 5555 and my web browser (internet explorer) then connects to it and the perl script connects to another server which is defined in the script.
I've never did it like this but I did some TCP stuff with Perl usig POE (Perl Object Environment). The POE cookbook has a few examples in the same vein as what you're trying to do, check it out: http://poe.perl.org/

With POE you not only get a lot of TCP stuff, you also get a full concurrent multitasking framework that's quite fun to use. I'd say that it's worthwhile to get into as you'll discover a lot of fun stuff. After that, doing a utility that watches for the presence of a file and send a TCP message when it appears is a snap to do and only requires a couple of lines of codes. Proxies, http servers, IRC servers, port forwarders, TCP servers/clients, job scheduling, are all regular POE tasks that are found in the POE cookbook.

Check it out.
 
Old 08-13-2006, 09:31 PM   #3
AbsoluteMonkey
Member
 
Registered: Mar 2004
Location: Boston
Distribution: Fedora Core 10
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks for the info on POE. It might be what I need. Although what this script will be when it's finished is far away from the POE examples, they defiantly do provide me with a starting point.

But I'm still curious as to why I'm having the 'blocking' issue. It's driving me crazy.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP POST with perl AM1SHFURN1TURE Programming 4 01-07-2006 11:03 PM
Non blocking sockets in Perl ivanatora Programming 3 08-19-2005 02:14 AM
PERL sockets bahadur Programming 4 05-08-2005 05:01 AM
Perl 403 HTTP Response barkers Linux - Software 4 04-27-2005 08:07 AM
Perl HTTP Greg21 Linux - Newbie 0 02-25-2003 03:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration