差不多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
server.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
client.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
我还尝试了另一台计算机上的脚本 - 同样的问题。我也尝试停用非阻塞套接字 - 仍然存在问题
答案 0 :(得分: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个cliens的初始列表之后加入,那么我建议使用单独的线程来处理传入连接或使用select()调用来接受读取事件server fd - 服务器fd上的读取事件意味着存在挂起连接,我们应该调用accept()。