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 7919225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:47:45+00:00 2026-06-03T15:47:45+00:00

I want to send a message from the server to the client and found

  • 0

I want to send a message from the server to the client and found several problems:

  • if sent by out.flush(); method then the client can read it, but
  • when I use the method out.write(); then the client receives the status of -1 (not receiving data)

What should I add so that the client can receive messages with the method in nmbr 2. The following is the method I use on the client side to receive the message from server:

    int totalBytesRcvd = 0; // Total bytes received
    int bytesRcvd; // Bytes received in last read
    byte[] byteBufferreceive = new byte[256];
    while (totalBytesRcvd < byteBufferreceive.length) {
        if ((bytesRcvd = in.read(byteBufferreceive, totalBytesRcvd, 
    byteBufferreceive.length - totalBytesRcvd)) == -1)
        {
        Toast.makeText(this, "byte lenght: "+bytesRcvd, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "communication error", Toast.LENGTH_LONG).show();
        }
        totalBytesRcvd += bytesRcvd;            
    }
    System.out.println("The length of the received data "+totalBytesRcvd);
    String receive;
    receive = new String(byteBufferreceive);
    Toast.makeText(this, receive, Toast.LENGTH_LONG).show(); 

at client side i use this:

    DataInputStream in = new DataInputStream(clientsock.getInputStream());

    DataOutputStream out = new DataOutputStream(clientsock.getOutputStream());

at server side i use this:

        in  = new BufferedReader(
              new InputStreamReader(socket.getInputStream()));

        out = new PrintWriter(
              new OutputStreamWriter(socket.getOutputStream()));
  • 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-03T15:47:48+00:00Added an answer on June 3, 2026 at 3:47 pm

    try this:

    out = new PrintWriter(socket.getOutpustStream(),true)
    

    and then use out.println() to send a line across?

    By default print streams dont flush() after write() calls due to performance reasons.

    So if you dont want to have to use flush directly you have to use one of the following “println, printf, or format ” plus the constructor that enables auto flushing

    http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html

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

Sidebar

Related Questions

Let's say I want to send a message from one client to another. How
I want to send a message from a server to all clients. There are
I want to send an SMS message from my facebook application so that the
I want to ask, why I cannot transfer file from server to client? When
This question uses the code for server.c and client.c found here. I want a
I want to send a message to my computer from my phone using TCP..My
I'm trying to type-check the commands I want to send to a server from
Can a J2ME app be triggered by a message from a remote web server.
I want to send messages from a client computer to a consumer on a
I want to send from the server (in java) two messages: send: 0x01 Sleep:

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.