tcp_server.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import socket
  2. import threading
  3. import json
  4. class TcpServer:
  5. def __init__(self):
  6. self.port = 2233 # 设置端口
  7. self.HEAD_LEN = 8
  8. self.tcpServerSocket = socket.socket() # 创建socket对象
  9. hostname = socket.gethostname() # 获取本地主机名
  10. sysinfo = socket.gethostbyname_ex(hostname)
  11. hostip = sysinfo[2][2]
  12. self.tcpServerSocket.setsockopt(
  13. socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 让端口可以复用
  14. # 将地址与套接字绑定,且套接字要求是从未被绑定过的
  15. self.tcpServerSocket.bind((hostip, self.port))
  16. self.tcpServerSocket.listen(5) # 代办事件中排队等待connect的最大数目
  17. def set_receive_listener(self, receive_listener):
  18. self.receive_listener = receive_listener
  19. def server(self):
  20. while True:
  21. print("等待连接")
  22. self.clientSocket, addr = self.tcpServerSocket.accept()
  23. print('连接地址:', addr)
  24. if self.connected_listener:
  25. self.connected_listener()
  26. while True:
  27. try:
  28. head_data = self.clientSocket.recv(self.HEAD_LEN)
  29. if not len(head_data) == 8:
  30. print("bad package!head_data len:", head_data)
  31. self.restart()
  32. return
  33. body_len = self.get_length_from_head_data(head_data)
  34. body_data = self.clientSocket.recv(body_len)
  35. if not body_len == len(body_data):
  36. print("bad package!body_len:", body_len)
  37. self.restart()
  38. return
  39. data_type = self.get_type_from_head_data(head_data)
  40. if data_type == 1: # test/json data
  41. text = body_data.decode()
  42. print(text)
  43. if not text:
  44. break
  45. if self.receive_listener:
  46. self.receive_listener(text)
  47. elif data_type == 2: # image data
  48. pass
  49. except ConnectionResetError:
  50. print("ConnectionResetError!")
  51. self.restart()
  52. return
  53. self.clientSocket.close() # 关闭连接
  54. self.tcpServerSocket.close()
  55. def get_length_from_head_data(self, head_data):
  56. if(not len(head_data) == 8):
  57. return
  58. ch1 = head_data[4] & 0x00FF
  59. ch2 = head_data[5] & 0x00FF
  60. ch3 = head_data[6] & 0x00FF
  61. ch4 = head_data[7] & 0x00FF
  62. return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0))
  63. def get_type_from_head_data(self, head_data):
  64. if(not len(head_data) == 8):
  65. return
  66. ch1 = head_data[0] & 0x00FF
  67. ch2 = head_data[1] & 0x00FF
  68. ch3 = head_data[2] & 0x00FF
  69. ch4 = head_data[3] & 0x00FF
  70. return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0))
  71. def start(self):
  72. self.server_threading = threading.Thread(target=self.server, args=())
  73. self.server_threading.start()
  74. def restart(self):
  75. self.start()
  76. def send_data(self, data):
  77. try:
  78. self.clientSocket.send(data)
  79. except ConnectionResetError:
  80. print("ConnectionResetError!")
  81. self.restart()
  82. def send_img(self, bytes_data):
  83. data = self.wrapper_data(2, bytes_data)
  84. self.send_data(data)
  85. def send_text(self, text):
  86. data = self.wrapper_data(1, text.encode())
  87. self.send_data(data)
  88. def wrapper_data(self, data_type, body_data):
  89. print("data_type:", data_type)
  90. print("body_data len:", len(body_data))
  91. type_bytes = data_type.to_bytes(4, 'big')
  92. print("type_bytes:", type_bytes)
  93. body_len = len(body_data)
  94. body_len_bytes = body_len.to_bytes(4, 'big')
  95. print("body_len_bytes:", body_len_bytes)
  96. head_data = type_bytes + body_len_bytes
  97. print("head_data:", head_data)
  98. data = head_data+body_data
  99. return data