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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:22:40+00:00 2026-06-08T08:22:40+00:00

I would like to establish a client server connection between my phone and PC,

  • 0

I would like to establish a client server connection between my phone and PC,

package com.example.tcptest;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    TCPClient tcpclient;
    Boolean isconnected = false;
    Button connectBtn;
    Button sendBtn;
    TextView ipport;
    TextView sendtext;
    TextView rcvtext;
    Timer timer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Buttons
        connectBtn=(Button)findViewById(R.id.connectbtn);
        sendBtn=(Button)findViewById(R.id.sendbtn);

        ipport = (TextView)findViewById(R.id.serverip);
        sendtext = (TextView)findViewById(R.id.sendtxt);
        rcvtext = (TextView)findViewById(R.id.console);      

        connectBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {   
                //String[] i_po = ((String)ipport.getText()).split(":");
                //tcpclint = new TCPClient(i_po[0], Integer.parseInt(i_po[1]));         

                Thread cThread = new Thread(new ClientThread());
                cThread.start();                
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public class ClientThread implements Runnable {     
        public void run() {
            tcpclient = new TCPClient("192.168.0.101", 5000);
            tcpclient.connect();
            tcpclient.send("hello server");
            rcvtext.setText(tcpclient.getdata());           
        }
    }

}

In this case i can’t change the text of the rcvtext because it’s running in thread, is there any possibilities to change the content of the rcvtext, if the connection was successful than I would like to write log to the rcvtext?

  • 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-08T08:22:41+00:00Added an answer on June 8, 2026 at 8:22 am

    Within your ClientThread‘s run() method, implement this:

    MainActivity.this.runOnUiThread(new Runnable() {
        public void run() {
            rcvtext.setText(tcpclient.getdata());
        }
    });
    

    That tells Android to run it on the UI thread (the main application thread) whenever it can, rather than the current thread.

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

Sidebar

Related Questions

On SignalR .NET, we establish a connection as below between the client and server:
Even if UDP is connection-less, I would like to establish some kind of connection
When I establish a socket connection to a server, both client and server have
I would like to use the HTTPS to secure the communication between my client
I would like to know how to establish the link between eclipse - tomcat
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to create a dummy application for connecting my Android phone to
We are currently using MICO to establish the communication between a server and a
I would like to know the proper way to handle a new connection from
Fundamentally, I would like to establish a callback to an Activity from an IntentService.

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.