7

差不多 2 天我仍然有同样的问题 - 客户端和服务器相互“对话”,但我不知道为什么在通信过程中突然出现问题。我尝试了很多东西,不幸的是仍然是同样的问题。

我在 Windows 7 上使用 python 2.7.5。

我的代码: cs_common.py

import socket
import os
import sys
import errno
from time import sleep


HOST = 'localhost'
MY_IP = socket.gethostbyname(socket.gethostname())
PORT = 50007
timeout_in_seconds = 2

def createSocket4server(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    s.listen(4)
    return s

def createSocket4Client(host, port, timeout_in_seconds=3):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    s.connect( (host, port) )
    print 'connected to %s port %s' % (host, port)
    return s

sent = 0
def sendToSocket(socket_, data): # to debug
    global sent
    print sent, ') Sending:', data
    socket_.send(data)
    sent += 1

received = 0
def recvFromSocket(socket_):
    global received
    print '>>>>>>>>>>Trying to receive'
    data = socket_.recv(1024)
    print received, ') Received:', data
    received += 1
    return data

def sendDataToAllPlayers(data_to_send, connections):
    for c in connections:
        sendToSocket(c[0], data_to_send)

def trySendingAsLongAsNotSent(socket_, data):
    while True:
            try:
                sendToSocket(socket_, data)
                break
            except socket.timeout:
                pass

def tryReceivingAsLongAsNotReceived(socket_):
    while True:
            try:
                data = recvFromSocket(socket_)
                return data
            except socket.timeout:
                pass

服务器.py:

from cs_common import *


server = createSocket4server(HOST, PORT)
server.setblocking(0) # 1 switch off blocking
server.settimeout(timeout_in_seconds)

connections = []

counter = 0
while counter<3:
    counter += 1
    try:
        c, addr = server.accept()
        print 'Got connection from', addr
        connections.append( [c, addr] )
    except socket.timeout:
        pass

if len(connections) == 0:
    print 'No connections!'
    exit()

number_of_players = len(connections)
print 'Connected with', number_of_players, 'players:'
print [ addr[1] for addr in connections ]

counter = 0
for c in connections:
        counter += 1
        number_of_the_player = counter
        initial_game_data = str(number_of_the_player) + ' rest of initial game data'

        sendToSocket(c[0], initial_game_data) # 2 sending initial game settings

sleep(1)
server.setblocking(1) # 3 switch on blocking

# MAIN LOOP #
while True:
    print 'LOOP___________________________'
    sendDataToAllPlayers('Synchronization data to players', connections) # 4 sending synchronization data to clients

    # doing some stuff

    for c in connections:
        print 'received from player:', recvFromSocket(c[0]) # 5 receiving synchronization data from clients

客户端.py:

from cs_common import *


server = createSocket4Client(HOST, PORT)
server.setblocking(0) # 1 switch off blocking
server.settimeout(timeout_in_seconds)

initial_game_data = tryReceivingAsLongAsNotReceived(server) # 2 getting initial game settings
print 'received initial_game_data from server:', initial_game_data

sleep(1)
server.setblocking(1) # 3 switch on blocking

# MAIN LOOP #
while True:
    print 'LOOP___________________________'
    sunchronizing_data = recvFromSocket(server) # 4 receive synchronization data from server
    print 'Received from server:', sunchronizing_data

    # doing some stuff

    sendToSocket(server, 'I was doing nothing during the frame')

当我运行上面的代码时:

客户端输出:

connected to localhost port 50007
>>>>>>>>>>Trying to receive
>>>>>>>>>>Trying to receive
>>>>>>>>>>Trying to receive
0 ) Received: 1 rest of initial game data
received initial_game_data from server: 1 rest of initial game data
LOOP___________________________
>>>>>>>>>>Trying to receive
1 ) Received: Synchronization data to players
Received from server: Synchronization data to players
0 ) Sending: I was doing nothing during the frame
LOOP___________________________
>>>>>>>>>>Trying to receive
Traceback (most recent call last):
  File ".\client.py", line 19, in <module>
    sunchronizing_data = recvFromSocket(server) # 4 receive synchronization data from server
  File "...\pygame_my\cs_common.py", line 38, in recvFromSocket
    data = socket_.recv(1024)
socket.error: [Errno 10053] An established connection was aborted by the softwar
e in your host machine

服务器输出:

Got connection from ('127.0.0.1', 55768)
Connected with 1 players:
[('127.0.0.1', 55768)]
0 ) Sending: 1 rest of initial game data
LOOP___________________________
1 ) Sending: Synchronization data to players
received from player: >>>>>>>>>>Trying to receive
Traceback (most recent call last):
  File ".\server.py", line 49, in <module>
    print 'received from player:', recvFromSocket(c[0]) # 5 receiving synchronization data from clients
  File "...\pygame_my\cs_common.py", line 38, in recvFromSocket
    data = socket_.recv(1024)
socket.error: [Errno 10035] A non-blocking socket operation could not be completed immediately

我还在另一台计算机上尝试了脚本-同样的问题。我还尝试停用非阻塞套接字 - 仍然存在问题

4

1 回答 1

7

您需要使子套接字阻塞。在使用“connections.append( [c, addr] )” 附加新连接之前,在服务器文件 (server.py) 中添加此调用“c.setblocking(1)”——如以下代码段。看起来子套接字正在从父服务器套接字继承非阻塞选项。通过此更改,我能够运行您的代码而不会出现任何错误。

    c, addr = server.accept()
    print 'Got connection from', addr
    c.setblocking(1) # Make it blocking.
    connections.append( [c, addr] )

另一种选择是在 cs_common.py 的 recvFromSocket() 函数中捕获 socket.error - 这是因为如果 recv() 超时是因为子套接字是非阻塞的,那么 recv() 调用将返回一个错误。由于您的代码没有处理它,因此您的应用程序遇到了问题。

和一个通用说明:如果有客户端将在 3 个客户端的初始列表之后加入,那么我建议使用单独的线程来处理传入连接或使用 select() 调用来接受服务器 fd 的读取事件 - - 服务器 fd 上的读取事件意味着有一个挂起的连接,我们应该调用一个 accept()。

于 2013-09-01T08:46:38.200 回答