send_main.py 1021 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. from socket import *
  3. from time import ctime
  4. from AppendClient import AppendClient
  5. # 服务配置
  6. HOST = '172.16.102.9'
  7. PORT = 10087
  8. # HOST = '127.0.0.1'
  9. # PORT = 49900
  10. BUFSIZE = 81920
  11. ADDR = (HOST, PORT)
  12. userList = []
  13. MAXCLIENT = 2
  14. def loopServer(tcpSrvSock):
  15. # 服务监听
  16. global userList, MAXCLIENT
  17. while True:
  18. print('Waiting For Client Connection ...', ctime())
  19. tcpCliSock, addr = tcpSrvSock.accept() # 会阻塞
  20. print('NewUser', addr, ctime())
  21. print('UserListLenght:', len(userList))
  22. # 增加线程
  23. if tcpCliSock not in userList and len(userList) < MAXCLIENT:
  24. newClinet = AppendClient(userList, tcpCliSock, BUFSIZE, addr)
  25. newClinet.start()
  26. else:
  27. # 不允许连接
  28. print('不允许连接')
  29. tcpCliSock.close()
  30. if __name__ == '__main__':
  31. tcpSrvSock = socket(AF_INET, SOCK_STREAM)
  32. tcpSrvSock.bind(ADDR)
  33. tcpSrvSock.listen(1)
  34. loopServer(tcpSrvSock)