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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:44:55+00:00 2026-05-22T15:44:55+00:00

After asking so many question about android socket programming and getting valuable answers from

  • 0

After asking so many question about android socket programming and getting valuable answers from stackoverflow’s members i could do a well working program using sockets to connect two device over wifi.
Thanks to all.
But still i am having some problem..

i have done the program in which
** Data can be sent from client and received at serverSocket**
But still i am not getting how to Send data from server which can be received at client

Code for Server Socket

private OnClickListener bt_sendListner = new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        String msg=et_msg.getText().toString();
        Log.d("Msg", msg);
        Thread threadsendmsg = new Thread(new Threadsendmsg(msg));
        threadsendmsg.start();

    }
};



public class Threadsendmsg implements Runnable{

    String msg;

    public Threadsendmsg(String msg) {
        // TODO Auto-generated constructor stub
      this.msg=msg;


    }

    public void run() {
        // TODO Auto-generated method stub
         try {

             Looper.prepare();
                Log.d("Msg", "Inside the thread");


             //connected = true; 
                while (true) {
                    try {

                        Log.d("Msg", "Msg to be sent");

                        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(serverSocket.accept().getOutputStream())), true);
                            // where you issue the commands
                            out.println("Client: "+msg);
                            Log.d("Msg", "Msg sent"+out.toString());

                            break;
                    } catch (final Exception e) {
                        handler.post(new Runnable() {

                            public void run() {
                                // TODO Auto-generated method stub
                    tv_chatbox.setText("S: Error= "+ e.getMessage());
                             Log.d("Msg", e.getMessage());   
                            }
                        });
                    }
                }
              //  socket.close();
              //  console.append("\nC: Closed.");
            } catch (final Exception e) {
                handler.post(new Runnable() {

                    public void run() {

                        // TODO Auto-generated method stub
                        tv_chatbox.setText("S: Error= "+ e.getMessage());
                                 Log.d("Msg", e.getMessage());


                        // TODO Auto-generated method stub
                //      console.append("\nC: Error= "+ e.getMessage());

                    }
                });

           //   connected = false;
            }



    }




}



public class ServerThread implements Runnable {

    public void run() { 
        try {
            Looper.prepare();
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                handler.post(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString()
                                , Toast.LENGTH_LONG).show();
                                serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString());

                    }
                });



                Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString()
                , Toast.LENGTH_LONG).show();
                serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString());
                while (true) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                     });

                    try { 
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        String line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);

                          final  String myline=new String(line);
                            handler.post(new Runnable() {
                                public void run() {
                                    tv_chatbox.setText("Client said:="+myline);

                                    // do whatever you want to the front end
                                    // this is where you can be creative
                                }
                            });
                        }
                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (final Exception e) {
            handler.post(new Runnable() {
                public void run() {
                    serverStatus.setText("Error"+e.getMessage());

                }
            });
            e.printStackTrace();
        }
    }
}

*There is no method as ServerSocket.getOutputStream() in ServerSocket class.*Which i have used client socket…

  • 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-22T15:44:56+00:00Added an answer on May 22, 2026 at 3:44 pm

    Both client and server use the same class Socket. But client creates his socket instance manually and connects to server. Server on the other hand listens at some port and when client connects, socket for server is created and returned from method accept().
    In your code you can use

    client.getOutputStream();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After asking this question and reading up a lot on templates, I am wondering
This question has been brought up many times, but I'd like to ask it
After getting fed up with c3p0's constant locking I'm turning to BoneCP for an
I've been using ComponentSoftware's CS-RCS Basic for many years now to manage my various
A co-worker of mine is taking an Introduction to Object Oriented Programming class and
I am porting PC game to iPad which has ton of graphic assets (over
I have a web application at work that is similar to a ticket working
Each product a product_date_added which is a Date field contained the date it was
I have two Java class hierarchies that share a common ancestor and implement a
I want to create a basic app where you have 8 numbered buttons, with

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.