Here is a fun one... I am trying to develop a Android app that will let users control a camera in Autodesk Maya (a 3d modeling application). You can tell Maya to listen for Mel commands (maya's scripting language) on a specific port. So, I wrote this app to send the commands and I get the following errors:

09-21 14:07:34.021  23651-23651/com.jvspad.mayaterm I/System.out﹕ Begin.... 09-21 14:07:34.071  23651-24337/com.jvspad.mayaterm E/TCP Client﹕ C: Connecting... 09-21 14:07:34.182  23651-24337/com.jvspad.mayaterm E/TCP﹕ C: Error
    java.net.ConnectException: failed to connect to /10.0.0.2 (port 1024): connect failed: ECONNREFUSED (Connection refused)
            at libcore.io.IoBridge.connect(IoBridge.java:114)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
            at java.net.Socket.startupSocket(Socket.java:566)
            at java.net.Socket.<init>(Socket.java:225)
            at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:71)
            at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:62)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
            at libcore.io.Posix.connect(Native Method)
            at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
            at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
            at libcore.io.IoBridge.connect(IoBridge.java:112)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
            at java.net.Socket.startupSocket(Socket.java:566)
            at java.net.Socket.<init>(Socket.java:225)
            at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:71)
            at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:62)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)

So far I have ruled out the IP address and the port. I have also forwarded my port on the router when I first got this error. Could you guys help me rule out the possibility that it is something in my android application? Here is the relevant code for the Activity.

package com.jvspad.mayaterm;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
//Using http://cs.lmu.edu/~ray/notes/javanetexamples/ as bases for connection...
import java.net.UnknownHostException;
import java.net.Socket;
import java.net.InetAddress;
import java.io.*;

import android.util.Log;


public class DisplayMessageActivity extends Activity {

    private static final int SERVERPORT = 1024;
    private static final String SERVERIP = "10.0.0.2";

    //private OnMessageReceived mMessageListener = null;
    private boolean mRun = false;

    PrintWriter out;
    BufferedReader in;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("Begin....");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        Intent intent = getIntent();
        String message = intent.getStringExtra(TerminalWindow.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);

        new SendCommandTask().execute("URL");

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class SendCommandTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVERIP);
                Log.e("TCP Client", "C: Connecting...");

                //Make a socket to connect....
                Socket socket = new Socket(serverAddr, SERVERPORT);

                try {
                    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);


                } catch (Exception e) {

                    Log.e("TCP", "S: Error", e);

                } finally {
                    //the socket must be closed. It is not possible to reconnect to this socket
                    // after it is closed, which means a new socket instance has to be created.
                    socket.close();
                }

            } catch (Exception e) {

                Log.e("TCP", "C: Error", e);

            }
            return "Comeplete";
        }

    }
}

Here is my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jvspad.mayaterm" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".TerminalWindow"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName=".TerminalWindow" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.jvspad.mayaterm.TerminalWindow" />
        </activity>
    </application>

</manifest>

Thank you for your help!

Related posts

Recent Viewed