Python 3:服务器套接字未正确关闭,收到无限循环的空白消息

时间:2014-01-19 22:28:04

标签: sockets python-3.3

编辑:我自己解决了这个问题,如果你有类似的问题,请看第一个答案

我是套接字的新手,并做了一个客户端 - 服务器测试修改一个例子,它发送和接收数据确定但是当关闭服务器时陷入接收空白消息的循环中我无法找到原因即使我使用shutdown(socket.SHUT_RDWR)根据文档立即关闭连接。

我不得不为每一个(服务器和客户端)使用两个cmd的窗口,因为首先运行服务器处于空闲状态,然后客户端我在client_example中运行ConnectionRefusedError:[WinError 10061]因为目标机器无法建立连接这里主动拒绝它是代码。无论如何,这是我尝试的序列:

(在客户端控制台中):

c:\python33\python client_example.py
SEND( TYPE q or Q to Quit):Hi
SEND( TYPE q or Q to Quit):q

(在服务器控制台中):

c:\python33\python server_example3.py
TCPServer Waiting for client on port 7000
I got a connection from  ('127.0.0.1', 49263)
RECEIVED: b'Hi'
RECEIVED: b''
RECEIVED: b''
RECEIVED: b''
RECEIVED: b''
RECEIVED: b''
....                 <---After I did CTRL+C
Traceback (most recent call last):
  File "server_example.py", line 19, in <module>
    print("RECEIVED:",data)
  File "c:\python33\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
KeyboardInterrupt

以下是服务器和客户端的代码:

server_example.py:

#TCP server example
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 7000))
server_socket.listen(5)

print("TCPServer Waiting for client on port 7000")

while 1:
    client_socket, address = server_socket.accept()
    print("I got a connection from ", address)
    while 1:
        data = client_socket.recv(32)
        if (data == 'q' or data == 'Q'):            
            client_socket.shutdown(socket.SHUT_RDWR)
            client_socket.close()
            break;
        else:
            print("RECEIVED:",data)

client_example.py:

# TCP client example
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 7000))
while 1:
    data = input( "SEND( TYPE q or Q to Quit):" )
    if (data != 'Q' and data != 'q'):
        my_bytes = bytearray()
        for c in data:
            my_bytes.append(ord(c))
        client_socket.send(my_bytes)
    else:
        client_socket.shutdown(socket.SHUT_RDWR)
        client_socket.close()
        break;

3 个答案:

答案 0 :(得分:0)

嗯,当我无法解决问题2小时后,我在11分钟发布问题后解决了这个问题,我感到愚蠢......

问题是我正在将数据与字符串'q'进行比较,这在python 2中是正确的但在python 3中发送的是字节,b'q'&lt;&gt; 'q'......

以下是更正后的代码:

server_example.py

#TCP server example
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 7000))
server_socket.listen(5)

print("TCPServer Waiting for client on port 7000")

while 1:
    client_socket, address = server_socket.accept()
    print("I got a connection from ", address)
    while 1:
        data = client_socket.recv(32)
        print("RECEIVED:",data)
        if (data == b'q' or data == b'Q'):            
##          client_socket.shutdown(socket.SHUT_RDWR)
            client_socket.close()
            break;
    break;

client_example.py

# TCP client example
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 7000))
while 1:
    data = input( "SEND( TYPE q or Q to Quit):" )
    if (data != 'Q' and data != 'q'):
        my_bytes = bytearray()
        for c in data:
            my_bytes.append(ord(c))
        client_socket.send(my_bytes)
    else:
        my_bytes = bytearray()
        my_bytes.append(ord(data))
        client_socket.send(my_bytes)        
##      client_socket.shutdown(socket.SHUT_RDWR)
        client_socket.close()
        break;

答案 1 :(得分:0)

您假设您将获得Q.当您从阅读中获取EOS时,您也应该参与相同的处理,无论采用何种形式的Python。

答案 2 :(得分:0)

@EJP:EOS你的意思是流的结束?如果你是对的,这个例子是一个非常简单的开始,我忽略了这些细节。

无论如何,我发现在服务器中使用此检查已解决:

data = input( "SEND( TYPE q or Q to Quit):" )
if (data == b''):
    break;