How to use bash to create a socket server and allow multiple clients in the same port?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have 4 programs, will be increased in the future, these programs have to connect to the same ip:port to send and receive messages at the same time.
Until now I have the socket opened, I also would like to keep the connection alive between the programs and the server.
#!/bin/sh
nc -lvk 88.109.110.161 100 > port100.txt 2>&1
linux networking tcp netcat
add a comment |
I have 4 programs, will be increased in the future, these programs have to connect to the same ip:port to send and receive messages at the same time.
Until now I have the socket opened, I also would like to keep the connection alive between the programs and the server.
#!/bin/sh
nc -lvk 88.109.110.161 100 > port100.txt 2>&1
linux networking tcp netcat
add a comment |
I have 4 programs, will be increased in the future, these programs have to connect to the same ip:port to send and receive messages at the same time.
Until now I have the socket opened, I also would like to keep the connection alive between the programs and the server.
#!/bin/sh
nc -lvk 88.109.110.161 100 > port100.txt 2>&1
linux networking tcp netcat
I have 4 programs, will be increased in the future, these programs have to connect to the same ip:port to send and receive messages at the same time.
Until now I have the socket opened, I also would like to keep the connection alive between the programs and the server.
#!/bin/sh
nc -lvk 88.109.110.161 100 > port100.txt 2>&1
linux networking tcp netcat
linux networking tcp netcat
edited Apr 21 at 16:35
Jeff Schaller♦
45.4k1164147
45.4k1164147
asked Apr 21 at 10:44
Martin Ocando CorleoneMartin Ocando Corleone
184
184
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
nc
does not handle multiple connected clients in parallel and is the wrong tool for this job.
There are quite a few right tools for this job, including:
- Bernstein
tcpserver
(original or djbwares) or Hoffmantcpserver
:tcpserver -v -R -H -l 0 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my
tcpserver
shim:tcpserver -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my UCSPI-TCP tools:
tcp-socket-listen 88.109.110.161 100 tcp-socket-accept --verbose sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot
s6-tcpserver4
:s6-tcpserver4 -v 2 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot s6-networking tools:
s6-tcpserver4-socketbinder 88.109.110.161 100 s6-tcpserver4d -v 2 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Pape
tcpsvd
:tcpsvd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Sampson
onenetd
:onenetd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
And one can substitute multilog
, s6-log
, svlogd
, or tinylog
for cyclog
.
Further reading
- Protocol:
- Jonathan de Boyne Pollard (2016). The gen on the UNIX Client-Server Program Interface. Frequently Given Answers.
- Daniel J. Bernstein (1996). UNIX Client-Server Program Interface. cr.yp.to.
- toolsets:
- Daniel J. Bernstein. ucspi-tcp. cr.yp.to.
- Erwin Hoffmann. ucspi-tcp6. fehcom.de.
s6-networking. Laurent Bercot. skarnet.org.- Jonathan de Boyne Pollard (2019). nosh. Softwares.
- Jonathan de Boyne Pollard (2019). djbwares. Softwares.
ipsvd. Gerrit Pape. smarden.org.
onenetd. Adam Sampson. offog.org.
- reference manuals:
- Daniel J. Bernstein. The
tcpserver
program. ucspi-tcp. - Erwin Hoffmann.
tcpserver
. ucspi-tcp6. fehcom.de.
s6-tcpserver4
. Laurent Bercot. s6-networking. skarnet.org.
tcpsvd
. ipsvd. Gerrit Pape. smarden.org.- Jonathan de Boyne Pollard (2019).
tcpserver
. djbwares. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-listen
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-accept
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcpserver
. nosh Guide. Softwares.
- Daniel J. Bernstein. The
- Logging:
- https://unix.stackexchange.com/a/340631/5132
- https://unix.stackexchange.com/a/505854/5132
One can also consider use a load balancing proxy likehaproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1
– crasic
Apr 21 at 20:24
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
add a comment |
It's a real pity that there's no standard/readily available tool for such basic task, but if you're not on some embedded system and have some scripting language like perl
or python
available, you can quickly put something together:
tcpsrv:
#! /usr/bin/perl
use strict;
use IO::Socket::INET6;
die "usage: $0 host:port { shell_cmd | cmd args ... }n" unless @ARGV >= 2;
my $h = shift;
my $s=new IO::Socket::INET6(ReusePort=>1, Listen=>6, LocalAddr=>$h)
or die "IO::Socket::INET($h): $!";
warn "listening on ", $s->sockhost, "/", $s->sockport, "n";
$SIG{CHLD} = sub { use POSIX qw(WNOHANG); 1 while waitpid(-1, WNOHANG) > 0 };
while(1){
my $a = $s->accept or do { die "accept: $!" unless $!{EINTR}; next };
warn "connection from ", $a->peerhost, "/", $a->peerport, "n";
die unless defined(my $p = fork);
close($a), next if $p;
open STDIN, "<&", $a and open STDOUT, ">&", $a or die "dup: $!";
close $s and close $a or die "close: $!";
exec(@ARGV); die "exec @ARGV: $!";
}
Usage: tcpsrv host:port cmd
This will listen on host:port
, and anytime a client connects to host:host
, it will fork & exec cmd
with its stdin and stdout redirected from/to the connection:
tcpsrv :9999 ls .
tcpsrv 127.0.0.1:7000 uptime
tcpsrv [::]:7000 uptime
tcpsrv 88.109.110.161:2000 'cat > port2000.txt'
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f513648%2fhow-to-use-bash-to-create-a-socket-server-and-allow-multiple-clients-in-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
nc
does not handle multiple connected clients in parallel and is the wrong tool for this job.
There are quite a few right tools for this job, including:
- Bernstein
tcpserver
(original or djbwares) or Hoffmantcpserver
:tcpserver -v -R -H -l 0 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my
tcpserver
shim:tcpserver -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my UCSPI-TCP tools:
tcp-socket-listen 88.109.110.161 100 tcp-socket-accept --verbose sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot
s6-tcpserver4
:s6-tcpserver4 -v 2 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot s6-networking tools:
s6-tcpserver4-socketbinder 88.109.110.161 100 s6-tcpserver4d -v 2 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Pape
tcpsvd
:tcpsvd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Sampson
onenetd
:onenetd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
And one can substitute multilog
, s6-log
, svlogd
, or tinylog
for cyclog
.
Further reading
- Protocol:
- Jonathan de Boyne Pollard (2016). The gen on the UNIX Client-Server Program Interface. Frequently Given Answers.
- Daniel J. Bernstein (1996). UNIX Client-Server Program Interface. cr.yp.to.
- toolsets:
- Daniel J. Bernstein. ucspi-tcp. cr.yp.to.
- Erwin Hoffmann. ucspi-tcp6. fehcom.de.
s6-networking. Laurent Bercot. skarnet.org.- Jonathan de Boyne Pollard (2019). nosh. Softwares.
- Jonathan de Boyne Pollard (2019). djbwares. Softwares.
ipsvd. Gerrit Pape. smarden.org.
onenetd. Adam Sampson. offog.org.
- reference manuals:
- Daniel J. Bernstein. The
tcpserver
program. ucspi-tcp. - Erwin Hoffmann.
tcpserver
. ucspi-tcp6. fehcom.de.
s6-tcpserver4
. Laurent Bercot. s6-networking. skarnet.org.
tcpsvd
. ipsvd. Gerrit Pape. smarden.org.- Jonathan de Boyne Pollard (2019).
tcpserver
. djbwares. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-listen
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-accept
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcpserver
. nosh Guide. Softwares.
- Daniel J. Bernstein. The
- Logging:
- https://unix.stackexchange.com/a/340631/5132
- https://unix.stackexchange.com/a/505854/5132
One can also consider use a load balancing proxy likehaproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1
– crasic
Apr 21 at 20:24
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
add a comment |
nc
does not handle multiple connected clients in parallel and is the wrong tool for this job.
There are quite a few right tools for this job, including:
- Bernstein
tcpserver
(original or djbwares) or Hoffmantcpserver
:tcpserver -v -R -H -l 0 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my
tcpserver
shim:tcpserver -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my UCSPI-TCP tools:
tcp-socket-listen 88.109.110.161 100 tcp-socket-accept --verbose sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot
s6-tcpserver4
:s6-tcpserver4 -v 2 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot s6-networking tools:
s6-tcpserver4-socketbinder 88.109.110.161 100 s6-tcpserver4d -v 2 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Pape
tcpsvd
:tcpsvd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Sampson
onenetd
:onenetd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
And one can substitute multilog
, s6-log
, svlogd
, or tinylog
for cyclog
.
Further reading
- Protocol:
- Jonathan de Boyne Pollard (2016). The gen on the UNIX Client-Server Program Interface. Frequently Given Answers.
- Daniel J. Bernstein (1996). UNIX Client-Server Program Interface. cr.yp.to.
- toolsets:
- Daniel J. Bernstein. ucspi-tcp. cr.yp.to.
- Erwin Hoffmann. ucspi-tcp6. fehcom.de.
s6-networking. Laurent Bercot. skarnet.org.- Jonathan de Boyne Pollard (2019). nosh. Softwares.
- Jonathan de Boyne Pollard (2019). djbwares. Softwares.
ipsvd. Gerrit Pape. smarden.org.
onenetd. Adam Sampson. offog.org.
- reference manuals:
- Daniel J. Bernstein. The
tcpserver
program. ucspi-tcp. - Erwin Hoffmann.
tcpserver
. ucspi-tcp6. fehcom.de.
s6-tcpserver4
. Laurent Bercot. s6-networking. skarnet.org.
tcpsvd
. ipsvd. Gerrit Pape. smarden.org.- Jonathan de Boyne Pollard (2019).
tcpserver
. djbwares. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-listen
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-accept
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcpserver
. nosh Guide. Softwares.
- Daniel J. Bernstein. The
- Logging:
- https://unix.stackexchange.com/a/340631/5132
- https://unix.stackexchange.com/a/505854/5132
One can also consider use a load balancing proxy likehaproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1
– crasic
Apr 21 at 20:24
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
add a comment |
nc
does not handle multiple connected clients in parallel and is the wrong tool for this job.
There are quite a few right tools for this job, including:
- Bernstein
tcpserver
(original or djbwares) or Hoffmantcpserver
:tcpserver -v -R -H -l 0 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my
tcpserver
shim:tcpserver -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my UCSPI-TCP tools:
tcp-socket-listen 88.109.110.161 100 tcp-socket-accept --verbose sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot
s6-tcpserver4
:s6-tcpserver4 -v 2 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot s6-networking tools:
s6-tcpserver4-socketbinder 88.109.110.161 100 s6-tcpserver4d -v 2 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Pape
tcpsvd
:tcpsvd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Sampson
onenetd
:onenetd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
And one can substitute multilog
, s6-log
, svlogd
, or tinylog
for cyclog
.
Further reading
- Protocol:
- Jonathan de Boyne Pollard (2016). The gen on the UNIX Client-Server Program Interface. Frequently Given Answers.
- Daniel J. Bernstein (1996). UNIX Client-Server Program Interface. cr.yp.to.
- toolsets:
- Daniel J. Bernstein. ucspi-tcp. cr.yp.to.
- Erwin Hoffmann. ucspi-tcp6. fehcom.de.
s6-networking. Laurent Bercot. skarnet.org.- Jonathan de Boyne Pollard (2019). nosh. Softwares.
- Jonathan de Boyne Pollard (2019). djbwares. Softwares.
ipsvd. Gerrit Pape. smarden.org.
onenetd. Adam Sampson. offog.org.
- reference manuals:
- Daniel J. Bernstein. The
tcpserver
program. ucspi-tcp. - Erwin Hoffmann.
tcpserver
. ucspi-tcp6. fehcom.de.
s6-tcpserver4
. Laurent Bercot. s6-networking. skarnet.org.
tcpsvd
. ipsvd. Gerrit Pape. smarden.org.- Jonathan de Boyne Pollard (2019).
tcpserver
. djbwares. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-listen
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-accept
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcpserver
. nosh Guide. Softwares.
- Daniel J. Bernstein. The
- Logging:
- https://unix.stackexchange.com/a/340631/5132
- https://unix.stackexchange.com/a/505854/5132
nc
does not handle multiple connected clients in parallel and is the wrong tool for this job.
There are quite a few right tools for this job, including:
- Bernstein
tcpserver
(original or djbwares) or Hoffmantcpserver
:tcpserver -v -R -H -l 0 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my
tcpserver
shim:tcpserver -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- my UCSPI-TCP tools:
tcp-socket-listen 88.109.110.161 100 tcp-socket-accept --verbose sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot
s6-tcpserver4
:s6-tcpserver4 -v 2 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Bercot s6-networking tools:
s6-tcpserver4-socketbinder 88.109.110.161 100 s6-tcpserver4d -v 2 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Pape
tcpsvd
:tcpsvd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
- Sampson
onenetd
:onenetd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
cyclog port100/
And one can substitute multilog
, s6-log
, svlogd
, or tinylog
for cyclog
.
Further reading
- Protocol:
- Jonathan de Boyne Pollard (2016). The gen on the UNIX Client-Server Program Interface. Frequently Given Answers.
- Daniel J. Bernstein (1996). UNIX Client-Server Program Interface. cr.yp.to.
- toolsets:
- Daniel J. Bernstein. ucspi-tcp. cr.yp.to.
- Erwin Hoffmann. ucspi-tcp6. fehcom.de.
s6-networking. Laurent Bercot. skarnet.org.- Jonathan de Boyne Pollard (2019). nosh. Softwares.
- Jonathan de Boyne Pollard (2019). djbwares. Softwares.
ipsvd. Gerrit Pape. smarden.org.
onenetd. Adam Sampson. offog.org.
- reference manuals:
- Daniel J. Bernstein. The
tcpserver
program. ucspi-tcp. - Erwin Hoffmann.
tcpserver
. ucspi-tcp6. fehcom.de.
s6-tcpserver4
. Laurent Bercot. s6-networking. skarnet.org.
tcpsvd
. ipsvd. Gerrit Pape. smarden.org.- Jonathan de Boyne Pollard (2019).
tcpserver
. djbwares. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-listen
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcp-socket-accept
. nosh Guide. Softwares. - Jonathan de Boyne Pollard (2019).
tcpserver
. nosh Guide. Softwares.
- Daniel J. Bernstein. The
- Logging:
- https://unix.stackexchange.com/a/340631/5132
- https://unix.stackexchange.com/a/505854/5132
edited Apr 21 at 12:34
answered Apr 21 at 12:27
JdeBPJdeBP
38.9k479186
38.9k479186
One can also consider use a load balancing proxy likehaproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1
– crasic
Apr 21 at 20:24
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
add a comment |
One can also consider use a load balancing proxy likehaproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1
– crasic
Apr 21 at 20:24
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
One can also consider use a load balancing proxy like
haproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1– crasic
Apr 21 at 20:24
One can also consider use a load balancing proxy like
haproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1– crasic
Apr 21 at 20:24
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
I will test them and will let you know how it goes
– Martin Ocando Corleone
Apr 22 at 13:06
add a comment |
It's a real pity that there's no standard/readily available tool for such basic task, but if you're not on some embedded system and have some scripting language like perl
or python
available, you can quickly put something together:
tcpsrv:
#! /usr/bin/perl
use strict;
use IO::Socket::INET6;
die "usage: $0 host:port { shell_cmd | cmd args ... }n" unless @ARGV >= 2;
my $h = shift;
my $s=new IO::Socket::INET6(ReusePort=>1, Listen=>6, LocalAddr=>$h)
or die "IO::Socket::INET($h): $!";
warn "listening on ", $s->sockhost, "/", $s->sockport, "n";
$SIG{CHLD} = sub { use POSIX qw(WNOHANG); 1 while waitpid(-1, WNOHANG) > 0 };
while(1){
my $a = $s->accept or do { die "accept: $!" unless $!{EINTR}; next };
warn "connection from ", $a->peerhost, "/", $a->peerport, "n";
die unless defined(my $p = fork);
close($a), next if $p;
open STDIN, "<&", $a and open STDOUT, ">&", $a or die "dup: $!";
close $s and close $a or die "close: $!";
exec(@ARGV); die "exec @ARGV: $!";
}
Usage: tcpsrv host:port cmd
This will listen on host:port
, and anytime a client connects to host:host
, it will fork & exec cmd
with its stdin and stdout redirected from/to the connection:
tcpsrv :9999 ls .
tcpsrv 127.0.0.1:7000 uptime
tcpsrv [::]:7000 uptime
tcpsrv 88.109.110.161:2000 'cat > port2000.txt'
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
add a comment |
It's a real pity that there's no standard/readily available tool for such basic task, but if you're not on some embedded system and have some scripting language like perl
or python
available, you can quickly put something together:
tcpsrv:
#! /usr/bin/perl
use strict;
use IO::Socket::INET6;
die "usage: $0 host:port { shell_cmd | cmd args ... }n" unless @ARGV >= 2;
my $h = shift;
my $s=new IO::Socket::INET6(ReusePort=>1, Listen=>6, LocalAddr=>$h)
or die "IO::Socket::INET($h): $!";
warn "listening on ", $s->sockhost, "/", $s->sockport, "n";
$SIG{CHLD} = sub { use POSIX qw(WNOHANG); 1 while waitpid(-1, WNOHANG) > 0 };
while(1){
my $a = $s->accept or do { die "accept: $!" unless $!{EINTR}; next };
warn "connection from ", $a->peerhost, "/", $a->peerport, "n";
die unless defined(my $p = fork);
close($a), next if $p;
open STDIN, "<&", $a and open STDOUT, ">&", $a or die "dup: $!";
close $s and close $a or die "close: $!";
exec(@ARGV); die "exec @ARGV: $!";
}
Usage: tcpsrv host:port cmd
This will listen on host:port
, and anytime a client connects to host:host
, it will fork & exec cmd
with its stdin and stdout redirected from/to the connection:
tcpsrv :9999 ls .
tcpsrv 127.0.0.1:7000 uptime
tcpsrv [::]:7000 uptime
tcpsrv 88.109.110.161:2000 'cat > port2000.txt'
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
add a comment |
It's a real pity that there's no standard/readily available tool for such basic task, but if you're not on some embedded system and have some scripting language like perl
or python
available, you can quickly put something together:
tcpsrv:
#! /usr/bin/perl
use strict;
use IO::Socket::INET6;
die "usage: $0 host:port { shell_cmd | cmd args ... }n" unless @ARGV >= 2;
my $h = shift;
my $s=new IO::Socket::INET6(ReusePort=>1, Listen=>6, LocalAddr=>$h)
or die "IO::Socket::INET($h): $!";
warn "listening on ", $s->sockhost, "/", $s->sockport, "n";
$SIG{CHLD} = sub { use POSIX qw(WNOHANG); 1 while waitpid(-1, WNOHANG) > 0 };
while(1){
my $a = $s->accept or do { die "accept: $!" unless $!{EINTR}; next };
warn "connection from ", $a->peerhost, "/", $a->peerport, "n";
die unless defined(my $p = fork);
close($a), next if $p;
open STDIN, "<&", $a and open STDOUT, ">&", $a or die "dup: $!";
close $s and close $a or die "close: $!";
exec(@ARGV); die "exec @ARGV: $!";
}
Usage: tcpsrv host:port cmd
This will listen on host:port
, and anytime a client connects to host:host
, it will fork & exec cmd
with its stdin and stdout redirected from/to the connection:
tcpsrv :9999 ls .
tcpsrv 127.0.0.1:7000 uptime
tcpsrv [::]:7000 uptime
tcpsrv 88.109.110.161:2000 'cat > port2000.txt'
It's a real pity that there's no standard/readily available tool for such basic task, but if you're not on some embedded system and have some scripting language like perl
or python
available, you can quickly put something together:
tcpsrv:
#! /usr/bin/perl
use strict;
use IO::Socket::INET6;
die "usage: $0 host:port { shell_cmd | cmd args ... }n" unless @ARGV >= 2;
my $h = shift;
my $s=new IO::Socket::INET6(ReusePort=>1, Listen=>6, LocalAddr=>$h)
or die "IO::Socket::INET($h): $!";
warn "listening on ", $s->sockhost, "/", $s->sockport, "n";
$SIG{CHLD} = sub { use POSIX qw(WNOHANG); 1 while waitpid(-1, WNOHANG) > 0 };
while(1){
my $a = $s->accept or do { die "accept: $!" unless $!{EINTR}; next };
warn "connection from ", $a->peerhost, "/", $a->peerport, "n";
die unless defined(my $p = fork);
close($a), next if $p;
open STDIN, "<&", $a and open STDOUT, ">&", $a or die "dup: $!";
close $s and close $a or die "close: $!";
exec(@ARGV); die "exec @ARGV: $!";
}
Usage: tcpsrv host:port cmd
This will listen on host:port
, and anytime a client connects to host:host
, it will fork & exec cmd
with its stdin and stdout redirected from/to the connection:
tcpsrv :9999 ls .
tcpsrv 127.0.0.1:7000 uptime
tcpsrv [::]:7000 uptime
tcpsrv 88.109.110.161:2000 'cat > port2000.txt'
edited Apr 21 at 18:52
answered Apr 21 at 17:43
Uncle BillyUncle Billy
1,0028
1,0028
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
add a comment |
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
Never worked with perl, but I will do the attempt, thanks
– Martin Ocando Corleone
Apr 22 at 13:07
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f513648%2fhow-to-use-bash-to-create-a-socket-server-and-allow-multiple-clients-in-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown