服务器客户端操作系统之间的连接错误

时间:2019-10-29 18:08:02

标签: python-3.x

您好,我是套接字的新手,我试图使用套接字在本地计算机内部建立连接。 这是服务器

import socket

def server():
  host = socket.gethostname()   # get local machine name
  port = 8080  # Make sure it's within the > 1024 $$ <65535 range

  s = socket.socket()
  s.bind(('192.168.56.1', port))

  s.listen(1)
  client_socket, adress = s.accept()
  print("Connection from: " + str(adress))
  while True:
    data = s.recv(1024).decode('utf-8')
    if not data:
      breakpoint
    print('From online user: ' + data)
    data = data.upper()
    s.send(data.encode('utf-8'))
  s.close()

if __name__ == '__main__':
    server()

这是客户

import socket


def client():
  host = socket.gethostname()  # get local machine name
  port = 8080  # Make sure it's within the > 1024 $$ <65535 range

  s = socket.socket()
  s.connect(("192.168.56.1", port))

  message = input('-> ')
  while message != 'q':
    s.send(message.encode('utf-8'))
    data = s.recv(1024).decode('utf-8')
    print('Received from server: ' + data)
    message = input('==> ')
  s.close()

if __name__ == '__main__':
  client()

我知道其中没有多余的线路,但是在连接完成后我会清理它。

0 个答案:

没有答案