0

Keeps on getting index out of range error when I run this code how do i solve this? The data contains a few values that would be passed down to the valueList through a server and i would like to use a part of those values in the data. For example , int(valueList[8]) contains the x coordinate of the ball. how do I solve this issue?

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

This

data = client.recv(8192)

receives up to 8192 bytes. You are probably getting partial lines. The line doesn't have to be longer than 8192 bytes for this to happen. You just get to read what has arrived over the network so far which might not be whole lines.

于 2012-09-28T05:26:26.030 に答える