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
jvieiraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.