赞
踩
server端
- # udp_gb_server.py
- '''服务端(UDP协议局域网广播)'''
-
- import socket,time,struct
-
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
-
- PORT = 6454
-
- network ='127.0.0.1'# '<broadcast>'
- s.sendto('Client broadcast message!'.encode('utf-8'), (network, PORT))
-
-
- def formatdata(dvcid,dat):
- sendBuf=[0x41, 0x72, 0x74, 0x2D, 0x4E, 0x65, 0x74, 0x00, 0x00, 0x50, 0x00, 0x0E, 0x77, 0x01]
-
- #sendBuf[14]设备节点
- #sendBuf[15] = 0x00;
- sendBuf.append(dvcid& 0xff)
- sendBuf.append(0x00)
-
- # 发送的字节数 512
- #sendBuf[16] = 0x02;
- #sendBuf[17] = 0x00;
- datlen = len(dat)
- sendBuf.append((datlen & 0xff00) >> 8)
- sendBuf.append(datlen & 0xff)
- # 发送的数据
- #sendBuf[17+n] = 0x00;
- print( datlen)
-
- for x in range(datlen):
- pass
- sendBuf.append((dat[x] & 0xff))#.to_bytes(1,'big')
- #data = [1, 2, 3]
- #buffer = struct.pack("!ihb", *data)
-
- packstyle=str(18+datlen)+'B'#B 0-255
- #req = struct.pack('14b',*sendBuf) 0-127
- req = struct.pack(packstyle, *sendBuf)
- return req
-
-
- import struct
- from ctypes import create_string_buffer
- def pack():
- # 创建一个9字节大小的缓存区,初始化默认全部为\x00
- buf = create_string_buffer(9)# buf.raw: '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
-
- # 冲缓存区buf的第0个字节开始打包两个4字节无符号整型数据1和2
- struct.pack_into(">II", buf, 0, 1, 2)# buf.raw: '\x00\x00\x00\x01\x00\x00\x00\x02\x00'
- # 然后我们想再打包一个布尔型数据到buf中就可以改变以下偏移量
- struct.pack_into(">?", buf, 8, True)# buf.raw: '\x00\x00\x00\x01\x00\x00\x00\x02\x01'
- return buf
-
- c=0
- while True:
- c=c+1
- d=[200,1,2,3,4,5,6,7]
- #d = [0 for i in range(512)]
- a= []
- for i in range(512):
- a.append(i)#'''
- print(a)
- print(c)
- #senddata(2,d)
- req = struct.pack('8B', int('3F', 16), int('20', 16), int('63', 16), int('31', 16), int('0D', 16), int('0A', 16), int('0D', 16), int('0A', 16))
- #s.sendto((b"\x78\x78\x11\x01\x07\x52\x53\x36\x78\x90\x02\x42\x70\x00\x32\x01\x00\x05\x12\x79\x0D\x0A"),(network, PORT))
- #s.sendto(req, (network, PORT))
- s.sendto(formatdata(128,a), (network, PORT))
- #s.sendto(pack(), (network, PORT))
- time.sleep(1)
- '''
- byte[] sendBuf = new byte[530];//设置发送缓存区
-
- //41 72 74 2D 4E 65 74 00 00 50 00 0E 77 01
- //按照协议报文填充数据
-
- //固定格式
- sendBuf[0] = 0x41;
- sendBuf[1] = 0x72;
- sendBuf[2] = 0x74;
- sendBuf[3] = 0x2D;
- sendBuf[4] = 0x4E;
- sendBuf[5] = 0x65;
- sendBuf[6] = 0x74;
- sendBuf[7] = 0x00;
- sendBuf[8] = 0x00;
- sendBuf[9] = 0x50;
- sendBuf[10] = 0x00;
- sendBuf[11] = 0x0E;
- sendBuf[12] = 0x77;
- sendBuf[13] = 0x01;
-
- //设备节点
- if (Convert.ToInt16(textBox7.Text) > 0 && Convert.ToInt16(textBox7.Text) < 5)
- {
- sendBuf[14] = Convert.ToByte(Convert.ToInt16(textBox7.Text)-1);
- }
- else
- {
- MessageBox.Show("设备节点输入有误");
- }
- sendBuf[15] = 0x00;
-
- //发送的字节数
- sendBuf[16] = 0x02;
- sendBuf[17] = 0x00;
-
-
- //后面的字节就是DMX512对应的512个通道的数据了
-
-
- if (Convert.ToInt16(textBox1.Text) >= 1 && Convert.ToInt16(textBox1.Text) <= 512 && Convert.ToInt16(textBox2.Text) >= 0 && Convert.ToInt16(textBox2.Text) <= 255)
- {
- sendBuf[17 + Convert.ToInt16(textBox1.Text)] = Convert.ToByte(textBox2.Text);
- }
- else
- MessageBox.Show("通道或数值参数输入有误");
-
-
-
-
-
- '''
client端
- # udp_gb_client.py
- '''客户端(UDP协议局域网广播)'''
-
- import socket
-
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
-
- PORT = 6454
-
- s.bind(('127.0.0.1', PORT))
- print('Listening for broadcast at ', s.getsockname())
-
- while True:
- data, address = s.recvfrom(65535)
- print('Server received from {}:{}'.format(address, data))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。