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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:10:34+00:00 2026-06-17T16:10:34+00:00

i have a sockets program in android, i want to send a command or

  • 0

i have a sockets program in android, i want to send a command or text message by clicking button on the client side. and send a different text message depending on which button is pressed. but i am getting the complier error message about the “out” PrintWriter object from
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);

if i use this “out” inside of an onclick listener, how do i fix this compiler error? I don’t really want to make this a final variable as recommended by the compiler as a fix.

here is the code shown below with the two onclick button listeners inside of the run() method for sending massage by socket from client to server android app

if you click the sendA button it sends the text message “testA” and if you click the sendB button it sends the text message “testB”

 public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, 8080);
            connected = true;
            while (connected) {
                try {
                    Log.d("ClientActivity", "C: Sending command.");
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                .getOutputStream())), true);



                    sendA.setOnClickListener(new View.OnClickListener() {


                    public void onClick(View v) {

                        out.println("testA");

                    }
                });

                    sendB.setOnClickListener(new View.OnClickListener() {


                        public void onClick(View v) {

                            out.println("testB");  

                        }
                    });

                    // where you issue the commands
                       // out.println("testX");


                        Log.d("ClientActivity", "C: Sent.");
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }
            }
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}
  • 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-17T16:10:35+00:00Added an answer on June 17, 2026 at 4:10 pm

    Putting final will clear your error.

    final PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                .getOutputStream())), true);
    

    But like you said you dont want final, then why not declare out as a global variable?

    // global variable
    PrintWriter out;
    
    .......
    
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                    .getOutputStream())), true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In sockets I have written the client server program. First I tried to send
I have a client server program using sockets, one android tablet is server and
I'm writing a program that will have both a server side and a client
I have a client-server program, on the server side: sockListen = socket(PF_INET, SOCK_STREAM, 0);
I have a simple program for server/client communication using sockets. The server class contains
I have an application using sockets.. a client and server program like mySql.. I
i have a question about sockets/clients.... I just finished writing a client server program
In my Java Sockets program, I have implemented a client-server Observer pattern. That is,
I have the following program to open lot's of sockets, and hold them open
I have a program I want to profile with gprof. The problem (seemingly) is

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.