Android客户端套接字

时间:2016-05-20 04:16:56

标签: android sockets client

我正在尝试创建一个客户端套接字来控制我的arduino板。我之前做过它并且有效。我不确定为什么这次它不会起作用。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>

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

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

mainactivity:

private Button connectButton;
private Button clearButton;
private EditText ipAddress;
private EditText portText;
private String ip;
private InetAddress address;
private String port;
private Socket socket;
private DataOutputStream outputStream;
private SeekBar brightness;
private TextView indicator;
private View.OnClickListener myListener;
private Context my;
private boolean press = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    connectButton = (Button)findViewById(R.id.button);
    clearButton = (Button)findViewById(R.id.button2);

    ipAddress = (EditText)findViewById(R.id.editText);
    portText = (EditText)findViewById(R.id.editText1);

    brightness = (SeekBar)findViewById(R.id.seekBar);
    indicator = (TextView)findViewById(R.id.textView2);

    connectButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            ip = ipAddress.getText().toString();
            port = portText.getText().toString();

            indicator.setText(ip);

            Thread thread = new Thread(new Runnable(){
                public void run() {
                    try {
                        socket = new Socket(InetAddress.getByName(ip), Integer.getInteger(port));
                        //outputStream = new DataOutputStream(socket.getOutputStream());

                        //while(outputStream != null){
                        // outputStream.writeBytes("0");
                        // }
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

            thread.start();

        }
    });

    brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

}

每次按连接时,程序都会崩溃。有人能解释为什么吗?

0 个答案:

没有答案