使netcat监听多个UDP数据包

如果我运行一个简单的UDP监听器,像这样:

nc -l -u -p 1234 

然后,我似乎只得到第一个入站UDP数据包。 例如,如果我运行:

 $ echo abc | nc -u localhost 1234 ["abc" appears in output of server as expected] $ echo abc | nc -u localhost 1234 read(net): Connection refused 

使用零(0)超时

“服务器”:

 nc -kluvw 0 localhost 9000 

“客户”:

 echo -e "all" | nc -vuw 0 localhost 9000 echo -e "the" | nc -vuw 0 localhost 9000 echo -e "udp" | nc -vuw 0 localhost 9000 echo -e "packets" | nc -vuw 0 localhost 9000 

结果:

 Connection from 127.0.0.1 port 9000 [udp/*] accepted all Connection from 127.0.0.1 port 9000 [udp/*] accepted the Connection from 127.0.0.1 port 9000 [udp/*] accepted udp Connection from 127.0.0.1 port 9000 [udp/*] accepted packets 

经过testing:

 uname -a Linux ubuntu 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux 

我find了一个可以帮助你的链接: netcat:与UDP的奇怪行为 – 只接收第一个数据包发送 。

在这个阶段,我不知道该怎么做,但是Nathan指出,如果我在客户端和服务器上使用了超时,那么就有可能发送多个UDP数据包。

我重新启动了netcat服务器,但这次是1秒超时:

 $ nc -kluvw 1 localhost 9000 

然后开始从一个netcat客户端发送UDP数据包,也有1秒超时:

  $ echo -e "all" | nc -vvuw 1 localhost 9000 Connection to localhost 9000 port [udp/cslistener] succeeded! $ echo -e "the" | nc -vvuw 1 localhost 9000 Connection to localhost 9000 port [udp/cslistener] succeeded! $ echo -e "udp" | nc -vvuw 1 localhost 9000 Connection to localhost 9000 port [udp/cslistener] succeeded! $ echo -e "packets" | nc -vvuw 1 localhost 9000 Connection to localhost 9000 port [udp/cslistener] succeeded! 

而netcat服务器现在收到所有这些:

 $ nc -kluvw 1 localhost 9000 XXXXall XXXXthe XXXXudp XXXXpackets 

这是一个有趣的问题,希望这回答你的问题。