Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

TCP server sends just part of my packet

by microcx (Initiate)
on Apr 16, 2013 at 10:09 UTC ( [id://1028865]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, monks! Another guy suffering from debugging TCP server-client pair. Please shed some light!

I'm using

     print $socketHandle "$longHexString"

it works fine, but if I use:

     pack("H*",$longHexString)

and send those packed raw bytes, my poor server just sends 483 bytes out of 796 bytes to my client.

I've tried the autoflush trigger and use EOT as $\, but they just don't work. Here are code for both the server and client, thanks in advance :-)

#!/usr/bin/perl -w use strict; use warnings; use IO::Socket; my $server_port=29000; my $server = IO::Socket::INET->new(LocalPort =>$server_port, Type=>SOCK_STREAM, Reuse=>1, Listen=>10 )# or SOMAXCONN or die "Couldn't be a tcp server on port $server_port : $@\n"; while (my $client = $server->accept()) { # $client is the new connection $client-> autoflush(1); print "$client\n"; my $dataPrint=<$client>; print "$dataPrint"; my $lig="0100031cc0000110010000160000000000000000000001074000004530303 +9362d736573736d67722e73746172656e742d67792d3032313b343736353239383230 +3b3533363832313038383b35313232333363382d36303032000000000001084000002 +3303039362d736573736d67722e73746172656e742d67792d30323100000001284000 +001b73746172656e746e6574776f726b732e636f6d000000011b4000001363616d696 +16e742e636f6d00000001024000000c00000004000001cd4000001633323235314033 +6770702e6f72670000000001a04000000c000000010000019f4000000c00000000000 +000014000000c766f6964000001164000000c5109e66a000000374000000cd4ccb248 +000001bb40000028000001c24000000c00000000000001bc400000133333373631333 +33739323300000001bb4000002c000001c24000000c00000001000001bc4000001732 +303832303130303134323032323100000001c74000000c00000001000001ca4000002 +c000001cb4000000c00000000000001cc400000183335343937323035303032323338 +303100000369c0000180000028af0000036ac0000174000028af00000002c00000100 +00028af1fc1b44700000003c0000010000028af00000000000004cbc0000012000028 +af00010a260f0b000000000005c0000025000028af39392d313339323146373339364 +53845383734383235383738000000000004ccc0000012000028af000150d6f4010000 +0000034fc0000012000028af000150d6f2c0000000000008c0000011000028af32303 +832300000000000000ac000000d000028af350000000000001ec000001b000028af6d +6d73626f75796774656c2e636f6d000000000cc000000d000028af300000000000000 +dc0000010000028af3038303000000012c0000011000028af32303832300000000000 +0017c000000e000028af40200000000003ec4000002c6d6d73626f75796774656c5f7 +2756c65626173655f464e425f4d6f6e74686c795f34474200000016c0000014000028 +af0102f8025097b00e00000015c000000d000028af01000000000004dfc0000010000 +028af00000000"; print "length ago:".length($lig)."\n"; my $lig1=pack("H*",$lig); print "length now:".length($lig1)."\n"; #my $lig2=unpack("H*",$lig1); #print "length now:".length($lig2)."\n"; #select ($client); #$\=chr(4); print $client $lig1."\n"; #print $client $lig2."\n"; #$client->syswrite($lig); #$client->flush(); } close($server);

client:

#!/usr/bin/perl -w use IO::Socket; use strict; my $remote_host='bt1svmi6'; my$remote_port=29000; my $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_port, Proto=> "tcp", Type=> SOCK_STREAM) or die "Couldn't connect to $remote_host:$remote_port : $@\n"; #$wantToSpeak=0; # ... do something with the socket my $lig="Need for Diameter!\n"; print $socket $lig; my $answer = <$socket>; # and terminate the connection when we're done print "I have the response\n"; close($socket); chomp ($answer); print $answer; $answer=unpack("H*",$answer);

Replies are listed 'Best First'.
Re: TCP server sends just part of my packet
by hdb (Monsignor) on Apr 16, 2013 at 12:05 UTC

    Your client is not reading all the server has to say! Try:

    $/ = undef; my $answer = <$socket>;

    or do

    while( $answer = <$socket> ) {

    There must be an end of line character in your message at byte 482.

      hdb:

      It's just as you said:

      470: 04 cb c0 00 00 12 00 00 28 af
      480: 00 01 0a 26 0f 0b 00 00 00 00
      490: 00 05 c0 00 00 25 00 00 28 af
      500: 39 39 2d 31 33 39 32 31 46 37

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Thx, now I understand it better, 0x0a is the sign. When it appears in data stream, it confuses the interpretation.

      Bravo!! I've taken your first piece of code and it's now working like a charm. Great job @hdb. In fact, the dumpfile I got (using tcpdump) from server's interface showed that only 482 bytes were sent and that misled me into thinking that the client worked properly and it was the server to be blame.

      Line 0220: (|last 5 bytes I got|28 af 00 01 0a) 26 0f 0b

      To make it clear, starting from 0x26, the packet is lost, so you believe that my client considers maybe this byte is the end of line? By the way, what represents the '\n' in packet transmission?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-24 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found