从客户端接收返回数据

receiving back data from the client

我使用从服务器到客户端的套接字连接发送了一个缩放数组,它运行良好,现在我想将数据发回服务器以在服务器中取消缩放。数据一次每行发送到客户端,因此我尝试将它们按顺序放回一个名为 final 的空数组中。

这是server.py

import socket
import numpy as np
import pandas as pd
import sklearn
from sklearn.preprocessing import MinMaxScaler

i = 0
scaler_ti = MinMaxScaler()
test_inputs = []
test_inputs = np.array(test_inputs)
temp_in = pd.read_excel(r'K:\BachelorThesis\Data\TestingData\Mix_Data_inputs.xlsx')
test_inputs = temp_in.to_numpy()
rows = test_inputs.shape[0]
scaler_ti.fit(test_inputs)
normalized_test_inputs = scaler_ti.transform(test_inputs)


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 62402
s.bind((host,port))
s.listen(5)

while True:

    connection, clientsocket, address = s.accept()
    print(f"connection from {address} has been established!")
    strg = test_inputs
    temp = strg.tobytes()
    clientsocket.send(temp)
    clientsocket.close()

    if i in range(65533):
        i = i + 1
        msg = connection.recv(64)
        out = np.frombuffer(msg)
        inverse = scaler_ti.inverse_transform(out.reshape(1,8))
        print(inverse)

这是client.py

import socket
import numpy as np
import pandas as pd
import sklearn
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import load_model
import tensorflow as tf
from random import randint

i = 0
final = []
final = np.array(final)
#modelLSTM = load_model('K:\BachelorThesis\code testing\TireForces.LSTM/LSTM_model.h5')

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 62402
s.connect((host, port))

while True:
    if i in range(65533):
        i = i + 1
        msg = s.recv(64)
        out = np.frombuffer(msg)
        #out = out.reshape(1,8)
        #out = out.reshape(1,1,8)
        #prediction = modelLSTM.predict(out)
        #inverse = scaler_ti.inverse_transform(prediction.reshape(1,8))
        #print(prediction)
        #print(inverse)
        final = np.vstack(out) 
        print(out)
        if len(msg) <= 0:
             break
    strg = final
    temp = strg.tobytes()
    s.send(temp)
    #serversocket.close()
#print (final)

这是我从 server.py

得到的错误
Traceback (most recent call last):
  File "K:\BachelorThesis\code testing\server.py", line 26, in <module>
    connection, clientsocket, address = s.accept()
ValueError: not enough values to unpack (expected 3, got 2)

这是我从 client.py

得到的错误
Traceback (most recent call last):
  File "K:\BachelorThesis\code testing\client.py", line 31, in <module>
    final = np.vstack(out)
  File "<__array_function__ internals>", line 5, in vstack
  File "C:\Users\karim\AppData\Local\Programs\Python\Python39\lib\site- 
packages\numpy\core\shape_base.py", line 283, in vstack
    return _nx.concatenate(arrs, 0)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: need at least one array to concatenate

您的主要问题是 accept() 总是只发送两个值,但您期望发送三个值。

应该是

 connection, address = s.accept()

你应该使用 connection 而不是 clientsocket


但它会带来其他问题,因为您在发送数据后关闭 clientsocket 但您还需要此连接来接收数据。

这一切看起来就像你加入了两个代码。这些代码可以单独工作,但不能一起工作——因为两者都需要关闭连接以通知这是数据结束,但现在您不能在发送后关闭它,因为您需要连接才能接收其他数据。

你必须使用不同的方式来通知对方这是数据结束。您必须首先发送数据大小(作为具有恒定大小的对象,因此作为字符串发送将不起作用,因为对于不同的数字它可能具有不同的长度)然后发送数据。然后另一端必须首先获取数据大小(作为具有恒定大小的对象),然后使用这个值来检测它是否获取所有数据。

您可以使用 structinteger 大小转换为 4 个字节(因此对于不同的值它将具有恒定的大小)并且另一端将必须读取 4 个字节并再次使用 struct 转换回 integer

我不能 运行 但这是代码。

服务器:

import socket
import struct

import numpy as np
import pandas as pd
import sklearn
from sklearn.preprocessing import MinMaxScaler

# --- functions ---

def send_data(connection, data):
    data_size = len(data)
    data_size_as_4_bytes = struct.pack('>I', data_size)
    
    connection.send(data_size_as_4_bytes)    
    connection.send(data)

def recv_data(connection, chunk_size=64):
    data_size_as_4_bytes = connection.recv(4)
    data_size = struct.unpack('>I', data_size_as_4_bytes)[0]
    
    data = b""
    size = 0

    while size < data_size:
        chunk = connection.recv(chunk_size)
        size += len(chunk)
        data += chunk
    
    return data

# --- main ---

scaler_ti = MinMaxScaler()

temp_in = pd.read_excel(r'K:\BachelorThesis\Data\TestingData\Mix_Data_inputs.xlsx')

test_inputs = temp_in.to_numpy()
rows = test_inputs.shape[0]

scaler_ti.fit(test_inputs)
normalized_test_inputs = scaler_ti.transform(test_inputs)

# -- send ---

HOST = ''  # or '0.0.0.0'
PORT = 62402

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket()  # default values are `socket.AF_INET, socket.SOCK_STREAM`
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # solution for '[Error 89] Address already in use'. Use before bind()
s.bind((HOST, PORT))
s.listen(5)

