LoginSignup
4

More than 3 years have passed since last update.

PythonでUDP同時接続

Posted at

動画データをストリーミングするために,まず最初にシンプルな数字のデータを送受信できるプログラムを作りました!

UDPを選んだ理由

最初はTCPでストリーミングサーバーを作っていたのですが,TCPだと一つのポートで複数のクライアントと同時通信ができなさそうだったので,StatelessなプロトコルのUDPで作ることにしました!

設定ファイル

connection.ini
[server]
ip = 127.0.0.1
port = 1935

[packet]
# [bytes]
header_size = 4
# [pixels]
image_width = 256
image_height = 256

受信側

server.py
import socket
import configparser
import logging

logging.basicConfig(level=logging.DEBUG)

config = configparser.ConfigParser()
config.read('./connection.ini', 'UTF-8')

# Connection Settings
SERVER_IP = config.get('server', 'ip')
SERVER_PORT = int(config.get('server', 'port'))

# Image Settings
IMAGE_WIDTH = int(config.get('packet', 'image_width'))
IMAGE_HEIGHT = int(config.get('packet', 'image_height'))
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT // 2
logging.info(" IMAGE SIZE: " + str(IMAGE_SIZE))

if __name__ == '__main__':
  with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.bind((SERVER_IP, SERVER_PORT))
    logging.info(" Binding port on: " + SERVER_IP + ":" + str(SERVER_PORT))

    while True:
      data, addr = s.recvfrom(IMAGE_SIZE)
      print(addr, int.from_bytes(data, 'big'))

送信側

stream.py
import socket
import configparser
import logging
import time

logging.basicConfig(level=logging.DEBUG)

config = configparser.ConfigParser()
config.read('./connection.ini', 'UTF-8')

# Connection Settings
SERVER_IP = config.get('server', 'ip')
SERVER_PORT = int(config.get('server', 'port'))

# Image Settings
IMAGE_WIDTH = int(config.get('packet', 'image_width'))
IMAGE_HEIGHT = int(config.get('packet', 'image_height'))
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT // 2
logging.info(" IMAGE SIZE: " + str(IMAGE_SIZE))

if __name__ == '__main__':
  with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    i = 0
    while True:
      s.sendto(i.to_bytes(IMAGE_SIZE, 'big'), (SERVER_IP, SERVER_PORT))
      logging.info(" Sent: " + str(i))
      time.sleep(1)
      i = i + 1

ざっくりとした流れ

設定ファイルからアドレスとポートを読み込み,そこにインクリメントされ続けるiの値をstream.pyから'server.py'に送信し続けます。
'socket.SOCK_DGRAM'はUDP通信を行うことを示しています。recvfrom()は受け取ったデータとデータの送信元のタプルを返してくれるので,これを利用してターミナルに情報を表示します。

実行結果

server.pyひとつとstream.pyふたつで実行してみます。

INFO:root: IMAGE SIZE: 32768
INFO:root: Binding port on: 127.0.0.1:1935
('127.0.0.1', 55382) 0
('127.0.0.1', 55382) 1
('127.0.0.1', 55382) 2
('127.0.0.1', 55382) 3
('127.0.0.1', 55382) 4
('127.0.0.1', 55382) 5
('127.0.0.1', 55382) 6
('127.0.0.1', 55382) 7
('127.0.0.1', 55382) 8
('127.0.0.1', 59979) 0
('127.0.0.1', 55382) 9
('127.0.0.1', 59979) 1
('127.0.0.1', 55382) 10
('127.0.0.1', 59979) 2
('127.0.0.1', 55382) 11
('127.0.0.1', 59979) 3
('127.0.0.1', 55382) 12
('127.0.0.1', 59979) 4
('127.0.0.1', 55382) 13
('127.0.0.1', 59979) 5
('127.0.0.1', 55382) 14
('127.0.0.1', 59979) 6
('127.0.0.1', 55382) 15
('127.0.0.1', 59979) 7
('127.0.0.1', 55382) 16
('127.0.0.1', 59979) 8
('127.0.0.1', 55382) 17
('127.0.0.1', 59979) 9
('127.0.0.1', 55382) 18
('127.0.0.1', 59979) 10
('127.0.0.1', 55382) 19
('127.0.0.1', 59979) 11

2つのアドレスからデータがそれぞれ送られてきています!

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
4