-1

I'm trying to send an array over TCP from a server-like script to a client-like one. The array is variable, so the data is sent using packets and then joined together at the client.

The data I'm trying to send is from the MNIST hand-written digits dataset for Deep Learning. The server-side code is:

tcp = '127.0.0.1'
port = 1234
buffer_size = 4096
(X_train, y_train), (X_test, y_test) = mnist.load_data()
test_data = (X_test, y_test)

# Client-side Deep Learning stuff

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((tcp, port))
x = pickle.dumps(test_data)
s.sendall(x)
s.close()

The client-side script loads a Neural Network that uses the test data to predict classes. The script for listening to said data is:

tcp = '127.0.0.1'
port = 1234
buffer_size = 4096

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((tcp, port))
print ('Listening...')
s.listen(1)

conn, addr = s.accept()
data_arr = []

while True:
    data_pack = conn.recv(buffer_size)
    if not data: break
    data_pack += data

my_pickle = b"".join(data_pack)
test_data = pickle.loads(my_pickle)
print ("Received: " + test_data)
conn.close()

# Irrelevant Deep Learning stuff...

The server sends the data without a hitch, but the client crashes when trying to join the packets received by the client (my_pickle = ...) with the following error:

TypeError: sequence item 0: expected a bytes-like object, int found

How should I format the join in order to recreate the data sent and use it for the rest of the script?

Meloku
  • 45
  • 1
  • 2
  • 9
  • Something seems to be missing in your server example code: you use `data`, yet `data` never gets assigned. –  Nov 28 '17 at 02:12
  • Just use `test_data = pickle.loads(data_pack)` and remove the `join` line: `data_pack` is already a bytes object, not a list. You can append to a byte string with `+=`, like you do here. –  Nov 28 '17 at 02:15

1 Answers1

0

I ended up using both Pickle and ZeroMQ to handle the comunication protocol. An advantage of this method is that I can send more than one data package.

On the client side:

ip = '127.0.0.1'
port = '1234'

# ZeroMQ context
context = zmq.Context()

# Setting up protocol (client)
sock = context.socket(zmq.REQ)
sock.bind('tcp://'+ip+':'+port)
print('Waiting for connection at tcp://'+ip+':'+port+'...')
sock.send(pickle.dumps(X_send))
X_answer = sock.recv()
sock.send(pickle.dumps(y_send))
print('Data sent. Waiting for classification...')
y_answer = sock.recv()
print('Done.')

And on the server side:

# ZeroMQ Context
context = zmq.Context()

# Setting up protocol (server)
sock = context.socket(zmq.REP)

ip = '127.0.0.1'
port = '1234'

sock.connect('tcp://'+ip+':'+port)
print('Listening to tcp://'+ip+':'+port+'...')

X_message = sock.recv()
X_test = pickle.loads(X_message)
sock.send(pickle.dumps(X_message))
y_message = sock.recv()
y_test = pickle.loads(y_message)
print('Data received. Starting classification...')

# Classification process

sock.send(pickle.dumps(y_message))
print('Done.')
Meloku
  • 45
  • 1
  • 2
  • 9