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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:11:15+00:00 2026-06-17T06:11:15+00:00

This issue I am having is that my program can only send my string

  • 0

This issue I am having is that my program can only send my string over TCP once through a AsyncTask. I have read that the AsyncTask can only run once and that after it runs it is supposed to be disposed and that if I need to send another command I should create another instance of the thread and use the new instance to send the new command. However for whatever reason when I attempt to create a second instance of my AsyncTask nothing happens. Something of note. I added a log command in the onPostExecute routine but I never see the log message.

I have the following in my Android-Manifest file:

<uses-permission android:name="android.permission.INTERNET/> 

This is the code in the main activity thread that calls the async task.

public void SendCommand(int DeviceID, String DeviceName, String CommandName)
{
    //Get the IP address and the port in order to communicate with the device
    DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String dbQuery = "SELECT * FROM " + dbHelper.System_Table + ", " + dbHelper.Devices_Table + ", " +
            dbHelper.Device_Commands_Table + " WHERE " + dbHelper.System_Table + "." + dbHelper.Attribute_Device_ID +
            " = " + String.valueOf(DeviceID) + " AND " + dbHelper.Devices_Table + "." + dbHelper.Attribute_Device_ID +
            " = " + String.valueOf(DeviceID) + " AND " + dbHelper.Device_Commands_Table + "." + dbHelper.Attribute_Device_ID +
            " = " + String.valueOf(DeviceID) + ";";
    Cursor c = db.rawQuery(dbQuery, null);
    String IPAddress = "";
    int Port = 0;
    String DeviceCommand = "";
    if (c.getCount() > 0)
    {
        int Finished = 0;
        while (c.moveToNext() && Finished == 0)
        {
            int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
            int iColumnDeviceCommandName = c.getColumnIndex(dbHelper.Attribute_Command_Name);
            final String CheckDeviceName = c.getString(iColumnDeviceName);
            final String CheckCommandName = c.getString(iColumnDeviceCommandName);
            if (DeviceName.equals(CheckDeviceName) && CheckCommandName.equals(CommandName))
            {
                Finished = 1;
                int iColumnIPAddress = c.getColumnIndex(dbHelper.Attribute_Device_IP);
                int iColumnPort = c.getColumnIndex(dbHelper.Attribute_Device_Port);
                int iColumnDeviceCommandString = c.getColumnIndex(dbHelper.Attribute_Command_String);
                IPAddress = c.getString(iColumnIPAddress);
                Port = c.getInt(iColumnPort);
                DeviceCommand = c.getString(iColumnDeviceCommandString);
                DeviceCommand = DeviceCommand.replace("<CR>", "\r");
                Log.d("Device Command To Send", DeviceCommand);
            }
        }
        c.close();
        dbHelper.close();
        ArrayList<String> passing = new ArrayList<String>();
        ArrayList<String> result = new ArrayList<String>();
        passing.add(IPAddress);
        passing.add(String.valueOf(Port));
        passing.add(DeviceCommand);
        SendCommand SC = new SendCommand();
        SC.execute(passing, result);
    }
}

This is the seperate class file that the above routine creates a instance of and executes.

public class SendCommand extends AsyncTask<ArrayList<String>, Void, ArrayList<String>>
{
    @Override
    protected void onPreExecute()
    {
        //progress bar
    }

    @Override
    protected ArrayList<String> doInBackground(ArrayList<String>... passing)
    {
        Log.d("Async Task", "Started to send command.");
        //Get the connection info and command to send from the caller
        String Data = passing[0].toString();
        int StopAt = Data.indexOf(",");
        String IPAddress = Data.toString().substring(1, StopAt);
        Data = Data.replace("[" + IPAddress + ", ", "");
        StopAt = Data.indexOf(",");
        int Port = Integer.parseInt(Data.toString().substring(0, StopAt));
        Data = Data.replace(Port + ", ", "");
        StopAt = Data.indexOf("]");
        String DeviceCommand = Data.toString().substring(0, StopAt);
        Send_Command(IPAddress, Port, DeviceCommand);
        return null;
    }

    protected void onPostExecute(Long result)
    {
        Log.d("Async Task", "Command Sent");
    }

    private void Send_Command(String IPAddress, int Port, String DeviceCommand)
    {
        //Setup the connection parameters
        SocketAddress SA = new InetSocketAddress(IPAddress, Port);
        int Timeout = 2000;
        Socket socket = new Socket();

        try
        {
            //Attempt to connect to the device
            socket.connect(SA, Timeout);
            int Count = 0;
            int Kill = 0;
            while (socket.isConnected() == false && Kill == 1)
            {
                //Waiting for the connection
                Count = Count + 1;
                if (Count == Timeout)
                {
                    Kill = 1;
                    Log.d("Connection Status", "Timed out");
                }
            }
            if (socket.isConnected() == true)
            {
                //send the command
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                wr.write(DeviceCommand);
                wr.flush();
                Log.d("Sent Device Command", DeviceCommand);
                //listen for the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String Response;
                while ((Response = rd.readLine()) != null)
                {
                    Log.d("Received Device Response", Response);
                }
                rd.close();
            }
            //close the socket once the response is received
            socket.close();
        }
        catch (UnknownHostException e)
        {
            Log.d("UnknownHostException", "Something bad happened");
        }
        catch (IOException e)
        {
            Log.d("IOException", "Something bad happened");
        }
        finally
        {
            if (socket != null)
            {
                try
                {
                    socket.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

Finally here is my log after attempting to send two commands over tcp.

01-14 23:53:29.773: D/Device Command To Send(31643): PN
01-14 23:53:29.773: D/Async Task(31643): Started to send command.
01-14 23:53:30.108: D/Sent Device Command(31643): PN
01-14 23:53:30.273: D/Received Device Response(31643): R
01-14 23:53:31.918: D/Device Command To Send(31643): PF

I can see that the first time the AsyncTask is started and it sends the command as well as receives the response from the target device. However I don’t see “Async Task”, “Command Sent” in the log so the onPostExecute is not executing.

Any thoughts on what I am doing wrong?

  • 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-17T06:11:16+00:00Added an answer on June 17, 2026 at 6:11 am

    Your onPostExecute signature is wrong.

    protected void onPostExecute(Long result)
    

    It should be

    protected void onPostExecute(ArrayList<String> result)
    

    It might be also good to use @Override annotation.

    Put some extra debug messages in your Send_Command, AsyncTask is probably hanging there.

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

Sidebar

Related Questions

I am having this issue. I have a script that checks if variable exists,
I am having some problems figuring out this issue. I have a server that
so I am having this issue that has been driving me crazy for hours,
I have simplified an issue that I've been having trying to isolate the problem,
I have a program that can open multiple forms, and when there are a
I am testing a GNU Radio program which can tunnel TCP traffic over a
I am having this issue and I am hoping that it is so simple
I would like to have a class T that can generate only 1 instance
having trouble coming up with search parameters for this issue, so I can't find
this is the second game that I'm having issues with in the same area...

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.