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

i am creating a server program which reads the config file and creates a socket. but it does not send and receive the messages.why? I am giving the code below.
#! usr/bin/perl -w use warnings; use IO::Socket::INET; use strict; use Config::Tiny; my ($DATA,$text, $msg); $/=undef; open CONFIGFILE, "config.txt" or die $!; DATA=<CONFIGFILE>; print $DATA; # Create a config my $Config = Config::Tiny->new(); # Open the config, replace with read and $DATA with filename $Config = Config::Tiny->read_string( $DATA ); # see perldoc Config::Tiny my $s = $Config->{_}; my $MySocket=new IO::Socket::INET->new ( LocalPort => $s->{LocalPort}, Proto => $s->{Proto} ); $MySocket or die "Can't create socket ($!)\n"; $MySocket->autoflush(1); print "Server started :Ready For Packet To Receive:\n"; for(;;) { $MySocket->recv($text,128);#Recv chomp $text; if($text ne "q") { print "\nReceived message:$text"; } print "\nEnter message to send to client or q to quit: "; if(defined($msg=<STDIN>)) { chomp $msg; if($msg ne "q") { $MySocket->send($msg); print "waiting for response: \n"; } else { $MySocket->send("q"); print "\n Server Exited"; exit 1; } } }
The config.txt contains:
LocalPort=1234
Proto=udp
Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: Why my socket is not send and receive data???
by rodion (Chaplain) on Aug 19, 2006 at 14:37 UTC
    Your call to IO::Socket::INET->new isn't quite right.
    my $MySocket=new IO::Socket::INET->new ( LocalPort => $s->{LocalPort}, Proto => $s->{Proto} );
    You've got two "new"s in the line.

      Good catch. He also needs to specify Listen for a server socket.

      Update: I'm so used to seeing TCP code, I first assumed the OP was using TCP. See my other post for more details.

Re: Why my socket is not send and receive data???
by choocroot (Friar) on Aug 19, 2006 at 15:53 UTC
    Why are you globally enabling "slurp mode" (with $/=undef) ?

    This disable the "readline mode" when you use $msg=<STDIN>, so Perl will wait forever until it gets a End-Of-File. It means, that entering your message and hitting ENTER will not give you back the message in $msg, so you'll have to type ^D (CTRL+D) in order to process your entry.

    If you just want to "slurp" your CONFIGFILE (and nothing else), you should first open CONFIGFILE, then locally disable $/ :
    open CONFIGFILE, "config.txt" or die $!; { local $/; $/ = undef; $DATA=<CONFIGFILE>; } print $DATA;
    That way, $/ remains in default "line mode" for your read from <STDIN> (see perlvar for details on scope with $/).

    Beside this "slurp mode" problem, your code seems to run well on my machine with "nc -u localhost 1234" as a client.
      local $/; $/ = undef;

      Have you had problems with local not undefining values? I think that second line is a no-op.

        No, but I'm used to always explicitly define the value of this variable when I want to change it's behaviour in a block.

        But you're right, my "$/ = undef" is redundant here.
Re: Why my socket is not send and receive data???
by ikegami (Patriarch) on Aug 19, 2006 at 18:26 UTC

    The protocol shouldn't be configurable. Or if it is, you need a *lot* more work. Your code assumes UDP is used.

    To also allow TCP, you'd have to specify Listen when you create the socket, you'd have to use accept on the socket to wait for a connection, and you'd have to use sysread and syswrite instead of recv and send.

    Update: I'm so used to seeing TCP code, I first assumed the OP was using TCP.

Re: Why my socket is not send and receive data???
by jeanluca (Deacon) on Aug 19, 2006 at 11:13 UTC