Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:54:08+00:00 2026-06-15T08:54:08+00:00

I am trying to create a socket client in Perl. The server side is

  • 0

I am trying to create a socket client in Perl. The server side is a C program running on the local host.

I have to send a hash over the socket. Here is the piece of code I am using.

sub applyGlobalConfig {
    my ($globalConfig, $ignoreData) = @_;

    my $socket = IO::Socket::INET->new(PeerAddr => $PEER_HOST,
                                       PeerPort => $PEER_PORT,
                                       Proto    => "tcp",
                                       Type     => SOCK_STREAM)
          or die "Couldn't connect to $PEER_HOST:$PEER_PORT : $@\n";

    my $reconfigResult;

    print $socket "$113\n";

    close($socket);

    unless ($reconfigResult) {
        return 0;
    }

    return ERR_NULL;
}

Now, the question I have is, the $globalConfig will contain a hash reference, and I want to send this over a socket. I am unable figure out. I googled and found some reference to Dumper but couldn’t understand much. How do I send the hash over?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:54:09+00:00Added an answer on June 15, 2026 at 8:54 am

    I’d prefer to use Storable module for this. Example:

    Receive-side:

    use strict;
    use IO::Socket::INET;
    use Storable;
    use Data::Dumper;
    
    my   $sock = IO::Socket::INET->new(Listen    => 5, LocalAddr => 'host',
                                     LocalPort => 9000,  Proto     => 'tcp');
    while( my $s = $sock->accept ) {
        my $struct = Storable::fd_retrieve($s);
        print Dumper($struct);
    }
    

    Send-side:

    use strict;
    use IO::Socket::INET;
    use Storable;
    
    my   $sock = IO::Socket::INET->new(PeerAddr => 'host',  PeerPort => 9000,
                     Type     => SOCK_STREAM, Proto     => 'tcp') || die "Fail: $!";
    my $struct = {
        a => 1,
        b => [2,3,4]
    };
    Storable::nstore_fd($struct, $sock);
    

    Tested by sending from i386 Linux to amd64 FreeBSD.

    Also you can use Data::Dumper to make string from hash and then send over network but its dirty and buggy method.

    UPD:

    But, I am struggling how to convert the values in the hash on perl side to a string separated by space.

    Try to use join/map combination:

    my $serialized = join("\n", map { "$_ ".$struct->{$_} } keys %$struct)."\n";
    

    Probably on C side its easier to use null-terminated string:

    my $keyvalue_count = scalar keys(%$struct);
    my $serialized = join("\0", map { "$_\0".$struct->{$_} } keys %$struct)."\0";
    

    In this simple case i’d prefer to use last variant since its native to C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a client-server program that send and receive a string (its
I'm trying to implement a server-client socket program in Java that can support multiple
I am trying to create an SSL Socket Server/Client between .NET and Java. In
I'm trying to simulate a socket client using simple perl program. socket client: #!/usr/bin/perl
I am trying to create a program that will get data directly from socket
I'm trying to get a simple winsock program working, so I create my socket
I've been trying to receive data on a server socket from a client with
I am trying to implement a TCP echo server and client program in Ubuntu
I am trying out with a TCP server/client program, and wonder why at server
I am trying to create a thread to simply send the text to client.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.