Solved

Display output from server for every command sent by client via Socket

Posted on 2010-09-02
6
351 Views
Last Modified: 2013-11-05
There are 4 commands that my client needs to send to server using socket named TCP_SOCK in sequence and my client needs to display the output from server for every command sent to it. How can I achieve this goal? Every command is ended by semi-column ";" to ask server to execute it.
I use "print scalar <TCP_SOCK>;" but the client cannot display all the text from the sever, it misses some texts. When I try to use:

while ($line = <>)
{
      print $line;
}

only the first command is executed, not continue with the other commands.
select TCP_SOCK; $| = 1; select STDOUT;

my @command;

$command[0] = qq/Do the first one;/;

$command[1] = qq/Do the second one;/;

$command[2] = qq/Do the third one;/;

$command[3] = qq/Do the fourth one;/;

print TCP_SOCK $command[0];

print TCP_SOCK $command[1];

print TCP_SOCK $command[2];

print TCP_SOCK $command[3];

close TCP_SOCK;

Open in new window

0
Comment
Question by:tballah
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:gremwell
ID: 33589754
Try the attached code.

#!/usr/bin/perl -w

use strict;
use IO::Socket::INET;

sub transaction($$) {
	my $socket = shift;
	my $command = shift;

	$socket->print($command . ";");
	my $res = <$socket>;
	chomp $res;

	return $res;
}

# ----

my $socket = new IO::Socket::INET(PeerAddr => "localhost:10000") || die;
$socket->autoflush(1);

# ----

my $res1 = transaction($socket, qq/Do the first one/);
printf "res1: >%s<\n", $res1;

# ----

my $res2 = transaction($socket, qq/Do the second one/);
printf "res2: >%s<\n", $res2;

# ----
$socket->close();

Open in new window

0
 
LVL 1

Author Comment

by:tballah
ID: 33590085
The response from the server contains many lines and I follow the solution from the sample code you provide to me, I can see only the first line from the output, there are many other lines missing. I can see all the lines when I use while loop but it blocks me from executing other commands.
0
 
LVL 3

Expert Comment

by:gremwell
ID: 33590114
If the response is multiline, how do you know which line is the last line of the response?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:tballah
ID: 33590378
The response for the first command looks like:

+++    SGSN                2010-09-02 22:10:27
O&M    #852
%%LGI:OP="ballah",PWD="*****",SER="MML Backup";%%
RETCODE = 0  Execution succeeded


---    END

The last line is "---    END". From the sample of your code provided I can see only the first line "+++    SGSN                2010-09-02 22:10:27". If I do loop with while I can see all the lines responded from the server but it could not exit loop and it loops forever and I cannot execute other commands. Please see my code.
select TCP_SOCK; $| = 1; select STDOUT;

my @sgsn_command;

$sgsn_command[0] = qq/LGI:OP="$user_name",PWD="$password",SER="MML Backup";/;

$sgsn_command[1] = qq/SAVE CFG:;/;

$sgsn_command[2] = qq/SET FTPENABLE:;/;

$sgsn_command[3] = qq/LGO:;/;

exec_mml($sgsn_command[0]);

exec_mml($sgsn_command[1]);

exec_mml($sgsn_command[2]);

exec_mml($sgsn_command[3]);

close TCP_SOCK;

}



sub exec_mml

{

  print TCP_SOCK shift;

  while (<TCP_SOCK>)

  {

    print;

  }

}

Open in new window

0
 
LVL 3

Accepted Solution

by:
gremwell earned 500 total points
ID: 33590438
Then you should add a stop condition to your loop, such as
     last if /^---    END$/;
0
 
LVL 1

Author Closing Comment

by:tballah
ID: 33590644
sub exec_mml
{
  print TCP_SOCK shift;
  while ()
  {
    print;
    last if /END/;
  }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Web Database 5 110
perl to mysql 5 130
use perl to insert into MySQL database 9 128
add a syntax to a csv file 8 98
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now