Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket programming 1

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
0
0
US
I have a server and client program roughly written that will connect on a socket and return output, etc.

What I cannot figure out is to execute the server program and have it execute the client program, for example:

server-side# ./server.pl

client-side# (starts running client.pl when server.pl was executed on server-side)
 
In your server.pl call client.pl. Without knowing how you connect to client.pl I dont' not sure which option will fit your need. Some options could be:

qx
system
` `
 
client-side# (starts running client.pl when server.pl was executed on server-side)

If server.pl is connecting to client, then clinet.pl can be called from with in the server.pl, post a successful connection or am I reading the post incorrectly?
 
Hi

max1x said:
If server.pl is connecting to client,
... then server.pl would actually be a client application.

Sorry, but I feel that cryptoadm's request is a nonsense.

The best workaround would be to implement a retry functionality in client.pl, start it, then let it wait & retry to connect to server.pl until succeeds.

Feherke.
 
I guess I'm confused as to what the op was requesting being done. It seemed to me as op can actually connect to the client and just run a client.pl there and didn't know how to call it from server.pl.

thanks for the clarification Feherke.
 
client
Code:
#!/usr/bin/perl

use IO::Socket;
my $sock = new IO::Socket::INET (
                                 PeerAddr => 'localhost',
                                 PeerPort => '7183',
                                 Proto => 'tcp',
                                 );
die "Could not create socket: $!\n" unless $sock;

$data_file = "test.dat";
open(DAT, $data_file) || die "Could not open file: $!\n";
while (<DAT>) {
        my $msg = $_;
        chomp $msg;
        print $sock "$msg\n";
}
close(DAT);
close($sock);

server
Code:
#!/usr/bin/perl

use IO::Socket;
use Net::hostent;

$PORT = 7183;

my $sock = new IO::Socket::INET (
                                 LocalHost => 'localhost',
                                 LocalPort => $PORT,
                                 Proto => 'tcp',
                                 Listen => 1,
                                 Reuse => 1,
                                 );
die "Could not create socket: $!\n" unless $sock;

my $new_sock = $sock->accept();
$hostinfo = gethostbyaddr($new_sock->peeraddr);

printf "[Connect from %s]\n", $hostinfo->name || $new_sock->peerhost;
        while(<$new_sock>) {
                $new_sock->autoflush(1);
                print $_;
        }
close($sock);

So when I start the server side code I would like to have it run the client.
 
Is the client always going to be on the same machine?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
No it will be on a different machine but the localhost for both is just for testing for now.
 
If you want the server to call the client your going to have to make the client also listen (like the server) and have it always be running (like the server) and ready to respond.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
could you have the server launch the client with an SSH module, or am I also confused?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top