在Android应用程序上创建套接字

时间:2015-12-08 00:18:41

标签: java android sockets

我已经用Java创建了一个客户端和服务器程序,通过网络控制我的PC并且一切正常。我现在正在将客户端更改为Android应用程序,并遇到打开套接字的问题。

外部方法:

private PrintWriter clientMessage;
private Socket socket;
private int port = 1234;

点击连接按钮:

try {
    System.out.println("a");
    InetAddress IP = InetAddress.getByName("192.168.0.12");
    System.out.println("a");

    System.out.println("b");
    socket = new Socket(IP, port);
    System.out.println("b");

    connected = true;
    System.out.println("connected");
    clientMessage = new PrintWriter(socket.getOutputStream(), true);
}
catch (IOException ex) {
    System.out.println("error connecting");
}

控制台返回:

12-07 23:59:50.812 I/System.out: a
12-07 23:59:50.812 D/libc: [NET] android_getaddrinfofornet+,hn 12(0x3139322e313638),sn(),hints(known),family 0,flags 4
12-07 23:59:50.812 D/libc: [NET] android_getaddrinfofornet-, SUCCESS
12-07 23:59:50.812 I/System.out: a
12-07 23:59:50.812 I/System.out: b
12-07 23:59:50.812 I/System.out: error connecting

...单击连接按钮。从a / b标记我可以看到创建套接字时出现问题。

这个代码几乎就是我在客户端用作Java应用程序时使用的代码,所以不确定这里出了什么问题?

对Android来说很新,很抱歉,如果它很明显 - 提前感谢!

注意:服务器的端口号和IP地址是正确的,服务器是打开并正常运行的,所以不是这样。

修改1:

抛出IOException的堆栈跟踪:

W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:623)
W/System.err:     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
W/System.err:     at java.net.Socket.startupSocket(Socket.java:582)
W/System.err:     at java.net.Socket.<init>(Socket.java:226)
W/System.err:     at kaihulme.remoteapplication.MainActivity.onClick(MainActivity.java:80)
W/System.err:     at android.view.View.performClick(View.java:4756)
W/System.err:     at android.view.View$PerformClick.run(View.java:19749)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:135)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5221)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
W/System.err: Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
W/System.err:     at libcore.io.Posix.socket(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:608)
W/System.err:   ... 14 more

编辑2:

添加了权限:

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

...在查看Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'后收到Android清单并收到以下错误:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: kaihulme.remoteapplication, PID: 24959
                  android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                      at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
                      at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
                      at libcore.io.IoBridge.connect(IoBridge.java:122)
                      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
                      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
                      at java.net.Socket.startupSocket(Socket.java:590)
                      at java.net.Socket.<init>(Socket.java:226)
                      at kaihulme.remoteapplication.MainActivity.onClick(MainActivity.java:80)
                      at android.view.View.performClick(View.java:4756)
                      at android.view.View$PerformClick.run(View.java:19749)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5221)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I/Process: Sending signal. PID: 24959 SIG: 9

编辑3:

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kaihulme.remoteapplication">
    <uses-permission android:name="android.permission.INTERNET"/>
    <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"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

0 个答案:

没有答案