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

  • SEARCH
  • Home
  • 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 7518411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:44:48+00:00 2026-05-30T01:44:48+00:00

I’m doing some socket work in Java, and am trying to encode the data

  • 0

I’m doing some socket work in Java, and am trying to encode the data it is sending.
That part is working correctly, but for some reason it doesn’t send the whole encoded String over the socket. It seems to send it in parts of 3.

The client performs a simple readLine() from the MyBufferedReader class (which is below), and the server sends it like this:

    private void sendFile(File f, String dest){ //The file to be sent and the destination
        System.out.println("Sending file started..");

        try {
            this.out.println("CMD MKFILE " + dest + "/" + f.getName());
            //TODO send the file
        }catch(Exception e){
            e.printStackTrace();
        }
    }

The client receives this:
CMD MKFILE C:\Users\Lolmewn\Documents\test/dir/n/linebrea and after another read k!.txt

The MyBufferedReader and MyPrintWriter classes look like this:

MyBufferedReader:

    @Override
    public String readLine() throws IOException {
        String read = super.readLine();
        System.out.println("UNDEC: " + read);
        try {
            System.out.println("DEC: " + decode(read));
            return decode(read);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return read;
    }

    public static String decode(String b) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes("UTF-8"));
        InputStream b64is = MimeUtility.decode(bais, "base64");
        byte[] tmp = new byte[b.length()];
        int n = b64is.read(tmp);
        byte[] res = new byte[n];
        System.arraycopy(tmp, 0, res, 0, n);
        return new String(res);
     }

and MyPrintWriter:

    private String hash(String x) {
        try {
            return encode(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return x;
    }

    public static String encode(String b) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream b64os = MimeUtility.encode(baos, "base64");
        b64os.write(b.getBytes("UTF-8"));
        b64os.close();
        return new String(baos.toByteArray());
     }

What’s happening and how can I fix this?

Please do note: I am doing async work on these Sockets, meaning I can’t just use a while(read != null) statement. That would cause other data not supposed to be there being there too.

  • 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-05-30T01:44:49+00:00Added an answer on May 30, 2026 at 1:44 am

    First this aside: If you are sending from multiple threads/clients then you should open one socket for each of them (like accept() does), that way messages from different clients cannot interleave one another.

    Now i am assuming only a single client per socket (reasonable):

    I can’t just use a while(read != null)

    You have to implement a simple protocol with which you can distinguish one message from the next, e.g. use a 0-byte if that can never be part of the data or prefix the message with the length of the message and send both; that way your server can distinguish one message from the next.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.