我可以在客户端读取数组。当我准备通过套接字将数组发送到客户端时,它只打印数组中的第一个元素,并且它与增量数量相关联。我已经成功打印出阵列了。然后我有数据变量,打印出通过套接字发送的数组。但它只接受第一个元素和字符串
客户端:
from socket import *
import time
from datetime import datetime
##############################
array = ['January', 'Feburary', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December']
##############################
TCP_IP = 'localhost'
TCP_PORT = 29004
start_time = time.time()
##############################
clientSocket = socket(AF_INET, SOCK_STREAM)
print ('Socket created: ', TCP_IP)
clientSocket.settimeout(2.0)
clientSocket.connect((TCP_IP, TCP_PORT))
print('Connected to Port: ', TCP_PORT)
###############################
a=0
b=0
#a=a+1
data_string = array[a] + " "+ str(a)
#a = a+1
#################
#Code now Prints the array in order.
#While loop, gets through array
while a < len(array):
#while a < len(data_string):
#print('Sender sent a message: ', data_string)
print('Sender sent a message: ', array[a])
a += 1
#clientSocket.send(MESSAGE)
clientSocket.send(data_string)
data = clientSocket.recv(1024)
##########################
# Code does not print all the months. It's stuck on the first element.
#Seconds it took to execute
while b < len(data):
print ("Data from Server: ", data + " Time: %s seconds" % (time.time() - start_time) )
b += 1
clientSocket.close()
服务器:
from socket import *
import time
##########################
TCP_PORT = 29004
b = 0
clientSocket = socket(AF_INET, SOCK_STREAM)
print('Socket created.')
clientSocket.bind(('',TCP_PORT))
clientSocket.listen(1)
print('Server is ready')
##########################
while 1:
connectionSocket, addr = clientSocket.accept()
message = connectionSocket.recv(1024)
while b < len(message):
print('Receving data: ', message)
b += 1
clientMessage = message
#clientMessage = message.upper()
connectionSocket.send(clientMessage)
connectionSocket.close()
#my_array = pickle.loads(data_string)