sajanagr has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I am new in socket programming and trying some luck to implement a sample program found on net.

The server code is -

#!/usr/bin/perl -w # server1.pl - a simple server use strict; use Socket; # use port 7890 as default my $port = shift || 7890; my $proto = getprotobyname('tcp'); # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; + # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "bind: $!"; listen(SERVER, SOMAXCONN) or die "listen: $!"; print "SERVER started on port $port\n"; # for each connection... my $client_addr; while ($client_addr = accept(CLIENT, SERVER)) { # find out who connected my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ipnum = inet_ntoa($client_ip); my $client_host = gethostbyaddr($client_ip, AF_INET); # tell who connected print "got a connection from: $client_host"," [$client_ipnum]\n"; # send them a message, close connection print CLIENT "Hello from the server: ", close CLIENT; }

Client code is -

#! /usr/bin/perl -w # client1.pl - a simple client use strict; use Socket; # initialize host and port my $host = shift || 'sajanar.random.com'; my $port = shift || 7890; my $proto = getprotobyname('tcp'); # get the port address my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET, $paddr) or die "connect: $!"; print "\n\nsajan before \n\n"; my $line; while ($line = <SOCKET>) { print $line; } close SOCKET or die "close: $!";

After this, i start server code and it says -

SERVER started on port 7890

and listens for any client connection. It works fine till here.

But, when I run client code. The error comes at server side and client doesnt get any socket message and exits.

Server side error -

got a connection from: sajanar.random.com [192.168.30.117] print() on closed filehandle CLIENT at ./perl_server.pl line 28.

I have tried a lot to debug the issue, but not getting any closer to solution. I feel there is something missing.

Kindly help me solve this issue.

Not all who wander are lost ---- /me

Replies are listed 'Best First'.
Re: New in socket programming .. getting error in socket handle
by moritz (Cardinal) on Jun 11, 2010 at 13:09 UTC
    See also.

    Don't you think it's a waste of volunteer's effort to post the same question in two message boards, without even linking to the other? In how many other boards have you pasted the same question?

    Perl 6 - links to (nearly) everything that is Perl 6.
      Sorry moritz, i didn't realize that i have posted it twice.. it was not intended..
Re: New in socket programming .. getting error in socket handle
by almut (Canon) on Jun 11, 2010 at 14:48 UTC

    It'll work a lot better if you replace the comma with a semicolon...

    print CLIENT "Hello from the server: ", ^

    With a comma, the subsequent close CLIENT is evaluated before the print (because it is an argument to print), which is why you get the error.

      Hi almust, thanks for the solution.. it worked perfectly fine with semicolon.. cudn't debug this.. thanks a ton