packetstormer has asked for the wisdom of the Perl Monks concerning the following question:
Hello
Could someone offer some advice on the code below? When I connect a client it connects and does as I would expect. However, it seems to close the socket after the first input from the client.
Is there any way to keep the server running to keep sending/receiving data to/from the client, until the client disconnects?
Ideally I'd like the client to connect, send a 99 and get a corresponding response from the server. Then, the client stays connected and at some point in the future is might send a 63 string or maybe even another 99. All of this without the server closing the connection and forcing the client to reconnect
Thanks!
#!/usr/bin/perl use strict; use warnings; use feature 'say'; use IO::Socket qw(AF_INET AF_UNIX SOCK_STREAM SHUT_WR); use Data::Dumper; my $server = IO::Socket->new( Domain => AF_INET, Type => SOCK_STREAM, Proto => 'tcp', LocalHost => '0.0.0.0', LocalPort => 3333, ReusePort => 1, Listen => 5, ) || die "Can't open socket: $IO::Socket::errstr"; say "Waiting on 3333"; while (1) { my $client = $server->accept(); my $client_address = $client->peerhost(); my $client_port = $client->peerport(); say "Connection from $client_address:$client_port"; my $data; $client->recv($data, 1024); say "received data: $data"; if ($data =~ m/99/) { $data = "We've received a string starting withh 99"; } if ($data =~ m/63/) { $data = "We've received a string starting with 63"; } $client->send($data); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Socket simple server
by haukex (Archbishop) on May 29, 2023 at 16:38 UTC | |
|
Re: IO::Socket simple server
by tybalt89 (Monsignor) on May 29, 2023 at 18:31 UTC | |
|
Re: IO::Socket simple server
by eyepopslikeamosquito (Archbishop) on May 29, 2023 at 22:14 UTC | |
|
Re: IO::Socket simple server
by Anonymous Monk on Jun 01, 2023 at 00:00 UTC |