By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,621 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,621 IT Pros & Developers. It's quick & easy.

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

P: 31
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
Share this Question
Share on Google+
2 Replies

miller
Expert 100+
P: 1,089
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

P: 31
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.