try:
    while True:
        print('Waiting for client')

        # wait for new client
        connection, address = s.accept()
        print(f"connection from {address} has been established!")
        
        # --- send data ---
        
        print('send:', test_inputs)
        
        data = test_inputs.tobytes()
        send_data(connection, data)
        
        # don't close it because it is needed to receive data
        #clientsocket.close()

        # --- receive data ---

        data = recv_data(connection)
        output_data = np.frombuffer(data)
        
        print('recv:', output_data)

        # --- now you can close ---

        connection.close()
        
except KeyboardInterrupt:
    print("Stopped by Ctrl+C")
finally:
    s.close()
        

客户:

import socket
import struct

import numpy as np
import pandas as pd
import sklearn
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import load_model
import tensorflow as tf
from random import randint

# --- functions ---

def send_data(connection, data):
    data_size = len(data)
    data_size_as_4_bytes = struct.pack('>I', data_size)
    
    connection.send(data_size_as_4_bytes)    
    connection.send(data)

def recv_data(connection, chunk_size=64):
    data_size_as_4_bytes = connection.recv(4)
    data_size = struct.unpack('>I', data_size_as_4_bytes)[0]
    
    data = b""
    size = 0

    while size < data_size:
        chunk = connection.recv(chunk_size)
        size += len(chunk)
        data += chunk
    
    return data

def some_calcuations(input_data)

    # need something different
    output_data = input_data
    
    return output_data

# --- main ---

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 62402
s.connect((host, port))

# --- recv ---

data = recv_data(s)
input_data = np.frombuffer(msg)

print('recv:', input_data)

# --- calculations ---

output_data = some_calcuations(input_data)

# --- send ---

print('send:', output_data)

data = output_data.tobytes()
send_data(s, data)

# --- close ---

s.close()

顺便说一句:

上周有一个类似的问题,我展示了工作代码 - 它也同时为许多客户端使用 threading 到 运行 服务器。

GUI为运行nig:
时使用套接字发送图像(截图)

使用socket发送pickle:

在服务器线程中使用套接字以与许多客户端一起工作:


编辑:

循环发送版本

在所有行之后,它发送单词 end 以通知它是数据的结尾。

或者它可以在数据之前发送行数。

服务器:

import socket
import numpy as np

import struct

# --- functions ---

def send_data(connection, data):
    data_size = len(data)
    data_size_as_4_bytes = struct.pack('>I', data_size)
    
    connection.send(data_size_as_4_bytes)    
    connection.send(data)

def recv_data(connection, chunk_size=64):
    data_size_as_4_bytes = connection.recv(4)
    data_size = struct.unpack('>I', data_size_as_4_bytes)[0]
    
    data = b""
    size = 0

    while size < data_size:
        chunk = connection.recv(chunk_size)
        size += len(chunk)
        data += chunk
    
    return data

# --- main ---

np.random.seed(0)  # it will always gives the same random numbers
test_inputs = np.random.random_sample((3,5))

# -- send ---

HOST = ''  # or '0.0.0.0'
PORT = 62402

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket()  # default values are `socket.AF_INET, socket.SOCK_STREAM`
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # solution for '[Error 89] Address already in use'. Use before bind()
s.bind((HOST, PORT))
s.listen(5)

try:
    while True:
        # --- wait for new client ---

        print('Waiting for client')
        
        connection, address = s.accept()
        print(f"Connection from {address} has been established!")

        # --- send ---
        
        for row in test_inputs:
        
            # --- send data ---
            
            print('send:', row)
            
            data = row.tobytes()
            send_data(connection, data)

            # --- receive data ---

            data = recv_data(connection)
            row = np.frombuffer(data)
            
            print('recv:', row)

        # information that it is end of data 
        send_data(connection, 'end'.encode())

        # --- now you can close ---
        
        connection.close()
        
except KeyboardInterrupt:
    print("Stopped by Ctrl+C")
finally:
    s.close()

客户:


# author: Bartlomiej "furas" Burek (https://blog.furas.pl)
# date: 2021.07.23
#
# title: receiving back data from the client
# url: 

import socket
import numpy as np
from random import randint

import struct

# --- functions ---

def send_data(connection, data):
    data_size = len(data)
    data_size_as_4_bytes = struct.pack('>I', data_size)
    
    connection.send(data_size_as_4_bytes)    
    connection.send(data)

def recv_data(connection, chunk_size=64):
    data_size_as_4_bytes = connection.recv(4)
    data_size = struct.unpack('>I', data_size_as_4_bytes)[0]
    
    data = b""
    size = 0

    while size < data_size:
        chunk = connection.recv(chunk_size)
        size += len(chunk)
        data += chunk
    
    return data

def some_calcuations(input_data):

    # need something different
    output_data = input_data
    
    return output_data

# --- main ---

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 62402
s.connect((host, port))

while True:
    # --- recv ---

    data = recv_data(s)

    if data == b'end':
        break

    input_data = np.frombuffer(data)

    print('recv:', input_data)

    # --- calculations ---

    output_data = some_calcuations(input_data)

    # --- send ---

    print('send:', output_data)
    
    data = output_data.tobytes()
    send_data(s, data)

# --- close ---

s.close()