1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to find the size of an object in Java Android

Discussion in 'Android Development' started by 109th_Regiment, Sep 17, 2019.

  1. 109th_Regiment

    Thread Starter

    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.

    Code (Java):
    1. //java client
    2.            OutputStream output = client.getOutputStream();
    3.            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(output));
    4.  
    5.            byte[] msg;
    6.            if(strings[1].equals("swipe"))
    7.            {
    8.                out.write(strings[1].getBytes());
    9.                out.flush();
    10.                //strings[0] contains the movement information in format of:
    11.                //xmovement_ymovement
    12.                //for example: 2.5365_8.5478
    13.                msg = strings[0].getBytes();
    14.                out.write(msg);
    15.                out.flush();
    16.            }
    17.  
    Code (Text):
    1. #python server
    2.    #the 1024 is a temporary number I just put in there
    3.    #because I don't know how to find the size of the data in the java client side
    4.    request_byte = client_socket.recv(1024)
    5.    print("reqeust_byte size: " + str(sys.getsizeof(request_byte)))
    6.    request = request_byte.decode()
    7.    print("Received " + request)
    8.  
    9.    if request == "swipe":
    10.        movement_byte = client_socket.recv(1024)
    11.        print("movement_byte size: " + str(sys.getsizeof(movement_byte)))
    12.        movement_str = movement_byte.decode()
    13.        x_movement, y_movement = movement_str.split("_")
    14.        print("x: " + x_movement)
    15.        print("y: " + y_movement)
    16.        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?
     


Loading...
Similar Threads - find size object
  1. huhiha
    Replies:
    4
    Views:
    188
  2. Nightpoison
    Replies:
    1
    Views:
    303
  3. Kayem59
    Replies:
    4
    Views:
    284
  4. PolluxDevelopers
    Replies:
    0
    Views:
    224
  5. ennisdavis
    Replies:
    3
    Views:
    603
  6. Meek Bits
    Replies:
    0
    Views:
    243
  7. guriausa
    Replies:
    4
    Views:
    433
  8. Rgarner
    Replies:
    0
    Views:
    577
  9. BrainBrain02
    Replies:
    3
    Views:
    268
  10. startac4
    Replies:
    18
    Views:
    1,062

Share This Page

Loading...