?
Solved

IO::Socket

Posted on 2003-03-29
5
Medium Priority
?
254 Views
Last Modified: 2010-03-05
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
Comment
Question by:jvieira
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
Trevize earned 2000 total points
ID: 8231619
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
 

Author Comment

by:jvieira
ID: 8231727
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8233047
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8233052
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
 

Author Comment

by:jvieira
ID: 8235243
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question