CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Client/Server Programming: Problem with displaying Sum

 



zak100
User

Jul 24, 2014, 11:07 AM

Post #1 of 7 (630 views)
Client/Server Programming: Problem with displaying Sum Can't Post

Hi,
I have written two programs: client (cli3.pl) & Server (ser3.pl). They are communicating. Server is in a while loop. Server gets two values from 1st client and it should display the sum. However, it displays the sum when the 2nd client is executed and server receives the first input of the 2nd client.
Server code is:

Code
#!C:/Perl64/bin/perl.exe 
use IO::Socket::INET;
$server=IO::Socket::INET->new(LocalPort=>'5465',Proto=>'UDP');
while(1) {
$server->recv($num1,1024);
print("\nnum1=", $num1);
$server->recv($num2,1024);
print("\nnum2=", $num2);
print ("\nSum=" , $num1 + $num2);
}


Client code is:

Code
 
#!C:/Perl64/bin/perl.exe
use IO::Socket::INET;

$client=IO::Socket::INET->new(PeerPort=>'5465',Proto=>'UDP', PeerAddr=>'127.0.0.1');
print("Enter the first number\n");
$Num1=<STDIN>;
$client->send($Num1);
print("Enter the second number\n");
$Num1=<STDIN>;
$client->send($Num1);


Server Screen Output:

Code
D:\Zulfi\PERL MAIN FOLDER\Client Server Programming\Lab3>perl ser3.pl 

num1=3

num2=4

Sum=7
num1=5

num2=6


Client screen output (Note two clients are invoked)

Code
D:\Zulfi\PERL MAIN FOLDER\Client Server Programming\Lab3>perl cli3.pl 
Enter the first number
3
Enter the second number
4

D:\Zulfi\PERL MAIN FOLDER\Client Server Programming\Lab3>perl cli3.pl
Enter the first number
5
Enter the second number
6

D:\Zulfi\PERL MAIN FOLDER\Client Server Programming\Lab3>


Somebody please help me.

Zulfi.


Laurent_R
Veteran / Moderator

Jul 25, 2014, 9:50 AM

Post #2 of 7 (613 views)
Re: [zak100] Client/Server Programming: Problem with displaying Sum [In reply to] Can't Post

Hi,

try to change the last line of your server code to this:


Code
print "\nSum=" , $num1 + $num2, "\n";


The printing of a line to the screen is usually buffered until a line return is entered. I think that is probably the source of your problem. I would recommend that you put the new line characters at the end, rather than beginning, of your other print strings.


zak100
User

Jul 25, 2014, 10:32 AM

Post #3 of 7 (610 views)
Re: [Laurent_R] Client/Server Programming: Problem with displaying Sum [In reply to] Can't Post

Hi,
Thanks for your help. I tried but its not working (showing me the same output). At the following site i found solution to my problem but i cant figure out the character used in the code:
http://desk.stinkpot.org:8080/tricks/index.php/2006/02/flush-perls-print-buffer/
They have used the code:

Code
$|++;


Plz guide me what is the scalar variable (being incremented)?

Zulfi.


(This post was edited by zak100 on Jul 25, 2014, 10:33 AM)


Laurent_R
Veteran / Moderator

Jul 25, 2014, 10:47 AM

Post #4 of 7 (607 views)
Re: [zak100] Client/Server Programming: Problem with displaying Sum [In reply to] Can't Post

$| if the output autoflush variable. Its default value is 0 (i.e. false), meaning that lines printed to a file are buffered. If you increment it, it becomes 1 (i.e. true), meaning that each line printed or written to the selected filehandle will be flushed immediately (not buffered).

But this is usually not necessary when printing to the screen (STDOUT attached to the terminal), I am surprised that it should make a difference for you, since you appear to print to the screen.

Detailed information on the subject can be found here:

http://perl.plover.com/FAQs/Buffering.html

(The author, Mark-Jason Dominus, is a well-known Perl expert and is a trustworthy source.)


zak100
User

Jul 26, 2014, 8:19 AM

Post #5 of 7 (583 views)
Re: [Laurent_R] Client/Server Programming: Problem with displaying Sum [In reply to] Can't Post


Code
$|


Please tell me the character following '$' sign. Is it 'l'?

Zulfi.


FishMonger
Veteran / Moderator

Jul 26, 2014, 8:29 AM

Post #6 of 7 (582 views)
Re: [zak100] Client/Server Programming: Problem with displaying Sum [In reply to] Can't Post

| is the "pipe" symbol.


zak100
User

Jul 26, 2014, 10:31 AM

Post #7 of 7 (574 views)
Re: [FishMonger] Client/Server Programming: Problem with displaying Sum [In reply to] Can't Post

Hi,
Thanks. Its working fine.
The server code is now:

Code
#!C:/Perl64/bin/perl.exe 
use IO::Socket::INET;
$server=IO::Socket::INET->new(LocalPort=>'5465',Proto=>'UDP');
while(1) {
$server->recv($num1,1024);
print("\nnum1=", $num1);
$server->recv($num2,1024);
print("\nnum2=", $num2);
print ("\nSum=" , $num1 + $num2 + "\n");
$|++;
}


Zulfi.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives