java - Maya和Android应用TCP发送Mel命令给 Maya

  显示原文与译文双语对照的内容
0 0

这里有一个有趣的。 我正在试图开发一个 Android 应用,它允许用户控制in中的相机。 你可以告诉Maya在特定端口上监听( Maya语言) 命令脚本脚本。 我写了这个应用程序来发送命令,我得到了以下错误:

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)

到目前为止我已经排除了IP地址和端口。 我第一次收到这个错误时,也在路由器上转发了我的端口。 你们能帮我排除在我的安卓应用程序中的可能性? 下面是 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";
 }
 }
}

这是我的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>

感谢你的帮助 !

时间:原作者:2个回答

0 0

你必须先打开maya端口,然后再发送命令给 maya 。

#force to close if its opened
cmds.commandPort(name=":9898", close=True)
#open new and change sourcetype to python if you sending python commands
cmds.commandPort(name=":9898", sourceType="mel")

我刚刚意识到它的旧问题:。 。)

原作者:
...