我正在尝试在 Android 客户端和 C# 服务器之间建立连接。我的服务器工作正常,除了 android 客户端即使在添加互联网连接权限后也无法工作
这是代码:
private void connectSocket(String a){
try {
InetAddress serverAddr = InetAddress.getByName("192.168.0.2");
Log.d("TCP", "C: Connecting....");
Socket socket = new Socket(serverAddr,4444);
Log.d("TCP", "C: I dunno ...");
//String message = "1";
PrintWriter out = null;
BufferedReader in = null;
try {
// Log.d("TCP", "C: Sending: '" + message + "'");
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//out.println(message);
while ((in.readLine()) != null) {
txt.append(in.readLine());
}
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: UnknownHostException", e);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: IOException", e);
e.printStackTrace();
}
}
这是调试日志:
11-15 02:41:58.040: W/dalvikvm(26839): threadid=1: thread exiting with uncaught exception (group=0x41c352a0)
11-15 02:41:58.075: E/AndroidRuntime(26839): FATAL EXCEPTION: main
11-15 02:41:58.075: E/AndroidRuntime(26839): android.os.NetworkOnMainThreadException
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-15 02:41:58.075: E/AndroidRuntime(26839): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-15 02:41:58.075: E/AndroidRuntime(26839): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-15 02:41:58.075: E/AndroidRuntime(26839): at libcore.io.IoBridge.connect(IoBridge.java:112)
11-15 02:41:58.075: E/AndroidRuntime(26839): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-15 02:41:58.075: E/AndroidRuntime(26839): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
11-15 02:41:58.075: E/AndroidRuntime(26839): at java.net.Socket.startupSocket(Socket.java:566)
11-15 02:41:58.075: E/AndroidRuntime(26839): at java.net.Socket.<init>(Socket.java:225)
11-15 02:41:58.075: E/AndroidRuntime(26839): at com.example.socketclient.SocketCode.connectSocket(SocketCode.java:50)
11-15 02:41:58.075: E/AndroidRuntime(26839): at com.example.socketclient.SocketCode.access$0(SocketCode.java:44)
11-15 02:41:58.075: E/AndroidRuntime(26839): at com.example.socketclient.SocketCode$1.onClick(SocketCode.java:35)
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.view.View.performClick(View.java:4211)
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.view.View$PerformClick.run(View.java:17267)
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.os.Handler.handleCallback(Handler.java:615)
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.os.Looper.loop(Looper.java:137)
11-15 02:41:58.075: E/AndroidRuntime(26839): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-15 02:41:58.075: E/AndroidRuntime(26839): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 02:41:58.075: E/AndroidRuntime(26839): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 02:41:58.075: E/AndroidRuntime(26839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-15 02:41:58.075: E/AndroidRuntime(26839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-15 02:41:58.075: E/AndroidRuntime(26839): at dalvik.system.NativeStart.main(Native Method)