Install Basic UDP Server on Ubuntu

I'm doing some code testing and one aspect is I need an active UDP Server. I've tried looking online how to install one and haven't had much luck.

My only requirement is a UDP Server which I can connect to with my code and retrieve a simple response.

I'm using Ubuntu 16.04 Xenial.

Can someone please assist in this.

My quesiton is to provide a UDP Server i can use.

I see Arcserve UDP Agent (Linux) but not sure because that might be for backups

I also see netcat but how do i send a response? Спасибо!

ОБНОВЛЕНИЕ

Вот что я пробовал:

root@ubuntuT:/home/jon/gocode/udpserv# echo "pingpong" | nc -u 127.0.0.1 -l 12345 &
[5] 36067
root@ubuntuT:/home/jon/gocode/udpserv# curl http://127.0.0.1:12345
curl: (7) Failed to connect to 127.0.0.1 port 12345: Connection refused
1
задан 11 June 2018 в 20:09
2 ответа

Попробуйте netcat:

nc -u -l 12345

Он запустит простой сервер, который будет отправлять вам байты с нулевым значением на порт 12345.

или:

эхо "пинг-понг" | nc -u -l 12345

, если вам нужен простой текстовый ответ.

2
ответ дан 3 December 2019 в 20:14

Вы можете установить dnsmasq для прослушивания порта 53 / udp. Это быстро.

Вы можете запустить прослушиватель Python на udp.

 #/usr/bin/env/python3 
 #Python UDP Listener, listening on localhost 1025, change address 
 # to listen on other ip/port combos. 
 import socket

 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 sock.bind(('127.0.0.1', 1025))

 while True:
    data, address = sock.recvfrom(65538)
    text = data.decode('ascii')
    print('Connection from Client{} says {}'.format(address, text))
    text = 'Your data was {} bytes long'.format(len(data))
    data = text.encode('ascii')
    sock.sendto(data, address)

Если вам нужен клиент, это сработает.

#/usr/bin/env/python3 
#UDP Client

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
text = 'Hello World'
data = text.encode('ascii')
sock.sendto(data, ('127.0.0.1', 1025))
data, address = sock.recvfrom(65538) 




#text = data.decode('ascii')
#print('The server {} replied {}'.format(address, text))
0
ответ дан 3 December 2019 в 20:14

Теги

Похожие вопросы