amBiHTion 2018-07-15 14:34 采纳率: 0%
浏览 1358
已结题

Python3 socket 编写全双工通信遇到问题

#服务器

import socket
import select
import sys

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 5007              # Arbitrary non-privileged port
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSer.bind(ADDR)
tcpSer.listen(1)
inputs = [tcpSer, sys.stdin]

while True:
    print('Waiting for connection')
    tcpCli, addr = tcpSer.accept()
    print('...connected from:', addr)
    inputs.append(tcpCli)
    readable, writable, exceptional = select.select(inputs, [], [])

    while True:
        for indata in readable:
            if indata is tcpCli:
                data = tcpCli.recv(BUFSIZ)
                if not data:
                    break
            print('message received:', data.decode())
        else:
            message = input('>')
            tcpCli.send(message.encode('utf-8'))

    tcpCli.close()
tcpSer.close()

#客户端

import socket
import select
import sys

HOST = '172.23.214.95'    # The remote host
PORT = 5007              # The same port as used by the server
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSock.connect(ADDR)

while True:
    readable, writable, exceptional = select.select([tcpSock, sys.stdin], [], [])
    for indata in readable:
        if indata is tcpSock:
            message = tcpSock.recv(BUFSIZ)
            if not message:
                break
            print('Message received:', message.decode('utf-8'))
        else:
            message = input('>')
            if not message:
                break
            tcpSock.send(message.encode('utf-8'))

tcpSock.close()

运行报错:
Traceback (most recent call last):
File "E:/Python project/Python_Study/Client01.py", line 14, in
readable, writable, exceptional = select.select([tcpSock, sys.stdin], [], [])
OSError: [WinError 10038] 在一个非套接字上尝试了一个操作。

  • 写回答

3条回答

  • devmiao 2018-07-15 15:47
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误