Re: My first socket question



In article <5npnh25e3ck8md6hauvsh0f1din2ddn3cb@xxxxxxx>, Michele Dondi
<bik.mido@xxxxxxxxxxxxx> wrote:

This is my first C/S socket question, so bear with me if the answer is
obvious or simple. I want to write a program that behaves like a
server if called with no arguments and like a client if supplied some,
sending commands based on those arguments to the server and waiting
for a reply. The following minimal example *does* work for me:


#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket;
use constant PORT => 9000;


if (@ARGV) {
my $sock=IO::Socket::INET->new(PeerAddr => 'localhost',
PeerPort => PORT,
Proto => 'tcp') or
die "Cannot connect to server: $!\n";
my $cmd=shift;
print "Client mode, sending command `$cmd'\n";
print $sock "$cmd\r\n";
print scalar <$sock>;
close $sock or die "Cannot disconnect from server: $!\n";
exit;
}

my $server=IO::Socket::INET->new(Listen => 5,
LocalPort => PORT,
Proto => 'tcp') or
die "Cannot start in server mode: $!\n";
print "Server mode\n";

while ( my $client=$server->accept ) {
(my $cmd=<$client>) =~ tr/\r\n//d;
my %answer=(foo => "foo: that's what I wanted!");
print $client
$answer{$cmd} || "Unknown command: `$cmd'", "\r\n";
}
close $server or die "Cannot stop server mode: $!\n";

__END__


Point is, in the *real* application I have in mind, when in server
mode the program will be doing other stuff as well. To be fair, its
main purpose will be doing other stuff. The C/S ipc's purpose will be
exactly of gathering information on how other stuff is being done or
influence how it is. Thus the actual code may be of the form

while (1) {
do_stuff;
???
}

where ??? is the code to handle incoming requests. What should I put
there?

A call to IO::Select::can_read to see if a client is trying to connect.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.



Relevant Pages

  • Re: Unable to print to networked printer - get access denied messa
    ... Check the permissions on the server assuming the client has a true RPC ... How is the Standard TCP/IP port configured for the device? ...
    (microsoft.public.windowsxp.print_fax)
  • Re: interfaces lo:1 lo:2 lo:3? (for remote ssh tunnels)
    ... That's the problem tunneling (port forwarding) solves. ... >>can't get past the client firewall. ... > I don't understand why the server would be making the ... server initiates another connection to the client -- in this ...
    (Debian-User)
  • Re: Remote Connection Issue
    ... through port number 3389 and a workstation on the LAN through port number ... I understand that you want to allow a LAN client ... and you have configured server publishing rule ... > By default Terminal Server and Windows 2000 Terminal Services uses TCP ...
    (microsoft.public.windows.server.sbs)
  • Re: RealVNC
    ... Default listening port for RealVNC server that runs on the machine on which ... Then there is default Java listening port on port 5800 on the client machine ...
    (microsoft.public.windows.server.sbs)
  • Re: Redirecting data sent to a local printer to another host and port on the network
    ... All client workstations have access to the ... simply redirecting netcat traffic on port 9100 to port 515 on ... Only LPR clients talk to LPD print server daemons. ... >workstation at the branch site where the print job originated. ...
    (comp.unix.sco.misc)