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 works in a fashion that it first receives the message from the client and then it sends the message to the client and then receives the message from the client and then sends the message to the client and so on. but what i actually need is it may first receive or it may first send and also receives more than one messages from the client one after another. and send more than one messages to the client one after another. Suggest any any modication in the code for it.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); open CONFIGFILE, "config.txt" or die $!; { local $/; $/ = undef; $DATA=<CONFIGFILE>; } print $DATA; # Create a config my $Config = Config::Tiny->new(); # Open the config, replace with read and $DATA with filename my $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;<br> 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
  • Comment on How my server programmer is bi-directional using the send() and recv() functions???
  • Download Code

Replies are listed 'Best First'.
Re: How my server programmer is bi-directional using the send() and recv() functions???
by cdarke (Prior) on Aug 21, 2006 at 12:36 UTC
    I suggest you investigate the four argument version of select, either from perlfunc or IO::Select. This enables you to wait on both read and write sockets, since it appears you do not know who will be doing the write first.
Re: How my server programmer is bi-directional using the send() and recv() functions???
by zentara (Cardinal) on Aug 21, 2006 at 12:52 UTC
    For a good basic introduction with alot of comments, see the Perl section at golden teaching notes. After that, you will see how the Perl modules do all the dirty work for you.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
A reply falls below the community's threshold of quality. You may see it by logging in.