Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8885141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:14:11+00:00 2026-06-14T21:14:11+00:00

I have an Android app that communicates over a TCP socket with a server

  • 0

I have an Android app that communicates over a TCP socket with a server I wrote. The method I’m using now to read and write output works fine for smaller strings (up to 60kB) but I get an exception thrown when the string is much longer than that. Here is the relevant part of what I have for the server and client:

Server:

            DataInputStream dis = null;
            DataOutputStream dos = null;

            try {
                    dis = new DataInputStream(server.getInputStream());
                    dos = new DataOutputStream(server.getOutputStream());

                    String input = "";
                    input = dis.readUTF();
                    handle_input info = new handle_input(input, id);
                    String xml = info.handle();

                    dos.writeUTF(xml);

                    server.close();

            } 

Client:

        Socket socket = null; 
        DataOutputStream dos = null;
        DataInputStream dis = null;
        Boolean result;

        try {
            socket = new Socket(ip, port);
            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());
            dos.writeUTF(the_text);
            String in = "";
            while (in.equals("")) {
                in += dis.readUTF();
            }
        }

How can I modify it to deal with potentially enormous Strings? I’ve been looking around and can’t seem to find a clear answer.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:14:12+00:00Added an answer on June 14, 2026 at 9:14 pm

    If you look at the javadoc for writeUTF(), it is obvious that the method can only handle strings with up to 65535 encoded bytes. (The encoded form starts with 2 bytes that give the string byte count).

    Your choices are:

    • send the String as an array of characters extracted using String.toCharArray(),
    • use String.getBytes(“UTF-8”) to UTF8-encode the string explicitly and send the resulting bytes, or
    • split it into smaller substrings, send them using writeUTF8, and splice them back together at the other end.

    All of these will entail preceding the actual data with a count to tell the other end how much to expect / read.

    The other approach is to send/receive the data as text using a Writer and Reader instead of DataOutputStream and DataInputStream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Android client app that communicates with server using Socket . On my
I have an Android app that communicates with a server via asynchronous socket connection
I am building an Android App that will communicate over TCP with a server.
I have an android app that listens for json commands over a socket. I
I have developed an Android App that communicates via HTTP-Request with a server. Some
I am trying to create an android app that communicates with a local server
I have an android app that posts to users wall by using the Facebook
I have an android application which communicates over plain tcp/ip (no http) connection with
I have an Android app that fetches data from the web service using ksoap2,
I have an Android app that in the onCreate() method, preloads a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.