Home > Archive > PERL Miscellaneous > March 2006 > How to receive data from a socket
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to receive data from a socket
|
|
|
| Hi
Im new to perl socket programming.
I have a server program on a certain machine which simply cat's a file
when it receives a connection to port 10000 . This is done via inetd on
a Solaris system as follows:
Line in /etc/inetd.conf:
# Test program
test stream tcp nowait root /usr/bin/test1
Line from /etc/services:
# Test program on port 10000
test 10001/tcp
The script /usr/bin/test1 simply contains a single line to cat a
certain text file, so if you telnet to port 10000 you simply get the
contents of the file.
I want to write a very simple perl script to connect to this server and
receive the data into $data
Problem is that the contents of the file can be very large (>300K) and
i dont seem to be getting all the data into $data
Im doing something like this:
our $sock = new IO::Socket::INET ( PeerAddr => $MESSAGE_SERVER,
PeerPort => $TARGET_PORT, Proto => 'tcp',);
And then receiving the data like this:
recv($sock,$data,3000,0);
$data doesnt contain everything though.
Like I say, i've never really done any socket programming before, so if
if someone knows the correct way of receiving data then that woulbe be
appreciated.
| |
| Jim Gibson 2006-03-29, 7:00 pm |
| In article <1143630352.296348.28210@i40g2000cwc.googlegroups.com>, Suk
<sukesh@zoom.co.uk> wrote:
> Hi
>
> Im new to perl socket programming.
>
> I have a server program on a certain machine which simply cat's a file
> when it receives a connection to port 10000 . This is done via inetd on
> a Solaris system as follows:
>
> Line in /etc/inetd.conf:
>
> # Test program
> test stream tcp nowait root /usr/bin/test1
>
> Line from /etc/services:
>
> # Test program on port 10000
> test 10001/tcp
>
> The script /usr/bin/test1 simply contains a single line to cat a
> certain text file, so if you telnet to port 10000 you simply get the
> contents of the file.
>
> I want to write a very simple perl script to connect to this server and
> receive the data into $data
> Problem is that the contents of the file can be very large (>300K) and
> i dont seem to be getting all the data into $data
>
> Im doing something like this:
>
> our $sock = new IO::Socket::INET ( PeerAddr => $MESSAGE_SERVER,
> PeerPort => $TARGET_PORT, Proto => 'tcp',);
>
> And then receiving the data like this:
>
> recv($sock,$data,3000,0);
>
> $data doesnt contain everything though.
>
>
> Like I say, i've never really done any socket programming before, so if
> if someone knows the correct way of receiving data then that woulbe be
> appreciated.
>
Look at 'perldoc perlipc' and search for 'Clients with IO::Socket'. The
recv function is more for UDP sockets. You are asking for only the
first 3000 bytes. From the perldoc example, adapted to your
requirements (untested):
my $remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $MESSAGE_SERVER,
PeerPort => $TARGET_PORT,
) or die "cannot connect to $TARGET_PORT at $MESSAGE_SERVER";
my $data;
while ( <$remote> ) {
$data .= $_;
}
| |
|
| Thanks very much, that worked perfectly :-)
|
|
|
|
|