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

Hi,
I have a perl executable (kindof like an application) which is running on a remote machine... And people in that place only could control the exe ( like input data, accept choices etc. )
To do all that from my place, i've started to use sockets. Since I'm new to sockets and i've got to do the code in less than a week, i've not experimented with it much.
My client looks like this:
$sockclient = new IO::Socket::INET (PeerAddr => $remote_host, PeerPort => $remote_port, Proto => 'tcp', ); die "Socket could not be created. Reason: $!\n" unless $sockclient +;
And my server like this :
foreach my $hostvar (@remote_host) { $sockserver = new IO::Socket::INET (LocalHost => $hostvar, LocalPort => $remote_port, Proto => 'tcp', Listen => 5, Reuse => 1 + ) or warn "Socket could not be created in $hostvar. Reason: $!" unless +$sockserver; } while ($new_sock = $sockserver->accept()) { while (defined ($buf = <$new_sock>)) { # do something with $buf, and ask confirmation, input #etc. from + the client and again come back ... in a loop. } } close ($sockserver); }
Could anybody suggest a reliable approach to this so that :
1. My server is running infinitely,
2. My client is connected to the server, and sends info.
3. My server does something with the data and asks client for more input..
4. 2 and 3 continously until some pre-defined key is pressed.

Thanks...Rupz.

Replies are listed 'Best First'.
Re: Sock-Server and Sock-Client
by AcidHawk (Vicar) on Apr 16, 2003 at 10:18 UTC

    Appologies for the VERY long post. I assume that you are running on a Win32 platform as you talk about an exe. You might have a look at running your server as a Win32 Service (Not for the faint hearted) but Re: Install a perl script as a Win NT/2000/XP service. should give you some ideas, or Re: kill a child / parent process (Service Skeleton) where you can get a kick start for the service stuff.

    As far as the client connecting to the server, the server doing stuff with what it receives and exiting the client when a key is pressed there are many references to this type of thing you just need to scratch around a bit. If you have made the server into a Win32 Service all you have to do to stop the server is to Stop the Service.

    Below is a simple example. I cant remember if I made any changes to the following code and so, have NOT tested it, but it should give to a good start.

    Update:Added Service Installer code.

Re: Sock-Server and Sock-Client
by castaway (Parson) on Apr 16, 2003 at 11:36 UTC
    Umm, that looks a little confused. Your server only needs to listen to one (local) IP and port, which the clients then connect to, thats the point of it being a server. (Unless I misunderstood what you are trying to do).
    For an example of doing that and looping indefinitely, see Re: Daemons in Perl, and just add:
    $x-add(\*STDIN);
    and:
    if($handle == \*STDIN) ..
    in appropriate places to be able to read from the command line and stop the program when a key is pressed.
    And fill in the parse_external_command sub to deal with data from the client.

    C.

      My first problem is this:
      how do i write back to the client from the server, which is running indefenitely?
      And Yes, the server listens to only one client...
        You just print to the socket you accepted. It's two-way.. So in your example, that would be:
        print $new_sock "Hallo!\n";
        I'm not sure what you mean by only listening to one client, I meant one IP/Port pair.

        C.

Re: Sock-Server and Sock-Client
by rupesh (Hermit) on Apr 16, 2003 at 09:30 UTC
    Update:
    The server code states
    foreach my $hostvar (@remote_host) { $sockserver = new IO::Socket::INET (LocalHost => $hostvar,
    because, the requirement is that the client can be run from anyplace.. so the server listens to many clients present in the array, until a connection is established and then continues...
    Any alternatives regarding this issue also will be great!

    Thanks again!!!