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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:21:05+00:00 2026-05-20T01:21:05+00:00

Well, i have a task to write Tcp Client for Andoid on java. Before,

  • 0

Well, i have a task to write Tcp Client for Andoid on java. Before, i have only work with C++ & Qt, it was easy =)

Now i tried to find out the best way to solve this task.
I want to devide Client on two thread.
The first one will be waiting and handling requests from server, on the second i will have GUI or ill can sending some requests to server (not matter).

Client will be connecting to the server and waiting for the some updating. On the server we have DB which can be edited by client. We must notify about changes on server definite group of users and they can do smth after.

Do you think is ok to devide it, or there are more good way? And how it can be implemented more clear.

ps guys, i have a some practice with Tcp-Ip, i dont needed on basic tutorials in this area! And i’m ok with Java. I need help just with architecture for building Client. Please, my question is strongly definite.

pps really sorry if it was hard to understand 😉

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

    First your question : is it ok to divide your client into two threads ?

    Well it depends on the kind of application you are running. If your client is interacting with the server along well defined client-server cases, it could be enough to use a same thread to send a message to the server and then block while waiting for the answer.

    However, this makes your program fairly not flexible, and if the server or the connection do not behave as they should, you will get a freeze of your ui at least.

    So it is IMHO rather necessary to use several threads for such communication handling. One thread will handle the receiving of messages and its processing, while the ui thread will call message sending.

    Here is a simple implementation which you can adapt to your case easily :

    package comm;
    
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    
    public class TcpClient {
    
        private InetAddress connectedAddress;
        private Socket tcpSocket;
        private BufferedReader inBuff;
        private PrintWriter outPrint;
        private int connectedPort;
    
        public TcpClient(String host, int port){
            try {
                connectedAddress = Inet4Address.getByName(host);
                connectedPort = port;
                this.tcpSocket = new Socket(connectedAddress, connectedPort);
                this.inBuff = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
                this.outPrint = new PrintWriter(tcpSocket.getOutputStream());
                (new Thread(new TcpListeningThread())).start();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void sendMessage(String message){
            synchronized(this) {
                if(!this.tcpSocket.isConnected())
                    return; // Or something more flexible...
                this.outPrint.println(message);
                this.outPrint.flush();
            }   
        }
    
    
        private class TcpListeningThread implements Runnable{
    
            public TcpListeningThread() {
                // Nothing to do...
            }
    
            @Override
            public void run() {
                try {
                    while(true){
                        String newLine = inBuff.readLine();
                        System.out.println(newLine); // Replace this with any processing you want !
                    }
                } catch (IOException e) {
                    System.err.println("Connection problem");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well i have a web page hosted in a IIS 7.0 now the problem
Well I have been writing in the same style for awhile now and all
I have task to write program allowing users to draw stars, which can differ
I'm working this programming task for college where we have to write a c++
Say I have two languages (A & B). My goal is to write some
I have a running wiki with users. Now I want to write an app
I'm fairly new to java and have come across a problem. My task is
well i have a configuration like this in the components part of my config
Well i have a div tag that i show as a modal dialog this
well I have that problem, im using a lightbox srcipt and im opening an

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.