tcp-socket-server.py 735 B

1234567891011121314151617181920212223242526
  1. import socket
  2. HOST = '192.168.51.102'
  3. PORT = 1
  4. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  6. s.bind((HOST, PORT))
  7. s.listen(5)
  8. print('server start at: %s:%s' % (HOST, PORT))
  9. print('wait for connection...')
  10. conn, addr = s.accept()
  11. print('connected by ' + str(addr))
  12. while True:
  13. outdata = input('please input message: ')
  14. conn.send(outdata.encode())
  15. # while True:
  16. indata = conn.recv(1024)
  17. # if len(indata) == 0: # connection closed
  18. # conn.close()
  19. # print('client closed connection.')
  20. # break
  21. print('recv: ' + indata.decode())
  22. indata = conn.recv(1024)
  23. print('recv: ' + indata.decode())