当前位置:   article > 正文

python 简单的udp发送和接收_python s.send(b'\x01')

python s.send(b'\x01')

server端 

  1. # udp_gb_server.py
  2. '''服务端(UDP协议局域网广播)'''
  3. import socket,time,struct
  4. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  5. s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  6. PORT = 6454
  7. network ='127.0.0.1'# '<broadcast>'
  8. s.sendto('Client broadcast message!'.encode('utf-8'), (network, PORT))
  9. def formatdata(dvcid,dat):
  10. sendBuf=[0x41, 0x72, 0x74, 0x2D, 0x4E, 0x65, 0x74, 0x00, 0x00, 0x50, 0x00, 0x0E, 0x77, 0x01]
  11. #sendBuf[14]设备节点
  12. #sendBuf[15] = 0x00;
  13. sendBuf.append(dvcid& 0xff)
  14. sendBuf.append(0x00)
  15. # 发送的字节数 512
  16. #sendBuf[16] = 0x02;
  17. #sendBuf[17] = 0x00;
  18. datlen = len(dat)
  19. sendBuf.append((datlen & 0xff00) >> 8)
  20. sendBuf.append(datlen & 0xff)
  21. # 发送的数据
  22. #sendBuf[17+n] = 0x00;
  23. print( datlen)
  24. for x in range(datlen):
  25. pass
  26. sendBuf.append((dat[x] & 0xff))#.to_bytes(1,'big')
  27. #data = [1, 2, 3]
  28. #buffer = struct.pack("!ihb", *data)
  29. packstyle=str(18+datlen)+'B'#B 0-255
  30. #req = struct.pack('14b',*sendBuf) 0-127
  31. req = struct.pack(packstyle, *sendBuf)
  32. return req
  33. import struct
  34. from ctypes import create_string_buffer
  35. def pack():
  36. # 创建一个9字节大小的缓存区,初始化默认全部为\x00 
  37. buf = create_string_buffer(9)# buf.raw: '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  38. # 冲缓存区buf的第0个字节开始打包两个4字节无符号整型数据12
  39. struct.pack_into(">II", buf, 0, 1, 2)#  buf.raw: '\x00\x00\x00\x01\x00\x00\x00\x02\x00'
  40. # 然后我们想再打包一个布尔型数据到buf中就可以改变以下偏移量
  41. struct.pack_into(">?", buf, 8, True)# buf.raw: '\x00\x00\x00\x01\x00\x00\x00\x02\x01'
  42. return buf
  43. c=0
  44. while True:
  45. c=c+1
  46. d=[200,1,2,3,4,5,6,7]
  47. #d = [0 for i in range(512)]
  48. a= []
  49. for i in range(512):
  50. a.append(i)#'''
  51. print(a)
  52. print(c)
  53. #senddata(2,d)
  54. 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))
  55. #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))
  56. #s.sendto(req, (network, PORT))
  57. s.sendto(formatdata(128,a), (network, PORT))
  58. #s.sendto(pack(), (network, PORT))
  59. time.sleep(1)
  60. '''
  61. byte[] sendBuf = new byte[530];//设置发送缓存区
  62. //41 72 74 2D 4E 65 74 00 00 50 00 0E 77 01
  63. //按照协议报文填充数据
  64. //固定格式
  65. sendBuf[0] = 0x41;
  66. sendBuf[1] = 0x72;
  67. sendBuf[2] = 0x74;
  68. sendBuf[3] = 0x2D;
  69. sendBuf[4] = 0x4E;
  70. sendBuf[5] = 0x65;
  71. sendBuf[6] = 0x74;
  72. sendBuf[7] = 0x00;
  73. sendBuf[8] = 0x00;
  74. sendBuf[9] = 0x50;
  75. sendBuf[10] = 0x00;
  76. sendBuf[11] = 0x0E;
  77. sendBuf[12] = 0x77;
  78. sendBuf[13] = 0x01;
  79. //设备节点
  80. if (Convert.ToInt16(textBox7.Text) > 0 && Convert.ToInt16(textBox7.Text) < 5)
  81. {
  82. sendBuf[14] = Convert.ToByte(Convert.ToInt16(textBox7.Text)-1);
  83. }
  84. else
  85. {
  86. MessageBox.Show("设备节点输入有误");
  87. }
  88. sendBuf[15] = 0x00;
  89. //发送的字节数
  90. sendBuf[16] = 0x02;
  91. sendBuf[17] = 0x00;
  92. //后面的字节就是DMX512对应的512个通道的数据了
  93. if (Convert.ToInt16(textBox1.Text) >= 1 && Convert.ToInt16(textBox1.Text) <= 512 && Convert.ToInt16(textBox2.Text) >= 0 && Convert.ToInt16(textBox2.Text) <= 255)
  94. {
  95. sendBuf[17 + Convert.ToInt16(textBox1.Text)] = Convert.ToByte(textBox2.Text);
  96. }
  97. else
  98. MessageBox.Show("通道或数值参数输入有误");
  99. '''

client端

  1. # udp_gb_client.py
  2. '''客户端(UDP协议局域网广播)'''
  3. import socket
  4. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  5. s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  6. PORT = 6454
  7. s.bind(('127.0.0.1', PORT))
  8. print('Listening for broadcast at ', s.getsockname())
  9. while True:
  10. data, address = s.recvfrom(65535)
  11. print('Server received from {}:{}'.format(address, data))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/218238
推荐阅读
相关标签
  

闽ICP备14008679号