Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Newline Character and Socket Comms

by Ducati (Beadle)
on May 04, 2001 at 19:00 UTC ( #77971=perlquestion: print w/replies, xml ) Need Help??

Ducati has asked for the wisdom of the Perl Monks concerning the following question:

I asked this question in the chatterbox and I couldn't get an answer ... so on the advice of a few monks I'll ask it here.

I found ... but lost ... an answer to a question on this site that teaches one how to set up a client/server comm.

This is the code that I grabbed:

#!/bin/Perl #This is the server side use strict; use IO::Socket; my $sock = new IO::Socket::INET (LocalPort => 1200, Proto => 'tcp', Listen => 5, Reuse => 1); die "Can't connect: $!\n" unless $sock; while (my $new_sock = $sock->accept()) { my $msg = <$new_sock>; print "client said '$msg'\n"; } close $sock; #Client Side use strict; use IO::Socket; my $sock = new IO::Socket::INET (PeerAddr => '127.0.0.1', PeerPort => 1200, Proto => 'tcp'); die "Can't create: $!\n" unless $sock; print $sock "send money!"; close $sock;

Aside from a few mis-placed commas ... which I fixed in the code above ... all worked well.

I extened the code to include:

for(my $i=0; $i <= 10; $i++) { print $sock "$i\n"; } print $sock "**** End of transmission ****";

When I run the script I get this:

'0 '

When I take out the /n I get: '0123456789****End Of Transmission****'

My Question: Why does the newline stop the data stream and what can I do to fix it??

Thanks in advance ....

Ducati

============================================

"We rock the body to rock the party ... until the party rocks the body"

De La Soul

Replies are listed 'Best First'.
Re: (Zigster) Newline Character and Socket Comms
by zigster (Hermit) on May 04, 2001 at 19:13 UTC
    I presume you mean you changed the server code to include:
    for(my $i=0; $i <= 10; $i++) { print $new_sock "$i\n"; }
    Within the while loop instead of the
    my $msg = <$new_sock>; print "client said '$msg'\n";
    I have tried this and all seems to work fine. Can you confirm a few more details regarding what you are doing and on what platform.
    Taa Update As tilly pointed out, in CB, it is a good idea to get out of the habit of using C style for loops use for my (1..10) {stuff}. Tiss neater and nice and clear. Not directly relevent but very good advice all the same. Cheers Tilly
    --

    Zigster

      I put the for loop in on the client side ... the loop sends the data to the server ... the while loop is on the server side and it loops waiting for more data.

      I was expecting:

      0 1 2 ...

      But I get:

      '0 '

      ============================================

      "We rock the body to rock the party ... until the party rocks the body"

      De La Soul

Re: Newline Character and Socket Comms
by chorg (Monk) on May 04, 2001 at 19:40 UTC
    You're using the readline operator <> to read your socket. This operator will, of course read _lines_ of input. The default line separator is \n. Your server code will read bytes until it sees a line end, just like it would if you were reading from a file. When you printed the "\n" to the socket, the server thought that the socket reading was over, and continued on. If you want to read bytes over newlines, you'll need to do a

    local $/= (anything but\n)

    inside your server to make sure that the server thinks that the end of the line is what you say it is whilst it it reading from the socket...
    _______________________________________________
    "Intelligence is a tool used achieve goals, however goals are not always chosen wisely..."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://77971]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2022-05-28 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?