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 610141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:38:35+00:00 2026-05-13T17:38:35+00:00

I am working on a mulithreaded TCP server. In the main thread, I listen

  • 0

I am working on a mulithreaded TCP server. In the main thread, I listen on a socket and create a new thread for new incoming connections. I want to save all incoming connections in a hash so that I can access them from yet another thread.

From the monitor thread, I can not read any newly added connections. It seems a new clients hash is created when creating the monitor thread.

How do i keep list of all sockets and loop them from my monitor thread?

Current code:

#!/usr/bin/perl
use strict;
use IO::Socket;
use threads;
use Thread::Queue;

# init
my $clients = {};
my $queue = Thread::Queue->new;

# thread that monitors
threads->create("monitor");

# create the listen socket
my $listenSocket = IO::Socket::INET->new(LocalPort  => 12345,
                                      Listen   => 10,
                                      Proto   => 'tcp',
                                      Reuse   => 1);

# make sure we are bound to the port
die "Cant't create a listening socket: $@" unless $listenSocket;

print "Server ready. Waiting for connections on 34567 ... \n";

# wait for connections at the accept call
while (my $connection = $listenSocket->accept) {
    # set client socket to non blocking
    my $nonblocking = 1;
    ioctl($connection, 0x8004667e, \\$nonblocking);

    # autoflush
    $connection->autoflush(1);

    # debug
    print "Accepted new connection\n";

    # add to list
    $clients->{time()} = $connection;

    # start new thread and listen on the socket
    threads->create("readData", $connection);
}

sub readData {
     # socket parameter
     my ($client) = @_;

     # read client
     while (<$client>) {
      # remove newline
      chomp $_;

  # add to queue
      $queue->enqueue($_);
     }

     close $client;
}

sub monitor {
    # endless loop
    while (1) {

        # loop while there is something in the queue
        while ($queue->pending) {

            # get data from a queue
            my $data = $queue->dequeue;

            # loop all sockets
            while ( my ($key, $value) = each(%$clients) ) {

               # send to socket
               print $value "$data\n";

            }
        }

        # wait 0,25 seconds
        select(undef, undef, undef, 0.25);
    }
}

close $listenSocket;
  • 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-05-13T17:38:35+00:00Added an answer on May 13, 2026 at 5:38 pm

    You need to share $clients via share from threads::shared:

    my $clients = &share({});
    

    The old-fashioned syntax is due to a documented issue with Perl’s prototypes. If you have at least Perl 5.8.9, use the nicer

    my $clients = shared_clone({});
    

    instead.

    You also want to protect $clients with a lock, e.g.,

    my $clients_lock : shared;
    {
      lock $clients_lock;
      $clients->{time()} = fileno $connection;
    }
    

    Finally, because IO::Socket::INET instances are Perl typeglobs, you can’t share them, so instead add their socket descriptors (from fileno) to $clients and then fdopen the socket when necessary with

    open my $fh, ">&=", $sockdesc or warn ...
    

    The program below repeats inbound data to the other connected sockets:

    #!/usr/bin/perl
    
    use strict;
    use IO::Socket;
    use threads;
    use threads::shared;
    use Thread::Queue;
    
    # init
    my $clients = &share({});
    my $clients_lock : shared;
    
    my $queue = Thread::Queue->new;
    
    # thread that monitors
    threads->create("monitor");
    
    # create the listen socket
    my $port = 12345;
    my $listenSocket = IO::Socket::INET->new(
      LocalPort  => $port,
      Listen     => 10,
      Proto      => 'tcp',
      Reuse      => 1
    );
    
    # make sure we are bound to the port
    die "Can't create a listening socket: $@" unless $listenSocket;
    
    print "Server ready. Waiting for connections on $port ... \n";
    
    # wait for connections at the accept call
    while (my $connection = $listenSocket->accept) {
      # set client socket to non blocking
      my $nonblocking = 1;
      ioctl($connection, 0x8004667e, \\$nonblocking);
    
      # autoflush
      $connection->autoflush(1);
    
      # debug
      print "Accepted new connection\n";
    
      # add to list
      {
        lock $clients_lock;
        $clients->{time()} = fileno $connection;
      }
    
      # start new thread and listen on the socket
      threads->create("readData", $connection);
    }
    
    sub readData {
      # socket parameter
      my ($client) = @_;
    
      # read client
      while (<$client>) {
        chomp;
        $queue->enqueue($_);
      }
    
      close $client;
    }
    
    sub monitor {
      # endless loop
      while (1) {
        # loop while there is something in the queue
        while ($queue->pending) {
          # get data from a queue
          my $data = $queue->dequeue;
    
          # loop all sockets
          {
            lock $clients_lock;
            while ( my ($key, $value) = each(%$clients) ) {
              # send to socket
              if (open my $fh, ">&=", $value) {
                print $fh "$data\n";
              }
              else {
                warn "$0: fdopen $value: $!";
              }
            }
          }
        }
    
        # wait 0,25 seconds
        select(undef, undef, undef, 0.25);
      }
    }
    
    close $listenSocket;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a multithreaded application, and I want to debug it using GDB.
I am working on a multithreaded program. It's able to close all the threads
I'm working on this example with C threaded server and java client. This is
I am working on a client-server project and need to implement a logic where
I'm working on a multithreaded server in c++ using boost-asio. Currently a design problem
I am new to multithreaded application. I have few doubts before starting working on
In my project which is kind of a tcp server, i need to keep
I have working on multithreaded application I am passing IMAGETHREADINFO structure in thread here
What is the experience of working with OpenOffice in server mode? I know OpenOffice
I have very little experience working with sockets and multithreaded programming so to learn

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.