#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Socket connection, pack unpack??


    Hello all,

    I have a server and client program which eventually I will use the server program to insert values into a mysql DB. When I send the data across, only the first character of each string is displayed and I can't work out a solution.

    Client.pl
    Code:
    use IO::Socket::INET;
    
    # auto-flush on socket
    $| = 1;
    
    # create a connecting socket
    my $socket = new IO::Socket::INET (
        PeerHost => '192.168.10.109',
        PeerPort => '7777',
        Proto => 'tcp',
    );
    die "cannot connect to the server $!\n" unless $socket;
    print "connected to the server\n";
    
    # data to send to a server
    my $deviceid  = '2';
    my $latitude  = '52.12333';
    my $longitude = '-0.48623';
    
    #@data_array = ($deviceid,$latitude,$longitude);
    #my $data = $socket->send(@data_array);
    
    my $data_pack = pack("sai",$deviceid,$latitude,$longitude);
    my $data = $socket->send($data_pack);
    
    print "sent data of length $size\n";
    
    # notify server that request has been sent
    shutdown($socket, 1);
    
    # receive a response of up to 1024 characters from server
    my $response = "";
    $socket->recv($response, 1024);
    print "received response: $response\n";
    
    $socket->close();
    server.pl
    Code:
    use IO::Socket::INET;
    use lib './lib';
    
    use DBI;
    use DBD::MySQL;
    
    # auto-flush on socket
        $| = 1;
    
    # creating a listening socket
        my $socket = new IO::Socket::INET (
            LocalHost => '0.0.0.0',
            LocalPort => '7777',
            Proto => 'tcp',
            Listen => 5,
            Reuse => 1
            );
            die "cannot create socket $!\n" unless $socket;
            print "server waiting for client connection on port 7777\n";
    
    while(1)
    {
        my $client_socket = $socket->accept();        # waiting for a new client connection
    
    # get information about a newly connected client
        my $client_address = $client_socket->peerhost();
        my $client_port = $client_socket->peerport();
        print "connection from $client_address:$client_port\n";
    
    # read up to 1024 characters from the connected client
        my $data = "";
        $client_socket->recv($data, 1024);
    @array = unpack ("sai","$data");
    print "Array $array[0]\n";
    print "Array $array[1]\n";
    print "Array $array[2]\n";
    
    #    print "received data: $data\n";
    
    # write response data to the connected client
        $data = "ok";
        $client_socket->send($data);
    
        shutdown($client_socket, 1);      # notify client that response has been sent
    }
    
    $socket->close();
    I am a novice programmer hence (I think) while I'm struggling.

    Any help is much appreciated.

    Chris
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    'a' can only be used to unpack fixed-length strings. You need to either
    • determine the maximum length of the string and use a fixed-length string such as 'a10'
    • use 'Z' which ends the string with a null byte
    • prefix the string with a length like 'n/a'


    I strongly recommend reading perldoc perlpacktut as packing/unpacking can involve get tricky.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Thumbs up


    Originally Posted by OmegaZero
    'a' can only be used to unpack fixed-length strings. You need to either
    • determine the maximum length of the string and use a fixed-length string such as 'a10'
    • use 'Z' which ends the string with a null byte
    • prefix the string with a length like 'n/a'


    I strongly recommend reading url; packing/unpacking can involve get tricky.
    Thanks for the tip, I'll have a look at your suggestions.

IMN logo majestic logo threadwatch logo seochat tools logo