0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PyQt5 > UDP > echo server > Multicast版? | non-Multicast版

Last updated at Posted at 2018-01-19
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
PyQt5 v5.5.1

プロジェクト開始: https://qiita.com/7of9/items/4077c92b9a773e054193

処理概要

  • UDP (ポート7000)で受信
  • 受信した文字をそのままエコーバックする

参考

code v0.1 > Multicast版?

udp_echoserver_180119.py
import PyQt5.QtNetwork as qtn

'''
v0.1 Jan. 19, 2018
  - echo back
'''

UDP_PORT = 7000

groupaddr = qtn.QHostAddress("192.168.0.1") 
udp = qtn.QUdpSocket()
udp.setSocketOption(qtn.QAbstractSocket.MulticastLoopbackOption,1)
udp.setSocketOption(qtn.QAbstractSocket.MulticastTtlOption,32)
udp.bind(qtn.QHostAddress.AnyIPv4,UDP_PORT,qtn.QUdpSocket.ShareAddress)
ni = qtn.QNetworkInterface()
udp.joinMulticastGroup(groupaddr,ni)

while True:
    if udp.hasPendingDatagrams():
        data,host,port = udp.readDatagram(udp.pendingDatagramSize())
        udp.writeDatagram(data, host, port)
        print(data)
        print(host)
        
udp.leaveMulticastGroup(groupaddr)
udp.close()

実行

受信側はUbuntu 16.04 LTSで、送信側はRaspberry Piとしている。

run(Ubuntu)
$ python3 udp_echoserver_180119.py 
b'test\n'
<PyQt5.QtNetwork.QHostAddress object at 0x7f4143108908>
b'test\n'
<PyQt5.QtNetwork.QHostAddress object at 0x7f4143108978>
...
run(RPi)
pi@raspberrypi ~ $ echo "test" | nc -u 192.168.0.9 7000
test
^C
pi@raspberrypi ~ $ echo "test" | nc -u 192.168.0.9 7000
test
^C
...

code v0.2 > non-Multicast版

udp_echoserver_180119.py
import PyQt5.QtNetwork as qtn

'''
v0.2 Jan. 19, 2018
  - remove multicast feature
v0.1 Jan. 19, 2018
  - echo back
'''

UDP_PORT = 7000

udp = qtn.QUdpSocket()
udp.bind(UDP_PORT)

while True:
    if udp.hasPendingDatagrams():
        data,host,port = udp.readDatagram(udp.pendingDatagramSize())
        udp.writeDatagram(data, host, port)
        print(data.decode("utf-8"))
        print(host)

udp.close()

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?