• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to find the size of an object in Java Android

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:
//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();
           }

Code:
#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 https://stackoverflow.com/questions/26641840/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 https://stackoverflow.com/questions...-way-to-determine-the-size-of-an-object#52682 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?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones