TCP Server bricht ab

  • Antworten:2
Mathias Bergmann
  • Forum-Beiträge: 2

13.12.2013, 10:06:35 via Website

Hallo

Ich habe mir einen TCP Server kopiert, der funktioninirt auch.

1package de.example.androidsocketserver;
2/*
3import android.os.Bundle;
4import android.app.Activity;
5import android.view.Menu;
6
7public class MainActivity extends Activity {
8
9 @Override
10 protected void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.main);
13 }
14
15 @Override
16 public boolean onCreateOptionsMenu(Menu menu) {
17 // Inflate the menu; this adds items to the action bar if it is present.
18 getMenuInflater().inflate(R.menu.main, menu);
19 return true;
20 }
21
22}
23*/
24
25
26
27
28import java.io.BufferedReader;
29import java.io.IOException;
30import java.io.InputStreamReader;
31import java.net.ServerSocket;
32import java.net.Socket;
33
34import com.javacodegeeks.android.androidsocketserver.R;
35
36import android.app.Activity;
37import android.os.Bundle;
38import android.os.Handler;
39import android.view.View;
40import android.widget.TextView;
41
42
43public class Server extends Activity {//Server
44
45 private ServerSocket serverSocket;
46 public Handler updateConversationHandler;
47 public Thread serverThread = null;
48 private TextView text;
49 public static final int SERVERPORT = 1500;
50
51 @Override
52 public void onCreate(Bundle savedInstanceState) {
53
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.main);
56
57 text = (TextView) findViewById(R.id.tv2);
58
59 updateConversationHandler = new Handler();
60
61 this.serverThread = new Thread(new ServerThread());
62 this.serverThread.start();
63
64
65
66 }
67
68
69 public void buttonClick(View view){
70
71
72 }
73
74 @Override
75 protected void onStop() {
76 super.onStop();
77 try {
78 serverSocket.close();
79 } catch (IOException e) {
80 e.printStackTrace();
81 }
82 }
83
84 class ServerThread implements Runnable {
85
86 public void run() {
87 Socket socket = null;
88 try {
89 serverSocket = new ServerSocket(SERVERPORT);
90 } catch (IOException e) {
91 e.printStackTrace();
92 }
93
94 while (!Thread.currentThread().isInterrupted()) {
95
96 try {
97
98 socket = serverSocket.accept();
99
100 CommunicationThread commThread = new CommunicationThread(socket);
101 new Thread(commThread).start();
102
103 }
104 catch (IOException e) {
105 e.printStackTrace();
106 }
107 }
108 }
109 }
110
111 class CommunicationThread implements Runnable {
112
113 private Socket clientSocket;
114
115 private BufferedReader input;
116
117 public CommunicationThread(Socket clientSocket) {
118
119 this.clientSocket = clientSocket;
120
121 try {
122
123 this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
124
125 } catch (IOException e) {
126 e.printStackTrace();
127 }
128 }
129
130 public void run() {
131
132 while (!Thread.currentThread().isInterrupted()) {
133
134 try {
135
136 String read = input.readLine();
137 updateConversationHandler.post(new updateUIThread(read));
138
139 } catch (IOException e) {
140 e.printStackTrace();
141 }
142 }
143 }
144 }
145
146
147 class updateUIThread implements Runnable {
148 private String msg;
149 public updateUIThread(String str) {
150 this.msg = str;
151 }
152
153 @Override
154 public void run() {
155 //SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
156 //System.out.println(sdf.format(new Date()));
157
158 if(msg != "null"){
159 text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
160 //text.setText(msg.length());
161 System.out.println("--------------"+String.valueOf(msg.length()));
162
163 }
164 }
165 }
166}

Nun sollte die länge vom imput her. Bekomme aber einen fehler code. Auch eine ausgabe mit System.out.println() wird abgebrochen.

112-13 09:28:36.375: D/dalvikvm(27476): Late-enabling CheckJNI
212-13 09:28:36.445: E/Trace(27476): error opening trace file: No such file or directory (2)
312-13 09:28:36.455: W/ActivityThread(27476): Application com.javacodegeeks.android.androidsocketserver is waiting for the debugger on port 8100...
412-13 09:28:36.480: I/System.out(27476): Sending WAIT chunk
512-13 09:28:36.485: I/dalvikvm(27476): Debugger is active
612-13 09:28:36.680: I/System.out(27476): Debugger has connected
712-13 09:28:36.680: I/System.out(27476): waiting for debugger to settle...
812-13 09:28:36.880: I/System.out(27476): waiting for debugger to settle...
912-13 09:28:37.080: I/System.out(27476): waiting for debugger to settle...
1012-13 09:28:37.285: I/System.out(27476): waiting for debugger to settle...
1112-13 09:28:37.485: I/System.out(27476): waiting for debugger to settle...
1212-13 09:28:37.685: I/System.out(27476): waiting for debugger to settle...
1312-13 09:28:37.885: I/System.out(27476): debugger has settled (1329)
1412-13 09:28:38.095: D/libEGL(27476): loaded /system/lib/egl/libEGL_mali.so
1512-13 09:28:38.095: D/libEGL(27476): loaded /system/lib/egl/libGLESv1_CM_mali.so
1612-13 09:28:38.100: D/libEGL(27476): loaded /system/lib/egl/libGLESv2_mali.so
1712-13 09:28:38.100: E/(27476): Device driver API match
1812-13 09:28:38.100: E/(27476): Device driver API version: 17
1912-13 09:28:38.100: E/(27476): User space API version: 17
2012-13 09:28:38.100: E/(27476): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Mon May 13 15:55:05 KST 2013
2112-13 09:28:38.155: D/OpenGLRenderer(27476): Enabling debug mode 0
2212-13 09:28:49.810: I/Choreographer(27476): Skipped 106 frames! The application may be doing too much work on its main thread.
2312-13 09:28:57.325: D/dalvikvm(27476): GC_CONCURRENT freed 7259K, 72% free 3144K/10880K, paused 4ms+4ms, total 37ms
2412-13 09:28:59.715: D/dalvikvm(27476): GC_CONCURRENT freed 1899K, 71% free 3191K/10880K, paused 2ms+2ms, total 23ms
2512-13 09:29:02.165: D/dalvikvm(27476): GC_CONCURRENT freed 1883K, 71% free 3236K/10880K, paused 2ms+3ms, total 20ms
2612-13 09:29:04.220: D/dalvikvm(27476): GC_CONCURRENT freed 1888K, 70% free 3269K/10880K, paused 2ms+2ms, total 18ms
2712-13 09:29:06.280: D/dalvikvm(27476): GC_CONCURRENT freed 1871K, 70% free 3346K/10880K, paused 2ms+3ms, total 26ms
2812-13 09:29:08.390: D/dalvikvm(27476): GC_CONCURRENT freed 1905K, 69% free 3373K/10880K, paused 2ms+2ms, total 17ms
2912-13 09:29:10.505: D/dalvikvm(27476): GC_CONCURRENT freed 1899K, 69% free 3397K/10880K, paused 2ms+2ms, total 17ms
3012-13 09:29:12.660: D/dalvikvm(27476): GC_CONCURRENT freed 1900K, 69% free 3452K/10880K, paused 2ms+2ms, total 23ms
3112-13 09:31:13.005: D/dalvikvm(27476): GC_FOR_ALLOC freed 202K, 59% free 4566K/11016K, paused 21ms, total 22ms
3212-13 09:31:13.270: D/dalvikvm(27476): GC_FOR_ALLOC freed 203K, 59% free 4568K/11016K, paused 28ms, total 29ms
3312-13 09:31:14.875: E/Trace(27601): error opening trace file: No such file or directory (2)
3412-13 09:31:14.880: I/System.out(27601): Sending WAIT chunk
3512-13 09:31:14.880: W/ActivityThread(27601): Application com.javacodegeeks.android.androidsocketserver is waiting for the debugger on port 8100...
3612-13 09:31:14.885: I/dalvikvm(27601): Debugger is active
3712-13 09:31:15.080: I/System.out(27601): Debugger has connected
3812-13 09:31:15.080: I/System.out(27601): waiting for debugger to settle...
3912-13 09:31:15.280: I/System.out(27601): waiting for debugger to settle...
4012-13 09:31:15.480: I/System.out(27601): waiting for debugger to settle...
4112-13 09:31:15.685: I/System.out(27601): waiting for debugger to settle...
4212-13 09:31:15.885: I/System.out(27601): waiting for debugger to settle...
4312-13 09:31:16.085: I/System.out(27601): waiting for debugger to settle...
4412-13 09:31:16.285: I/System.out(27601): debugger has settled (1316)
4512-13 09:31:16.490: D/libEGL(27601): loaded /system/lib/egl/libEGL_mali.so
4612-13 09:31:16.490: D/libEGL(27601): loaded /system/lib/egl/libGLESv1_CM_mali.so
4712-13 09:31:16.490: D/libEGL(27601): loaded /system/lib/egl/libGLESv2_mali.so
4812-13 09:31:16.495: E/(27601): Device driver API match
4912-13 09:31:16.495: E/(27601): Device driver API version: 17
5012-13 09:31:16.495: E/(27601): User space API version: 17
5112-13 09:31:16.495: E/(27601): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Mon May 13 15:55:05 KST 2013
5212-13 09:31:16.540: D/OpenGLRenderer(27601): Enabling debug mode 0
5312-13 09:34:38.795: D/dalvikvm(27601): GC_CONCURRENT freed 2789K, 31% free 7614K/10880K, paused 8ms+4ms, total 65ms
5412-13 09:34:54.900: D/dalvikvm(27601): GC_FOR_ALLOC freed 150K, 28% free 7869K/10880K, paused 38ms, total 38ms
5512-13 09:34:55.415: D/dalvikvm(27601): GC_FOR_ALLOC freed 4K, 28% free 7876K/10880K, paused 36ms, total 36ms
5612-13 09:34:55.415: I/dalvikvm-heap(27601): Grow heap (frag case) to 10.032MB for 56-byte allocation
5712-13 09:34:55.790: D/dalvikvm(27601): GC_FOR_ALLOC freed 3K, 28% free 7882K/10884K, paused 36ms, total 37ms
5812-13 09:34:55.790: I/dalvikvm-heap(27601): Grow heap (frag case) to 10.038MB for 56-byte allocation
5912-13 09:34:56.170: D/dalvikvm(27601): GC_FOR_ALLOC freed 3K, 28% free 7887K/10888K, paused 37ms, total 37ms
6012-13 09:35:45.360: I/dalvikvm-heap(27601): Grow heap (frag case) to 10.708MB for 56-byte allocation
6112-13 09:35:45.765: D/dalvikvm(27601): GC_FOR_ALLOC freed 3K, 25% free 8575K/11388K, paused 39ms, total 39ms
6212-13 09:35:45.765: I/dalvikvm-heap(27601): Grow heap (frag case) to 10.714MB for 56-byte allocation
6312-13 09:44:21.265: E/Trace(27897): error opening trace file: No such file or directory (2)
6412-13 09:44:21.275: W/ActivityThread(27897): Application com.javacodegeeks.android.androidsocketserver is waiting for the debugger on port 8100...
6512-13 09:44:21.280: I/System.out(27897): Sending WAIT chunk
6612-13 09:44:21.280: I/dalvikvm(27897): Debugger is active
6712-13 09:44:21.480: I/System.out(27897): Debugger has connected
6812-13 09:44:21.480: I/System.out(27897): waiting for debugger to settle...
6912-13 09:44:21.680: I/System.out(27897): waiting for debugger to settle...
7012-13 09:44:21.880: I/System.out(27897): waiting for debugger to settle...
7112-13 09:44:22.080: I/System.out(27897): waiting for debugger to settle...
7212-13 09:44:22.280: I/System.out(27897): waiting for debugger to settle...
7312-13 09:44:22.485: I/System.out(27897): waiting for debugger to settle...
7412-13 09:44:22.685: I/System.out(27897): debugger has settled (1324)
7512-13 09:44:22.940: D/libEGL(27897): loaded /system/lib/egl/libEGL_mali.so
7612-13 09:44:22.940: D/libEGL(27897): loaded /system/lib/egl/libGLESv1_CM_mali.so
7712-13 09:44:22.940: D/libEGL(27897): loaded /system/lib/egl/libGLESv2_mali.so
7812-13 09:44:22.945: E/(27897): Device driver API match
7912-13 09:44:22.945: E/(27897): Device driver API version: 17
8012-13 09:44:22.945: E/(27897): User space API version: 17
8112-13 09:44:22.945: E/(27897): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Mon May 13 15:55:05 KST 2013
8212-13 09:44:22.975: D/OpenGLRenderer(27897): Enabling debug mode 0
8312-13 09:51:35.625: D/dalvikvm(27897): GC_CONCURRENT freed 2796K, 31% free 7607K/10880K, paused 2ms+4ms, total 59ms
8412-13 09:51:52.560: D/dalvikvm(27897): GC_FOR_ALLOC freed 156K, 28% free 7873K/10880K, paused 38ms, total 38ms
8512-13 09:51:53.100: D/dalvikvm(27897): GC_FOR_ALLOC freed 4K, 28% free 7881K/10880K, paused 37ms, total 37ms
8612-13 09:51:53.100: I/dalvikvm-heap(27897): Grow heap (frag case) to 10.037MB for 56-byte allocation
8712-13 09:51:53.480: D/dalvikvm(27897): GC_FOR_ALLOC freed 3K, 28% free 7886K/10884K, paused 38ms, total 38ms
8812-13 09:51:53.480: I/dalvikvm-heap(27897): Grow heap (frag case) to 10.041MB for 56-byte allocation
8912-13 09:51:53.865: D/dalvikvm(27897): GC_FOR_ALLOC freed 3K, 28% free 7892K/10888K, paused 37ms, total 37ms
9012-13 09:51:53.865: I/dalvikvm-heap(27897): Grow heap (frag case) to 10.047MB for 56-byte allocation
9112-13 09:51:54.250: D/dalvikvm(27897): GC_FOR_ALLOC freed 3K, 28% free 7897K/10892K, paused 35ms, total 36ms
9212-13 09:52:52.385: D/dalvikvm(27897): GC_FOR_ALLOC freed 3K, 25% free 8689K/11468K, paused 40ms, total 40ms
9312-13 09:52:52.385: I/dalvikvm-heap(27897): Grow heap (frag case) to 10.826MB for 56-byte allocation
9412-13 09:52:52.825: D/dalvikvm(27897): GC_FOR_ALLOC freed 3K, 25% free 8695K/11472K, paused 43ms, total 43ms
9512-13 09:52:52.825: I/dalvikvm-heap(27897): Grow heap (frag case) to 10.832MB for 56-byte allocation
9612-13 09:57:37.150: E/Trace(28383): error opening trace file: No such file or directory (2)
9712-13 09:57:37.330: D/libEGL(28383): loaded /system/lib/egl/libEGL_mali.so
9812-13 09:57:37.335: D/libEGL(28383): loaded /system/lib/egl/libGLESv1_CM_mali.so
9912-13 09:57:37.340: D/libEGL(28383): loaded /system/lib/egl/libGLESv2_mali.so
10012-13 09:57:37.350: E/(28383): Device driver API match
10112-13 09:57:37.350: E/(28383): Device driver API version: 17
10212-13 09:57:37.350: E/(28383): User space API version: 17
10312-13 09:57:37.355: E/(28383): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Mon May 13 15:55:05 KST 2013
10412-13 09:57:37.380: D/OpenGLRenderer(28383): Enabling debug mode 0
10512-13 09:57:47.805: D/AndroidRuntime(28383): Shutting down VM
10612-13 09:57:47.805: W/dalvikvm(28383): threadid=1: thread exiting with uncaught exception (group=0x40df1930)
10712-13 09:57:47.815: E/AndroidRuntime(28383): FATAL EXCEPTION: main
10812-13 09:57:47.815: E/AndroidRuntime(28383): java.lang.NullPointerException
10912-13 09:57:47.815: E/AndroidRuntime(28383): at de.example.androidsocketserver.Server$updateUIThread.run(Server.java:161)
11012-13 09:57:47.815: E/AndroidRuntime(28383): at android.os.Handler.handleCallback(Handler.java:725)
11112-13 09:57:47.815: E/AndroidRuntime(28383): at android.os.Handler.dispatchMessage(Handler.java:92)
11212-13 09:57:47.815: E/AndroidRuntime(28383): at android.os.Looper.loop(Looper.java:137)
11312-13 09:57:47.815: E/AndroidRuntime(28383): at android.app.ActivityThread.main(ActivityThread.java:5227)
11412-13 09:57:47.815: E/AndroidRuntime(28383): at java.lang.reflect.Method.invokeNative(Native Method)
11512-13 09:57:47.815: E/AndroidRuntime(28383): at java.lang.reflect.Method.invoke(Method.java:511)
11612-13 09:57:47.815: E/AndroidRuntime(28383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11712-13 09:57:47.815: E/AndroidRuntime(28383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11812-13 09:57:47.815: E/AndroidRuntime(28383): at dalvik.system.NativeStart.main(Native Method)


Benutze das sg3 cyangomod 10.10

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

13.12.2013, 10:47:35 via Website

Da du zum einen deine Strings falsch vergleichst und zum anderen auch noch ib beiden fällen auf das msg zugreifst kann es nur zu einer NPE kommen.
Das sollte recht einfach zu fixen sein.

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
Mathias Bergmann
  • Forum-Beiträge: 2

13.12.2013, 11:12:59 via Website

Wen ich nicht auf den wert zugreifen kann wie soll ich dann mit ihm arbeiten ?

— geändert am 13.12.2013, 11:13:15

Antworten