pugnator
pugnator

Reputation: 715

perl tcp client does not display incoming data

I'm completely new to net programming and can't solve this issue

I have a server (some kind of LPT port to TCP/IP proxy) that automatically tries to establish connection at random interval. I need to listen on specific port and communicate with server. Thats the client. It works fine with perl tcp server.. But in this case it waits.... nothing happens

#!/usr/bin/perl -w
use IO::Socket;
my $sock = new IO::Socket::INET(
    LocalHost => '192.168.1.1',
    LocalPort => '7000',
    Proto     => 'tcp',
    Listen    => 1,
    Reuse     => 1,
);
die "Could not create socket: $!\n" unless $sock;
my $new_sock = $sock->accept();
while (<$new_sock>) {
    print $_;
}
close($sock);

It works OK with my simple tcp server, but with this 'black-box' - nope On client machine there are two connection:

tcp        0      0 192.168.1.1:7000      0.0.0.0:*               LISTEN     
tcp        0      0 192.168.1.1:7000      192.168.1.2:33822     ESTABLISHED

But if I try with

nc -l 192.168.1.1 7000

It works like a charm, data is flowing. And only one connection is present (like the second one)

tcpdump fragment of one symbol transmission captured on client. Seems to be OK

21:42:00.242172 IP (tos 0x0, ttl 64, id 30448, offset 0, flags [DF], proto TCP (6), length 53)
192.168.1.2.33837 > 192.168.1.1.7000: Flags [P.], cksum 0x5646 (correct), seq 3592562130:3592562131, ack 1351632513, win 92, options [nop,nop,TS val 140364552 ecr 92554083], length 1

I don't know what I'm doing wrong... More complicated examples are don't working for me as I don't know how to debug them...

Upvotes: 1

Views: 1384

Answers (1)

ikegami
ikegami

Reputation: 385847

<$new_sock>, short for readline($new_sock), only returns after a complete line is received, which is to say when newline is received.

sysread will return as soon as data is available, so you could use that.

my $rv = sysread($new_sock, my $buf, 64*1024);

Alternatively, since the lines you receive end with carriage return, you could communicate this to Perl.

$/ = "\r";

Upvotes: 2

Related Questions