470,863 Members | 1,139 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

Error occured "accept() on closed socket SERVER at proxy.pl line"

Hi all

I am working on a socket program in Perl. Main goal is to develop a proxy server. Here is the code attached. An error is encountered. Anyone Please help me out.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # server1.pl - a simple server
  3.  
  4. use strict;
  5. use Socket;
  6. use HTTP::Response;
  7. use LWP::UserAgent;
  8. use LWP::Simple;
  9. use LWP 5.64;
  10.  
  11. # use port 6555 as default
  12. my $port = shift || 6555;
  13. my $proto = getprotobyname('tcp');
  14.  
  15. # create a socket, make it reusable
  16. socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
  17. setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
  18.  
  19. # grab a port on this machine
  20. my $paddr = sockaddr_in($port, INADDR_ANY);
  21.  
  22. # bind to a port, then listen
  23. bind(SERVER, $paddr) or die "bind: $!";
  24. listen(SERVER, SOMAXCONN) or die "listen: $!";
  25.  
  26. print "SERVER started on port $port\n";
  27.  
  28. my $client_addr;
  29. while (1) {
  30.     # find out who connected
  31.     $client_addr = accept(CLIENT,SERVER);
  32.     my ($client_port, $client_ip) = sockaddr_in($client_addr);
  33.     my $client_ipnum = inet_ntoa($client_ip);
  34.     my $client_host = gethostbyaddr($client_ip, AF_INET);
  35.  
  36.     my $pid = fork();
  37.     if ($pid == 0) {
  38.         &connection;
  39.     }
  40.     close SERVER or die "close: $!";
  41. }
  42.  
  43. sub connection() {
  44.     my $url = 'http://www.cs.memphis.edu/';
  45.     my $browser = LWP::UserAgent->new;
  46.     my $response = $browser->get( $url );
  47.     my $result= $response->content;
  48.     print CLIENT $result;
  49. }
  50.  

Thanks in advance
Vasudha
May 4 '07 #1
2 2256
miller
1,089 Expert 1GB
Hi Vasu,

When debugging code and an error occurs, It is often helpful to actually share what the error is. Also, please start including your code within CODE tags. I've had to fix this in your posts multiple times now.

- Miller
May 4 '07 #2
Hi Miller,

Sorry about that. The following are the errors encountered....

accept() on closed socket SERVER at proxyn.pl line 31.
Use of uninitialized value in subroutine entry at /usr/lib/perl5/5.8.0/i386-linux-thread-multi/Socket.pm line 370.
Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16 at /usr/lib/perl5/5.8.0/i386-linux-thread-multi/Socket.pm line 370.


Thanks
Vasu
May 4 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Grant Richard | last post: by
12 posts views Thread by Yarco | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.