#!/usr/bin/perl use strict; use IO::Socket; use IO::Select; $| = 1; my $host = 'localhost'; my $port = '3000'; my $data; my $socket = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', Type => SOCK_STREAM, Blocking => 1, Timeout => 10, ) or die "Couldn't connect to $host:$port : $!"; my $sel = IO::Select->new($socket) or die "IO::Select error $!"; my $request = "abcdefghijklmpnoq"; my $request_length = length($request); print "Request:\n$request\n"; print "length(request) = $request_length\n"; # 17 my $x = htonl($request_length); print "x = $x\n"; # 285212672 printf("x = %x\n", $x); # 11000000 if ($sel->can_write(10)) { $socket->autoflush(1); # write request length # THIS IS WHERE I THINK SOMETHING IS GOING WRONG - THE SERVER GETS THE WRONG LENGTH HERE!!! $socket->send( htonl($request_length) ) or die "3a Couldn't write to socket: $!"; # write request $socket->send( $request ) or die "3b Couldn't write to socket: $!"; if ($sel->can_read(10)) { # read response length $socket->recv($data, 4) or die "5 Couldn't read data from server: $!"; my $reponse_length = ntohl($data); if ($sel->can_read(10)) { # read response $socket->recv($data, $reponse_length) or die "6 Couldn't read data from server: $!"; print "Response:\n$data\n"; } else { print "can't read 2: $@ : $!"; } } else { print "can't read 1: $@ : $!"; } } else { print "can't write: $@ : $!"; } $socket->close(); sub htonl { my $input = shift; my $output = unpack('N*',pack('L*',$input)); return $output; } sub ntohl { my $input = shift; my $output = unpack('L*', pack('N*', $input)); return $output; }