I'm a newbie in android and python, and I'm trying to establish a TCP socket connection between my android client and a python server. Every time I swipe in my app, I send the swipe movement's direction to the server.
//java client
OutputStream output = client.getOutputStream();
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(output));
byte[] msg;
if(strings[1].equals("swipe"))
{
out.write(strings[1].getBytes());
out.flush();
//strings[0] contains the movement information in format of:
//xmovement_ymovement
//for example: 2.5365_8.5478
msg = strings[0].getBytes();
out.write(msg);
out.flush();
}
#python server
#the 1024 is a temporary number I just put in there
#because I don't know how to find the size of the data in the java client side
request_byte = client_socket.recv(1024)
print("reqeust_byte size: " + str(sys.getsizeof(request_byte)))
request = request_byte.decode()
print("Received " + request)
if request == "swipe":
movement_byte = client_socket.recv(1024)
print("movement_byte size: " + str(sys.getsizeof(movement_byte)))
movement_str = movement_byte.decode()
x_movement, y_movement = movement_str.split("_")
print("x: " + x_movement)
print("y: " + y_movement)
swipe(float(x_movement), float(y_movement))
When a simple button press is done and a single data is sent, the code works fine, but when I swipe and a lot of data is being sent in a short amount of time to the server, the data gets mixed up and turns unpredictable.
Now, as this post Receiving end of socket splits data when printed says, I know that I need to build a protocol that delineates messages, but on the Java client side, I can't get the size of the data I'm trying to send, so I'm stuck here.
This post In Java, what is the best way to determine the size of an object? says I can find size of objects by using java.lang.instrument package, but I can't import java.lang.instrument.Instrumentation in android studio. Is there another way to find the size of an object in android studio?