-
Notifications
You must be signed in to change notification settings - Fork 11
/
WIZUDPSock.py
36 lines (28 loc) · 1.18 KB
/
WIZUDPSock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
import socket
class WIZUDPSock:
# def __init__(self, port, peerport):
def __init__(self, port, peerport, ipaddr=None):
self.sock = None
# self.localport = randint(52000, 53000)
self.localport = 52000
self.peerport = peerport
self.ipaddr = ipaddr
def open(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# socket rcv buffer size
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 524288) # 512 KB
# print('getsockopt SO_RCVBUF:', self.sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF))
# self.sock.bind(("", self.localport))
self.sock.bind((self.ipaddr, self.localport))
self.sock.setblocking(0)
def sendto(self, msg):
self.sock.sendto(msg, ("255.255.255.255", self.peerport))
# self.sock.sendto(msg, ("192.168.50.255", self.peerport))
def recvfrom(self):
data, addr = self.sock.recvfrom(2048)
return data
def close(self):
self.sock.close()