Jason Gerfen wrote:
> I am new to this list so if this is the wrong place to ask please let me
> know. I am attempting to build a client/server application using the
> IO::Socket package. I am having a hard time implimenting a simple
> server which can send/receive messages in response to a client that can
> also send/recieve messages.
>
> As of yet I have only been able to get the server to listen, accept, and
> return a response to the client send, the client at that point blocks
> and cannot retrieve the servers response to its initial send. Below is
> the code I am working with, any help is appreciated;
>
> [server.pl]
> sub socket
> {
> print "Binding: $host on port $port, please wait...\n";
> my $sock = new IO::Socket::INET( LocalHost => $host,
> LocalPort => $port,
> Proto => 'tcp',
> Listen => $max_clients,
> Resuse => 1 );
> die "Error: Could not create socket - $!\n" unless $sock;
> print "Daemon bound at $host on $port\n";
> print "Awaiting connections...\n";
> for( ;; ) {
> while( my $new = $sock->accept ) {
> print "Recieved connection from " . $new->peerhost . " : " .
> $new->peerport . "\n";
> $sock->autoflush( 1 );
> $sock->blocking( 0 );
> while( defined( my $data = <$new> ) ) {
> if( $data =~ /^status$/ ) {
> $buffer = &GetStatus;
> print "Command STATUS recieved, sending data...\n";
> print $new $buffer;
> print "Sent to client:\n$buffer\n";
> close( $new );
You may want to consider leaving the socket open until you get an
error/disconnect/quit command from the client (unless that's not
feasible in your scenario).
> } elsif( $data =~ /^stats$/ ) {
> @data = &GetStats;
> print "Command STATS recieved, sending data...\n";
> foreach $buffer( @data ) {
> print $new $buffer;
> print "$buffer\n";
> close( $new );
> }
> } else {
> $new->send( "Error: Command not
> recognized.\nAvailable Commands: stats | status\n" );
> print "Error: Command not recognized from client -
> $data\n";
> print $new "Error: Command not recognized.\nAvailable
> Commands: stats | status\n";
> close( $new );
> }
> }
> }
> }
> }
>
> [client.pl]
> sub Connect
> {
> print "Connecting to $host on $port...\n";
> my $sock = new IO::Socket::INET( PeerAddr => $host,
> PeerPort => $port,
> Proto => 'tcp' );
> die "Error: Could not create socket - $!\n" unless defined( $sock );
> $sock->autoflush( 1 );
I would avoid the fork on Win32 (assuming that's where you are).
print "Sending $command command to $host on $port...\n";
$sock->send($command);
while (my $buffer = <$sock>) {
print "Server data:\n";
print "$buffer\n";
}
$sock->close;
> die "Error: Could not fork process - $!\n" unless defined( my $pid =
> fork() );
> if( $pid ) {
> while( my $buffer = <$sock> ) {
> print "Server data:\n";
> print "$buffer\n";
> }
> close( $sock );
> kill( "TERM", $pid );
> } else {
> print "Sending $command command to $host on $port...\n";
> $sock->send( $command );
> }
> }
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs