Strange behaviour of netcat with UDP

前端 未结 4 593
天命终不由人
天命终不由人 2020-12-13 13:36

I noticed a strange behaviour working with netcat and UDP. I start an instance (instance 1) of netcat that listens on a UDP port:

nc -lu -p 10000
         


        
4条回答
  •  醉酒成梦
    2020-12-13 13:50

    Having given up on netcat on my OS version this is pretty short and gets the job done:

    #!/usr/bin/ruby
    # Receive UDP packets bound for a port and output them
    require 'socket'
    require 'yaml'
    
    unless ARGV.count == 2
      puts "Usage: #{$0} listen_ip port_number"
      exit(1)
    end
    listen_ip = ARGV[0]
    port = ARGV[1].to_i
    
    u1 = UDPSocket.new
    u1.bind(listen_ip, port)
    while true
      mesg, addr = u1.recvfrom(100000)
      puts mesg
    end
    

提交回复
热议问题