I have a python server who's listening to a java client.
I want to send an array from java to python and respond by sending an array back.
I am listening from python like this:
INPUT_SPEC = {'rows': 5, 'columns': 2, 'dtype': np.dtype('float32')}
OUTPUT_SPEC = {'rows': 1, 'columns': 1, 'dtype': np.dtype('float32')}
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
nbytes = INPUT_SPEC['rows'] * INPUT_SPEC['columns'] * INPUT_SPEC['dtype'].itemsize
try:
print >>sys.stderr, 'connection from', client_address
data = connection.recv(nbytes)
a = np.fromstring(data, dtype = INPUT_SPEC['dtype']).reshape((INPUT_SPEC['rows'], INPUT_SPEC['columns']))
result = np.array(mysum(a), dtype = OUTPUT_SPEC['dtype']).reshape((OUTPUT_SPEC['rows'], OUTPUT_SPEC['columns']))
print >>sys.stderr, 'sending data back to the client: {0}'.format(result)
connection.sendall(result.tobytes())
except Exception as e:
print(e)
connection.close()
finally:
# Clean up the connection
connection.close()
And I am sending from java like this:
String hostName = "localhost";
int portNumber = 10000;
try (
//open a socket
Socket clientSocket = new Socket(hostName, portNumber);
BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
) {
System.out.println("Connected");
Double[][] test2 = new Double[5][2];
test2[1][1]= 0.1;
test2[1][0]= 0.2;
test2[2][1]= 0.2;
test2[2][0]= 0.2;
test2[3][1]= 0.1;
test2[3][0]= 0.2;
test2[4][1]= 0.2;
test2[4][0]= 0.2;
test2[0][1]= 0.2;
test2[0][0]= 0.2;
out.println(test2);
String response;
while ((response = in.readLine()) != null)
{
System.out.println( response );
}
}
The error message I get is:
string size must be a multiple of element size
So I must not be defining my sent data well. Any suggestions?