android套接字编程:通过esp8266发送字符串时显示错误?

时间:2016-05-14 03:30:28

标签: android sockets

我正在使用切换按钮,所以当它打开时它应该发送一个特定的字符串,当它关闭时它应该发送另一个字符串。我发布了我的代码和日志cat。所有ip地址和端口都在程序中配置和提及,还有什么需要在此代码中添加.................. .................................................. .................................................. .................................................. .................................................. .................................................. ................................................. < / p>

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.widget.ToggleButton;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Socket client;
    private PrintWriter printwriter;


    private ToggleButton toggleButton1;
    private String messsage;
    int port = 0;

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

        toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
        toggleButton1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (toggleButton1.isChecked()) {
                    messsage = "TV ON";
                    Log.d("On", "Button On" + messsage);
                    new Thread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            try {
                                // client = new
                                // Socket(etIP.getText().toString(), port);
                                client = new Socket("192.168.4.1", 100);

                                printwriter = new PrintWriter(client
                                        .getOutputStream(), true);
                                printwriter.write(messsage);
                                printwriter.flush();
                                printwriter.close();
                                client.close();
                            }

                            catch (UnknownHostException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }).start();

                } else {

                    messsage = "TV OFF";
                    Log.d("off", "Button off " + messsage);// etMsg.getText().toString();
                    // etMsg.setText("");
                    // port = Integer.parseInt(etPort.getText().toString());

                    new Thread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            try {
                                // client = new
                                // Socket(etIP.getText().toString(), port);
                                client = new Socket("192.168.4.1", 100);

                                printwriter = new PrintWriter(client
                                        .getOutputStream(), true);
                                printwriter.write(messsage);
                                printwriter.flush();
                                printwriter.close();
                                client.close();
                            }

                            catch (UnknownHostException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }).start();

                }

            }
        });

        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                try {
                    // client = new Socket(etIP.getText().toString(), port);
                    client = new Socket("192.168.4.1", 100);

                    printwriter = new PrintWriter(client.getOutputStream(),
                            true);
                    printwriter.write(messsage);
                    printwriter.flush();
                    printwriter.close();
                    client.close();
                }

                catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();




    }

log cat:

05-14 01:08:33.494    8293-8293/? D/off﹕ Button off TV OFF
05-14 01:08:33.494   8293-11491/? W/System.err﹕ java.net.SocketException: socket failed: EACCES (Permission denied)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:623)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at java.net.Socket.startupSocket(Socket.java:582)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at java.net.Socket.tryAllAddresses(Socket.java:128)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at java.net.Socket.<init>(Socket.java:178)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at java.net.Socket.<init>(Socket.java:150)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at com.example.lenovo.send.MainActivity$1$2.run(MainActivity.java:80)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at libcore.io.Posix.socket(Native Method)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:608)
05-14 01:08:33.494   8293-11491/? W/System.err﹕ ... 7 more
05-14 01:08:33.574    1950-2083/? I/art﹕ Explicit concurrent mark sweep GC freed 10727(462KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 17MB/28MB, paused 3.913ms total 80.446ms
05-14 01:08:33.574     973-1185/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.584     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.594     326-1158/? D/audio_hw_primary﹕ out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
05-14 01:08:33.594     326-6000/? E/AudioPolicyManager﹕ unknown stream type
05-14 01:08:33.594     326-6000/? I/AudioFlinger﹕ setAppName(), name=[system_server], active=[1]
05-14 01:08:33.624     326-1187/? I/AudioFlinger﹕ setAppName(), name=[system_server], active=[0]

1 个答案:

答案 0 :(得分:0)

你在AndroidManifest.xml中添加了所需的权限吗?

通过查看此refrence,必须添加以下权限:

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