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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:23:19+00:00 2026-05-20T22:23:19+00:00

I am making and application to send data using sockets from an android device

  • 0

I am making and application to send data using sockets from an android device to a server and in reverse.
I am using an AsyncTask class to handle the connection. My problem is that at some point in my main Activity class i have to read from the input stream eg.

    conhandler = new ConnectionHandler();
    conhandler.execute();
    Log.i("AppOne", "Reading from stream");
    message = conhandler.readFromStream();

and what happens is that this code snippet is executed before my ConnectionHandler class has established a socket connection. Any ways to fix this issue?
Here is the code to my ConnectionHandler class:

public class ConnectionHandler extends AsyncTask<Void, Void, Void>{

public static String serverip = "192.168.1.100";
public static int serverport = 7777;
Socket s;
public DataInputStream dis = null;
public DataOutputStream dos = null;
public int message;



@Override
protected Void doInBackground(Void... params) {

    try {
        Log.i("AsyncTank", "doInBackgoung: Creating Socket");
        s = new Socket("192.168.1.100", 7777);
    } catch (Exception e) {
        Log.i("AsyncTank", "doInBackgoung: Cannot create Socket");
    }
    Log.i("AsyncTank", "doInBackgoung: Socket created");
    if (s.isConnected()) {
        try {
            Log.i("AsyncTank", "doInBackgoung: inside input try");
            dis = new DataInputStream(s.getInputStream());
            Log.i("AsyncTank", "doInBackgoung: InputStream assigned");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
            e.printStackTrace();
        }
        try {
            Log.i("AsyncTank", "doInBackgoung: inside output try");
            dos = new DataOutputStream(s.getOutputStream());
            Log.i("AsyncTank", "doInBackgoung: OutputStream assigned");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
            e.printStackTrace();
        }
    } else {
        Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket is closed");
    }

    return null;
}

public void writeToStream(double lat, double lon) {
    try {
            Log.i("AsynkTask", "writeToStream : Writing lat");
            dos.writeDouble(lat);
            dos.flush();
        } catch (Exception e) {
        Log.i("AsynkTask", "writeToStream : Writing failed");
    }
    try {
            Log.i("AsynkTask", "writeToStream : Writing lon");
            dos.writeDouble(lon);
            dos.flush();
        } catch (Exception e) {
        Log.i("AsynkTask", "writeToStream : Writing failed");
    }
}

public int readFromStream() {
    try {
        if (s.isConnected()) {
            Log.i("AsynkTask", "readFromStream : Reading message");
            message = dis.readInt();
        } else {
            Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
        }
    } catch (Exception e) {
        Log.i("AsynkTask", "readFromStream : Reading failed");
    }
    return message;
}

}

  • 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-20T22:23:19+00:00Added an answer on May 20, 2026 at 10:23 pm

    Yep, you need to do all the network operations in the ASyncTask thread. You can also do things when the task finishes, by overriding the onPostExecute() method. Stuff in there runs on the UI thread, so you can manipulate textviews, layouts etc.

    Example of how you could use an asynctask to open a socket and write a double to it:

    public class ASyncExample extends Activity {
    
        private TextView mTextView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // set stuff up
            super.onCreate(savedInstanceState);
        }
    
        private class WriteToServer extends AsyncTask<Double, Void, Void> {
    
            private final String serverip = "192.168.1.100";
            private final int serverport = 7777;
            Socket s;
            private DataOutputStream dos = null;
    
            @Override
            protected Void doInBackground(Double... params) {
    
                try {
    
                    // open the stream
                    s = new Socket("192.168.1.100", 7777);
                    dos = new DataOutputStream(s.getOutputStream());
    
                    // write the passed double to the stream
                    dos.writeDouble(params[0]);
                    dos.flush();
    
                } catch (Exception e) {
                    Log.i("AsyncTank", "something's gone wrong");
                }
    
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.