android.system.ErrnoException:套接字失败:EACCES(权限被拒绝)

时间:2018-04-17 07:40:14

标签: java android sockets

我正在尝试将数据从设备(xiaomi redmi note 3 pro)传输到计算机。

这是计算机上的服务器代码。

public class Server implements Runnable {

private static volatile Server instane = null;
private final int SERVER_PORT = 27015;
private ServerSocket serverSocket = null;

public Server() {
}

public static Server getServer()
{
    if (instane == null)
    {
        synchronized (Server.class)
        {
            if(instane == null)
            {
                instane = new Server();
            }
        }
    }
    return instane;
}

@Override
public void run()
{
    try {
        serverSocket = new ServerSocket(SERVER_PORT);
        System.out.print("pusk...");
        while (true)
        {
            ConnectionWorker worker = null;

            try
            {
                worker = new ConnectionWorker(serverSocket.accept());

                Thread t = new Thread(worker);
                t.start();
            }catch (Exception e)
            {
                System.out.print(e.getMessage());
            }
        }

    } catch (IOException e)
    {
        e.printStackTrace();
    }
    finally {
        if(serverSocket != null)
        {
            try
            {
                serverSocket.close();
            }catch (IOException e)
            {}
        }
    }
}
}

主要

public class Main {

public static void main(String[] args) {
    Server server = Server.getServer();
    Thread t = new Thread(server);
    t.start();
}
}

Class ConnectionWorker

public class ConnectionWorker implements Runnable{ 

private Socket clientSocet = null;
private InputStream inputStream = null;

public ConnectionWorker(Socket clientSocet) {
    this.clientSocet = clientSocet;
}

@Override
public void run()
{
    try {
        inputStream = clientSocet.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] buffer = new byte[1024*4];

    while (true)
    {
        try
        {
            int count = inputStream.read(buffer,0, buffer.length);

            if(count > 0)
            {
                System.out.print(buffer);
            }else
            {
                if(count == -1)
                {
                    System.out.print("Close");
                    clientSocet.close();
                    break;
                }
            }

        }catch (IOException e)
        {}
    }
}
}

Android代码

主要课程

public class Main extends Activity{

Button btnOne;
Button btnTwo;
LaptopServer mServer;

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

    Toast.makeText(Main.this, "kfnklf", Toast.LENGTH_SHORT).show();

    btnOne = (Button) findViewById(R.id.btnOne);
    btnTwo = (Button) findViewById(R.id.btnTwo);

    btnOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final byte[] data = {1};
            mServer = new LaptopServer();

            try {
                mServer.openConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }

            new Thread(new Runnable() {
                @Override
                public void run()
                {
                    try {
                        mServer.sendData("j".getBytes());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                   // mServer.closeConnection();
                }
            }).start();
        }
    });
}
}

LaptopServer类

public class LaptopServer{

private String mServerName = "192.168.100.4";
private int mPortServer = 27015;
private Socket mSocket = null;

public LaptopServer() {
}

public void openConnection() throws Exception
{
    closeConnection();
    try
    {
        mSocket = new Socket(mServerName, mPortServer);
    }catch (IOException e)
    {
        throw new Exception("Невозможно открыть" + e.getMessage());
    }
}


public void closeConnection()
{
    if(mSocket != null && !mSocket.isClosed())
    {
        try
        {
            mSocket.close();
        }catch (IOException e)
        {}
        finally {
            mSocket = null;
        }
    }
    mSocket = null;
}

public void sendData(byte[] data) throws Exception
{
    if(mSocket == null || mSocket.isClosed())
    {
        throw  new Exception("Не создан сокет");
    }
    try
    {
        mSocket.getOutputStream().write(data);
        mSocket.getOutputStream().flush();
    }
    catch (IOException e)
    {
        throw new Exception("Невозможно отправить" + e.getMessage());
    }
}

@Override
protected void finalize() throws Throwable {
    super.finalize();
    closeConnection();
}
}

许可

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.pe.bsuir.transmitterwifi">

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

<application android:allowBackup="true"
             android:label="@string/app_name"
             android:icon="@mipmap/ic_launcher"
             android:supportsRtl="true"
             android:theme="@style/AppTheme">

    <activity android:name=".Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

</application>

我按照示例https://sidstudio.com.ua/sidstudio-blog/%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D1%87%D0%B0-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85-%D0%BF%D0%BE-%D1%81%D0%B5%D1%82%D0%B8-%D0%BF%D1%80%D0%B8-%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D0%B8-socket-%D0%BF%D1%80%D0%B8-%D1%80%D0%B0%D0%B7%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%BA%D0%B5-android-%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F

进行了操作

当我单击按钮时,我收到错误android.system.ErrnoException:套接字失败:EACCES(权限被拒绝)。

请告诉我如何修复或如何正确实施项目?

1 个答案:

答案 0 :(得分:0)

  

当我单击按钮时,我收到错误android.system.ErrnoException:套接字失败:EACCES(权限被拒绝)。

但在此之前你又有一个你忽略的例外。

  } catch (Exception e) {
            e.printStackTrace();
        }

你应该在那里放一个日志语句来记录异常。 “Невозможнооткрыть”。或者显示祝酒词。然后停下来。或者返回。

mSocket = new Socket(mServerName, mPortServer);

这将导致NetworkOnMainThreadException。你转换为异常。然后你忽略了。

您也应该将该代码放在一个线程中。