Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

IO::Socket

Hello,

I want to write a perlscript using IO::Socket to send messages back and forth to a machines.

Basically a client machine sends a meesage to a server machine, the server does something with that message and then replies back to the client.

The server side:
use IO::Socket;

$server = new IO::Socket::INET(LocalHost => 'xxx.xxx.xxx.xxx',
                    LocalPort => someport,
                    Proto     => 'tcp',
                    Listen    => 5,
                    Reuse     => 1
                   );
die "Socket could not be created. Reason: $!" unless $server;

while($client = $server->accept()) {
  while(defined($buf = <$client>)) {
    print $buf,"\n";
    print $client "Hello client, I got your message\n";
    # do something with $buf
    print $client "Hello client, I finished doing what I had to, you can go now\n";
  }
}
close ($client);


This is what I got for the client side:
use IO::Socket;

$sock = new IO::Socket::INET(PeerAddr => 'xxx.xxx.xxx.xxx',
                    PeerPort => someport,
                    Proto    => 'tcp'
                   );
die "Socket could not be created. Reason: $!\n" unless $sock;

print $sock "Msg $_: Hello server, here is my message\n";
$sock->flush();
while($sock) { print }
close($sock);

------
How do I get the client side code to print out the reply messages the server sent?

Thanks,

Joe
0
jvieira
Asked:
jvieira
  • 2
  • 2
1 Solution
 
TrevizeCommented:
while($sock) in your client is wrong, it must be:

   while(<$sock>) { print }

This loop will never end however as the server never closes the connection - it waits for the client to close the connection first. You can change the server to receive one line, send a reply and close the connection then, the client can reconnect then. Here is how it looks like:

while($client = $server->accept()) {
 if(defined($buf = <$client>)) {
   print $buf,"\n";
   print $client "Hello client, I got your message\n";
   # do something with $buf
   print $client "Hello client, I finished doing what I had to, you can go now\n";
 }
 close ($client);
}
0
 
jvieiraAuthor Commented:
What if the server will reply more than once.  Basically what I want is the client to call the server to do something.  Then the server replies back several times, telling the client it's starting, as it finishes each stage, etc.  Then when the server is finally done it sends the client one final message and tells the client it can now end, but keeps listening for additional requests.  The client will be started by a user and he will be prompted if he wants to send the server additional tasks, or just close the connection and quit.  The server script will be running 24/7 always listening for a client requests.
0
 
jmcgOwnerCommented:
Try this. I moved one line in server and modified the line in the client that Trevize had already pointed out.

#server side:

use IO::Socket;

$server = new IO::Socket::INET(LocalHost => 'xxx.xxx.xxx.xxx',
                   LocalPort => someport,
                   Proto     => 'tcp',
                   Listen    => 5,
                   Reuse     => 1
                  );
die "Socket could not be created. Reason: $!" unless $server;

while($client = $server->accept()) {
 while(defined($buf = <$client>)) {
   print $buf,"\n";
   print $client "Hello client, I got your message\n";
   # do something with $buf
   print $client "Hello client, I finished doing what I had to, you can go now\n";
 }
 close ($client);
}


##################################

##client side:

use IO::Socket;

$sock = new IO::Socket::INET(PeerAddr => 'xxx.xxx.xxx.xxx',
                   PeerPort => someport,
                   Proto    => 'tcp'
                  );
die "Socket could not be created. Reason: $!\n" unless $sock;

print $sock "Msg $_: Hello server, here is my message\n";
$sock->flush();
while(<$sock>) { print; last if /I finished/; }
close($sock);

0
 
jmcgOwnerCommented:
If you want it to be possible for more than one client to be running simultaneously, you'd probably want to do a fork after the accept in the server. There are a bunch of other things you'd have to watch out for, though, so we won't go down that road unless you need to.
0
 
jvieiraAuthor Commented:
I sat down for a while to think about how to approach.  Basically what happens is the client calls the server and asks it to run and application (written in perl), the server replies back with updates on the app and when it finally completes it tells the client and closes the socket connection.  Right now the user only has two apps to choose from and he/she would rarely have to run one after the other.  If he has two than I will just recreate the socket connection.  I'm going to stick with Trevize response and award him the points.  Jmcs thank you for your input as well.  Eventully when there are more apps to choose and more users connecting I'll have to rewrite the code.  But I'll cross that bridge when I get to it.

Thanks again,

Joe
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now