netcat doesn't terminate when stdin closes
I'm trying to send a message through netcat
. After sending the message, netcat
must terminate.
I've tried the following:
cat tsmmessage.bin | nc -u localhost 4300
nc -u localhost 4300 < message.bin
The -q
option states:
-q seconds
after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.
But
nc -q0 -u localhost 4300 < message.bin
also doesn't work.
What am I missing?
netcat
add a comment |
I'm trying to send a message through netcat
. After sending the message, netcat
must terminate.
I've tried the following:
cat tsmmessage.bin | nc -u localhost 4300
nc -u localhost 4300 < message.bin
The -q
option states:
-q seconds
after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.
But
nc -q0 -u localhost 4300 < message.bin
also doesn't work.
What am I missing?
netcat
add a comment |
I'm trying to send a message through netcat
. After sending the message, netcat
must terminate.
I've tried the following:
cat tsmmessage.bin | nc -u localhost 4300
nc -u localhost 4300 < message.bin
The -q
option states:
-q seconds
after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.
But
nc -q0 -u localhost 4300 < message.bin
also doesn't work.
What am I missing?
netcat
I'm trying to send a message through netcat
. After sending the message, netcat
must terminate.
I've tried the following:
cat tsmmessage.bin | nc -u localhost 4300
nc -u localhost 4300 < message.bin
The -q
option states:
-q seconds
after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.
But
nc -q0 -u localhost 4300 < message.bin
also doesn't work.
What am I missing?
netcat
netcat
asked Mar 11 '15 at 7:06
Frank KustersFrank Kusters
15117
15117
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Assuming that after sending EOF connection will stay idle, you can use -w timeout
option, which works for timeout
being equal to zero (unlike stupid -q
option...)
cat tsmmessage.bin | nc -u localhost 4300 -w0
1
This is the correct answer and must be the accepted one rather than-q
.
– ccpizza
Nov 13 '17 at 18:07
zero time out doesn't work on my machine (debian stretch). it saysinvalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
add a comment |
Without the -q
flag your instance of netcat
will wait forever. There's no "end of stream" message with UDP so there is no way for netcat
to know that both stdin and the network connection have finished.
For example, using TCP/IP this works as expected:
nc -l localhost 4300 # Window 1
nc localhost 4300 </etc/group # Window 2
But as you have determined, using UDP/IP this never ends:
nc -u -l localhost 4300 # Window 1
nc -u localhost 4300 </etc/group # Window 2
This is where the -q
flag comes in. But unfortunately it doesn't accept a value of 0
. It also won't accept non-integer values. Here is the best alternative I can offer without recourse to timeout
or some other external utility:
nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Even here, it's not possible to have the listening netcat
time out gracefully. (The -w
timeout option is ignored, and -q
is irrelevant.) Something like this might be of use in a practical situation, so that the netcat
is killed after 90 seconds:
timeout 90 nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
-q 0
works for me.
– AlikElzin-kilaka
Nov 11 '18 at 8:52
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
add a comment |
I suggest you close the connection on EOF from stdin using -c or --close
# listen on receiver
nc -u -l localhost 4300
# sender
cat tsmmessage.bin | nc -u -c localhost 4300
New contributor
add a comment |
Stumbled upon this when Googling regarding pretty much the same problem. It turned out the issue was that netcat got killed by bash right after all the data got sucked in, without getting any chance to receive the response.
My solution to this was to add some delay after piping the data, like this:
(echo INFO; sleep 1) | nc redis.service.consul 6379
With a file this can look like:
(cat tsmmessage.bin; sleep 5) | nc -u localhost 4300
Hope this helps anyone who gets here further on on his search for truth.
netcat
still doesn't close whensleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.
– Frank Kusters
Jun 1 '16 at 9:35
how about adding-q 1
? i.e.(echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?
– SkyWriter
Jun 1 '16 at 9:36
With-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.
– Frank Kusters
Jun 1 '16 at 10:00
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
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%2f189454%2fnetcat-doesnt-terminate-when-stdin-closes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming that after sending EOF connection will stay idle, you can use -w timeout
option, which works for timeout
being equal to zero (unlike stupid -q
option...)
cat tsmmessage.bin | nc -u localhost 4300 -w0
1
This is the correct answer and must be the accepted one rather than-q
.
– ccpizza
Nov 13 '17 at 18:07
zero time out doesn't work on my machine (debian stretch). it saysinvalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
add a comment |
Assuming that after sending EOF connection will stay idle, you can use -w timeout
option, which works for timeout
being equal to zero (unlike stupid -q
option...)
cat tsmmessage.bin | nc -u localhost 4300 -w0
1
This is the correct answer and must be the accepted one rather than-q
.
– ccpizza
Nov 13 '17 at 18:07
zero time out doesn't work on my machine (debian stretch). it saysinvalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
add a comment |
Assuming that after sending EOF connection will stay idle, you can use -w timeout
option, which works for timeout
being equal to zero (unlike stupid -q
option...)
cat tsmmessage.bin | nc -u localhost 4300 -w0
Assuming that after sending EOF connection will stay idle, you can use -w timeout
option, which works for timeout
being equal to zero (unlike stupid -q
option...)
cat tsmmessage.bin | nc -u localhost 4300 -w0
answered Nov 22 '16 at 6:40
Bora M. AlperBora M. Alper
18815
18815
1
This is the correct answer and must be the accepted one rather than-q
.
– ccpizza
Nov 13 '17 at 18:07
zero time out doesn't work on my machine (debian stretch). it saysinvalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
add a comment |
1
This is the correct answer and must be the accepted one rather than-q
.
– ccpizza
Nov 13 '17 at 18:07
zero time out doesn't work on my machine (debian stretch). it saysinvalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
1
1
This is the correct answer and must be the accepted one rather than
-q
.– ccpizza
Nov 13 '17 at 18:07
This is the correct answer and must be the accepted one rather than
-q
.– ccpizza
Nov 13 '17 at 18:07
zero time out doesn't work on my machine (debian stretch). it says
invalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
zero time out doesn't work on my machine (debian stretch). it says
invalid wait-time 0
– Anubis
Apr 20 '18 at 10:47
add a comment |
Without the -q
flag your instance of netcat
will wait forever. There's no "end of stream" message with UDP so there is no way for netcat
to know that both stdin and the network connection have finished.
For example, using TCP/IP this works as expected:
nc -l localhost 4300 # Window 1
nc localhost 4300 </etc/group # Window 2
But as you have determined, using UDP/IP this never ends:
nc -u -l localhost 4300 # Window 1
nc -u localhost 4300 </etc/group # Window 2
This is where the -q
flag comes in. But unfortunately it doesn't accept a value of 0
. It also won't accept non-integer values. Here is the best alternative I can offer without recourse to timeout
or some other external utility:
nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Even here, it's not possible to have the listening netcat
time out gracefully. (The -w
timeout option is ignored, and -q
is irrelevant.) Something like this might be of use in a practical situation, so that the netcat
is killed after 90 seconds:
timeout 90 nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
-q 0
works for me.
– AlikElzin-kilaka
Nov 11 '18 at 8:52
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
add a comment |
Without the -q
flag your instance of netcat
will wait forever. There's no "end of stream" message with UDP so there is no way for netcat
to know that both stdin and the network connection have finished.
For example, using TCP/IP this works as expected:
nc -l localhost 4300 # Window 1
nc localhost 4300 </etc/group # Window 2
But as you have determined, using UDP/IP this never ends:
nc -u -l localhost 4300 # Window 1
nc -u localhost 4300 </etc/group # Window 2
This is where the -q
flag comes in. But unfortunately it doesn't accept a value of 0
. It also won't accept non-integer values. Here is the best alternative I can offer without recourse to timeout
or some other external utility:
nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Even here, it's not possible to have the listening netcat
time out gracefully. (The -w
timeout option is ignored, and -q
is irrelevant.) Something like this might be of use in a practical situation, so that the netcat
is killed after 90 seconds:
timeout 90 nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
-q 0
works for me.
– AlikElzin-kilaka
Nov 11 '18 at 8:52
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
add a comment |
Without the -q
flag your instance of netcat
will wait forever. There's no "end of stream" message with UDP so there is no way for netcat
to know that both stdin and the network connection have finished.
For example, using TCP/IP this works as expected:
nc -l localhost 4300 # Window 1
nc localhost 4300 </etc/group # Window 2
But as you have determined, using UDP/IP this never ends:
nc -u -l localhost 4300 # Window 1
nc -u localhost 4300 </etc/group # Window 2
This is where the -q
flag comes in. But unfortunately it doesn't accept a value of 0
. It also won't accept non-integer values. Here is the best alternative I can offer without recourse to timeout
or some other external utility:
nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Even here, it's not possible to have the listening netcat
time out gracefully. (The -w
timeout option is ignored, and -q
is irrelevant.) Something like this might be of use in a practical situation, so that the netcat
is killed after 90 seconds:
timeout 90 nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Without the -q
flag your instance of netcat
will wait forever. There's no "end of stream" message with UDP so there is no way for netcat
to know that both stdin and the network connection have finished.
For example, using TCP/IP this works as expected:
nc -l localhost 4300 # Window 1
nc localhost 4300 </etc/group # Window 2
But as you have determined, using UDP/IP this never ends:
nc -u -l localhost 4300 # Window 1
nc -u localhost 4300 </etc/group # Window 2
This is where the -q
flag comes in. But unfortunately it doesn't accept a value of 0
. It also won't accept non-integer values. Here is the best alternative I can offer without recourse to timeout
or some other external utility:
nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Even here, it's not possible to have the listening netcat
time out gracefully. (The -w
timeout option is ignored, and -q
is irrelevant.) Something like this might be of use in a practical situation, so that the netcat
is killed after 90 seconds:
timeout 90 nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
answered Jul 27 '17 at 9:08
roaimaroaima
44.4k555119
44.4k555119
-q 0
works for me.
– AlikElzin-kilaka
Nov 11 '18 at 8:52
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
add a comment |
-q 0
works for me.
– AlikElzin-kilaka
Nov 11 '18 at 8:52
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
-q 0
works for me.– AlikElzin-kilaka
Nov 11 '18 at 8:52
-q 0
works for me.– AlikElzin-kilaka
Nov 11 '18 at 8:52
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
@AlikElzin-kilaka doesn't work for me though. You're definitely using UDP in your tests? What version of netcat do you have? You're probably on a more recent version.
– roaima
Nov 11 '18 at 9:01
add a comment |
I suggest you close the connection on EOF from stdin using -c or --close
# listen on receiver
nc -u -l localhost 4300
# sender
cat tsmmessage.bin | nc -u -c localhost 4300
New contributor
add a comment |
I suggest you close the connection on EOF from stdin using -c or --close
# listen on receiver
nc -u -l localhost 4300
# sender
cat tsmmessage.bin | nc -u -c localhost 4300
New contributor
add a comment |
I suggest you close the connection on EOF from stdin using -c or --close
# listen on receiver
nc -u -l localhost 4300
# sender
cat tsmmessage.bin | nc -u -c localhost 4300
New contributor
I suggest you close the connection on EOF from stdin using -c or --close
# listen on receiver
nc -u -l localhost 4300
# sender
cat tsmmessage.bin | nc -u -c localhost 4300
New contributor
New contributor
answered 52 mins ago
krazedkrishkrazedkrish
1011
1011
New contributor
New contributor
add a comment |
add a comment |
Stumbled upon this when Googling regarding pretty much the same problem. It turned out the issue was that netcat got killed by bash right after all the data got sucked in, without getting any chance to receive the response.
My solution to this was to add some delay after piping the data, like this:
(echo INFO; sleep 1) | nc redis.service.consul 6379
With a file this can look like:
(cat tsmmessage.bin; sleep 5) | nc -u localhost 4300
Hope this helps anyone who gets here further on on his search for truth.
netcat
still doesn't close whensleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.
– Frank Kusters
Jun 1 '16 at 9:35
how about adding-q 1
? i.e.(echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?
– SkyWriter
Jun 1 '16 at 9:36
With-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.
– Frank Kusters
Jun 1 '16 at 10:00
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
add a comment |
Stumbled upon this when Googling regarding pretty much the same problem. It turned out the issue was that netcat got killed by bash right after all the data got sucked in, without getting any chance to receive the response.
My solution to this was to add some delay after piping the data, like this:
(echo INFO; sleep 1) | nc redis.service.consul 6379
With a file this can look like:
(cat tsmmessage.bin; sleep 5) | nc -u localhost 4300
Hope this helps anyone who gets here further on on his search for truth.
netcat
still doesn't close whensleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.
– Frank Kusters
Jun 1 '16 at 9:35
how about adding-q 1
? i.e.(echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?
– SkyWriter
Jun 1 '16 at 9:36
With-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.
– Frank Kusters
Jun 1 '16 at 10:00
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
add a comment |
Stumbled upon this when Googling regarding pretty much the same problem. It turned out the issue was that netcat got killed by bash right after all the data got sucked in, without getting any chance to receive the response.
My solution to this was to add some delay after piping the data, like this:
(echo INFO; sleep 1) | nc redis.service.consul 6379
With a file this can look like:
(cat tsmmessage.bin; sleep 5) | nc -u localhost 4300
Hope this helps anyone who gets here further on on his search for truth.
Stumbled upon this when Googling regarding pretty much the same problem. It turned out the issue was that netcat got killed by bash right after all the data got sucked in, without getting any chance to receive the response.
My solution to this was to add some delay after piping the data, like this:
(echo INFO; sleep 1) | nc redis.service.consul 6379
With a file this can look like:
(cat tsmmessage.bin; sleep 5) | nc -u localhost 4300
Hope this helps anyone who gets here further on on his search for truth.
answered Jun 1 '16 at 6:14
SkyWriterSkyWriter
99
99
netcat
still doesn't close whensleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.
– Frank Kusters
Jun 1 '16 at 9:35
how about adding-q 1
? i.e.(echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?
– SkyWriter
Jun 1 '16 at 9:36
With-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.
– Frank Kusters
Jun 1 '16 at 10:00
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
add a comment |
netcat
still doesn't close whensleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.
– Frank Kusters
Jun 1 '16 at 9:35
how about adding-q 1
? i.e.(echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?
– SkyWriter
Jun 1 '16 at 9:36
With-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.
– Frank Kusters
Jun 1 '16 at 10:00
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
netcat
still doesn't close when sleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.– Frank Kusters
Jun 1 '16 at 9:35
netcat
still doesn't close when sleep
finishes. I would expect the first command line to return to the prompt after 1 second, but it doesn't.– Frank Kusters
Jun 1 '16 at 9:35
how about adding
-q 1
? i.e. (echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?– SkyWriter
Jun 1 '16 at 9:36
how about adding
-q 1
? i.e. (echo INFO; sleep 1) | nc -q 1 redis.service.consul 6379
?– SkyWriter
Jun 1 '16 at 9:36
With
-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.– Frank Kusters
Jun 1 '16 at 10:00
With
-q
everything works, even the example in my original question. I've moved to a newer version of Ubuntu since then, maybe that causes the difference.– Frank Kusters
Jun 1 '16 at 10:00
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
That's weird. Anyways, glad we both found a way around this :)
– SkyWriter
Jun 1 '16 at 17:32
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%2f189454%2fnetcat-doesnt-terminate-when-stdin-closes%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