0

运行此代码时不断出现索引超出范围错误我该如何解决?数据包含一些值,这些值将通过服务器传递给 valueList,我想在数据中使用这些值的一部分。例如, int(valueList[8]) 包含球的 x 坐标。我该如何解决这个问题?

import os,sys
import pygame
import socket
import time
from multiprocessing import Process, Value
from ctypes import c_bool

Host = '59.191.193.45'
Port = 5555

def updatePosition(valueList, update_valueList, quit_flag):
    while not quit_flag.value:
        print 'Ball x', int(valueList[8])
        print 'Ball y', int(valueList[9])
        update_valueList = valueList[:]
    print 'Closing child update process'

def activateRobot(update_valueList, quit_flag): 
    while not quit_flag.value:
        print 'Ball x', int(valueList[8])
        print 'Ball y', int(valueList[9])
        print 'turn to'
        print 'move to', int(valueList[8])
        print 'move to', int(valueList[9])
    print 'Closing child activate robot process'

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((Host,Port))

valueList = []
update_valueList = []
quit_flag = Value(c_bool,False)

update = Process(target = updatePosition, args=(valueList, update_valueList,quit_flag))
activate = Process(target = activateRobot, args=(update_valueList,quit_flag))

update.start()
activate.start()

while True:
    client.sendall("loc\n")
    data = client.recv(8192)
    if not data:
        print 'network connection close by client'
        break
    valueList = data.split()

print 'All done, closing child process'
update.join()
activate,join()
4

1 回答 1

3

这个

data = client.recv(8192)

最多接收8192 个字节。您可能会得到部分线条。该行不必长于 8192 字节即可。您只需阅读到目前为止通过网络到达的内容,这些内容可能不是整行。

于 2012-09-28T05:26:26.030 回答