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

  • Home
  • SEARCH
  • 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 8763601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:48:59+00:00 2026-06-13T15:48:59+00:00

Im trying to emulate a TCP Server on the same PC where the app

  • 0

Im trying to emulate a TCP Server on the same PC where the app is running.
I dont know if it can be done in Perl because im not very experienced.

With the code bellow the first reply is working but i dont know how to implement the second.

#!/usr/bin/perl -w

use IO::Socket::INET;
use strict;


my $socket = IO::Socket::INET->new('LocalPort' => '3000',
                   'Proto' => 'tcp',
                   'Listen' => SOMAXCONN)
    or die "Can't create socket ($!)\n";
print "Server listening\n";
while (my $client = $socket->accept) {
    my $name = gethostbyaddr($client->peeraddr, AF_INET);
    my $port = $client->peerport;
    while (<$client>) {
    print "$_";
    print $client "RESPONSE1";
    }
    close $client
    or die "Can't close ($!)\n";
}
die "Can't accept socket ($!)\n";

EDIT: Thank you guys for the imput, i ended up with php done it and its working, yay!

  • 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-13T15:49:00+00:00Added an answer on June 13, 2026 at 3:49 pm

    Use Net::Server for the connection, and a variable in the sub to keep the current state ($state in this code); something like this:

    package MyServer;
    use base qw/Net::Server/;
    use strict;
    use warnings;
    
    sub process_request {
      my $self = shift;
      my $state = 0;
      while (<STDIN>) {
        s/\r?\n$//; # like chomp but for crlf too
        if ($state == 0 and $_ eq 'data1') {
          print "> okay1\n";
          $state++;
        } elsif ($state == 1 and $_ eq 'data2') {
          print "> okay2\n";
          $state++;
        } else {
          last if $state == 2;
          $state = 0;
        }
      }
    }
    
    my $port = shift || 3000;
    MyServer->run( port => $port );
    

    The example in the Net::Server POD suggests using an alarm to timeout connections, which might be appropriate here. The code above does the following:

    $ nc localhost 3000
    data1
    > okay1
    data2
    > okay2
    data3
    $
    

    And if you need to move to a forking / preforking / non-blocking / co-routine driven system, there’s a Net::Server personality for that.

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

Sidebar

Related Questions

I have client and server programs which now communicate via TCP. I'm trying out
So I am trying to emulate google app's status page: http://www.google.com/appsstatus#hl=en but for backups
I'm trying to emulate the twitter model in my Rails 2.3.8 app (ruby 1.8.7)
I'm trying to emulate the animation seen in the default camera app, where a
I'm trying to emulate this graph: If I have a correlation matrix how can
I'm trying to emulate what growl does with an app that I have. At
I'm trying to emulate the scrolling list views in iOS, where you can scroll
I have a list view on a page and am trying emulate the toggling
I am trying to emulate another form that the other developer here created. In
I'm trying to emulate Xcode's ⌘-R keystroke in another editor (namely, Vim); I thought

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.