读取数据时套接字已损坏或超时

时间:2018-11-20 14:51:08

标签: java android sockets

此刻我正在编写一个套接字客户端。

在套接字上写完全正常。

但是当我尝试使用BufferedReader读取数据时,它将挂起。

有时它可以工作,但是大多数套接字被(设备?)破坏了。

(我只在服务器端看到此信息:+ WIND:62:Socket客户端消失:172.20.237.2,这意味着客户端不见了/关闭了套接字)

我尝试过:

- first i used readNewLine, it worked but slowly so i tought this was 
blocking.
- then i tried Char by Char but this is also blocking at: br.read i think.
- i tried to set Socket Timeot to 100000 e.g.
- i changed port (80,3067, now 6660)

我不知道为什么它从Wifi模块(带有Socketlistener)到Android手机的连接如此之慢。

在此示例中,我使用char“!”让我的客户知道字符串何时结尾。

也许是另一个问题... 我希望有人能帮助我。

这是我的读写代码:

 @Override
     protected String doInBackground(String... params) //was strings
     {
         ip = params[0];
        message = params[1];
        port = params[2];


        try
        {
            s = new Socket(ip,Integer.parseInt(port));
            s.setSoTimeout(100000);

            if (s.isConnected())
            {
                dos = new DataOutputStream(s.getOutputStream());
                dos.writeBytes(message);
                dos.flush();

                is = s.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                //StringBuffer buffer = new StringBuffer();

                // or of course StringBuffer if you need to be treadsafe
                StringBuilder messageBuffer = new StringBuilder();

                // reads to the end of the stream or till end of message
                while((value = br.read()) != -1) {
                    char c = (char)value;
                    // end?  jump out
                    if (c == endMarker) {
                        break;
                    }
                    // else, add to buffer
                    messageBuffer.append(c);
                }
                // message is complete!
                response = messageBuffer.toString();

                /*
                BufferedReader br = new BufferedReader(new 
InputStreamReader(s.getInputStream()));
                response = br.readLine();
                */
            }
            //dos.writeUTF(message);

            //br.close();
            dos.close();
            s.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }


        return response;
    }

我看到客户端消失后的LogCat:

我不知道这是引起问题还是在发生之后:

java.lang.NumberFormatException:s == null

2018-11-20 15:34:44.921 3787-3920/? D/SemContextService: 
Service.updateContext() : service = Step Level Monitor
2018-11-20 15:34:44.924 22956-23045/? I/Sensor[0x06]: [0x01] 33
2018-11-20 15:34:44.925 22956-23045/? I/Sensor[0x04]: [0x01] 
1542724446610[0x02] 0[0x03] 2[0x04] 10012
2018-11-20 15:34:45.686 27406-27406/? D/gpsd: CALL_SENTRY(main()): 
GlEngine::OnResourceRelease took 251 ms (from 143759612 to 143759863) 
(logOverhead=0,0,0,0,0,0,0), start: 15:34:45.435
2018-11-20 15:34:45.937 27406-27406/? D/gpsd: CALL_SENTRY(main()): 
GlEngine::OnResourceRequest took 251 ms (from 143759863 to 143760114) 
(logOverhead=0,0,0,0,0,0,0), start: 15:34:45.686
2018-11-20 15:34:45.940 27406-27406/? D/gpsd: CALL_SENTRY(main()): 
GlEngine::ChipData took 506 ms (from 143759611 to 143760117) 
(logOverhead=0,0,0,0,0,0,0), start: 15:34:45.434
2018-11-20 15:34:45.941 27406-27406/? D/gpsd: CALL_SENTRY(main()): 
ProcessEvent took 507 ms (from 143759611 to 143760118) 
(logOverhead=0,0,0,0,0,0,0), start: 15:34:45.434
2018-11-20 15:34:46.161 3787-8916/? D/ConnectivityService: 
filterNetworkStateForUid() uid: 1000 networkInfo: [type: WIFI[] - WIFI, 
state: CONNECTED/CONNECTED, reason: (unspecified), extra: "SocketAppTest", 
failover: false, available: true, roaming: false, metered: false]
2018-11-20 15:34:46.192 3408-6813/? V/APM_AudioPolicyManager: 
getAudioPolicyConfig: audioParam;activeStream
2018-11-20 15:34:46.193 3408-6813/? V/APM_AudioPolicyManager: ### active 
stream : 0
2018-11-20 15:34:46.194 3787-8916/? D/AudioService: active stream is 0x0
2018-11-20 15:34:46.196 3408-6813/? V/APM_AudioPolicyManager: 
getAudioPolicyConfig: audioParam;outDevice
2018-11-20 15:34:46.196 3408-6813/? V/APM_AudioPolicyManager: 
getNewOutputDevice() selected device 0
2018-11-20 15:34:46.197 3408-6813/? V/APM_AudioPolicyManager: ### curdevice 
: 2
2018-11-20 15:34:46.202 3787-8916/? W/System.err: 
java.lang.NumberFormatException: s == null
2018-11-20 15:34:46.202 3787-8916/? W/System.err:     at 
java.lang.Integer.parseInt(Integer.java:570)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at b.b.aF(:79)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at b.c.aN(:149)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at b.n.run(:72)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at 
android.os.Handler.handleCallback(Handler.java:789)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at 
android.os.Handler.dispatchMessage(Handler.java:98)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at 
android.os.Looper.loop(Looper.java:164)
2018-11-20 15:34:46.203 3787-8916/? W/System.err:     at 
android.os.HandlerThread.run(HandlerThread.java:65)
2018-11-20 15:34:46.678 12232-12699/? D/ClClient: Not sending keepalive.  
Current connection state=STOPPED
2018-11-20 15:34:46.699 3418-3639/? D/DnsProxyListener: DNSDBG::dns addrinfo 
af 2
2018-11-20 15:34:47.611 3787-8916/? D/SSRM:w: SIOP:: AP = 340, PST = 340 
(W:15), BAT = 313, USB = 318, CHG = 346, CP = 333
2018-11-20 15:34:48.087 4959-4959/? D/io_stats: !@   8,0 r 5038254 276077112 
w 1290028 18962328 d 122047 41066928 f 289127 307003 iot 2157260 1768288 th 
307200 0 0 pt 0 inp 0 0 63174.392
2018-11-20 15:34:49.339 3787-3909/? D/SamsungAlarmManager: Expired : 4
2018-11-20 15:34:49.340 3787-3909/? I/SamsungAlarmManager: setLocked to 
kernel - T:2 / 20181120T153509, SetElapsed=143783728, nowELAPSED=143763517
2018-11-20 15:34:49.341 3787-3909/? I/SamsungAlarmManager: setLocked to 
kernel - T:3 / 20181120T153500, SetElapsed=143774177, nowELAPSED=143763518
2018-11-20 15:34:49.342 3787-3909/? V/SamsungAlarmManager: Sending to uid : 
1000 listener=android.app.AlarmManager$ListenerWrapper@fa0b687 
alarm=Alarm{5a2ea2f type 2 when 143763516 android}
2018-11-20 15:34:49.356 3787-4006/? D/SamsungAlarmManager: setExact Listener 
(T:2/F:9/AC:false) 20181120T153525 - CU:1000/CP:3787
2018-11-20 15:34:49.501 13308-13308/? E/hw-IPCThreadState: binder thread 
pool (1 threads) starved for 155 ms
2018-11-20 15:34:49.632 13308-13308/? E/hw-IPCThreadState: binder thread 
pool (1 threads) starved for 129 ms
2018-11-20 15:34:49.649 3787-4015/? D/WifiP2pService: InactiveState143375
2018-11-20 15:34:49.650 3787-4015/? D/WifiP2pService: P2pEnabledState143375
2018-11-20 15:34:49.701 3787-6300/? D/SamsungAlarmManager: setExact Listener 
(T:2/F:9/AC:false) 20181120T153450 - CU:1000/CP:3787
2018-11-20 15:34:49.702 3787-6300/? I/SamsungAlarmManager: setLocked to 
kernel - T:2 / 20181120T153450, SetElapsed=143764875, nowELAPSED=143763879
2018-11-20 15:34:50.698 3787-3909/? D/SamsungAlarmManager: Expired : 4
2018-11-20 15:34:50.699 3787-3909/? I/SamsungAlarmManager: setLocked to 
kernel - T:2 / 20181120T153509, SetElapsed=143783728, nowELAPSED=143764876
2018-11-20 15:34:50.700 3787-3909/? V/SamsungAlarmManager: Sending to uid : 
1000 listener=android.app.AlarmManager$ListenerWrapper@44c69c5 
alarm=Alarm{213f1a type 2 when 143764844 android}
2018-11-20 15:34:50.747 3787-6300/? D/SamsungAlarmManager: setExact Listener 
(T:2/F:9/AC:false) 20181120T153452 - CU:1000/CP:3787
2018-11-20 15:34:50.747 3787-6300/? I/SamsungAlarmManager: setLocked to 
kernel - T:2 / 20181120T153452, SetElapsed=143767063, nowELAPSED=143764924
2018-11-20 15:34:50.753 3787-6300/? D/DhcpClient: Renewed lease: IP address 
172.20.237.2/24 Gateway 172.20.237.1  DNS servers: [ 172.20.237.1 ] Domains  
DHCP server /172.20.237.1 Vendor info null lease 240 seconds
2018-11-20 15:34:50.754 3787-6300/? D/SamsungAlarmManager: Cancel Alarm 
calling from uid:1000 pid :3787 / 
listener:android.app.AlarmManager$ListenerWrapper@77dfa4b
2018-11-20 15:34:50.754 3787-4006/? D/SamsungAlarmManager: Cancel Alarm 
calling from uid:1000 pid :3787 / 
listener:android.app.AlarmManager$ListenerWrapper@912383c
2018-11-20 15:34:50.758 3787-6300/? I/SamsungAlarmManager: setLocked to 
kernel - T:2 / 20181120T153500, SetElapsed=143774177, nowELAPSED=143764935
2018-11-20 15:34:50.769 3787-4015/? D/WifiP2pService: InactiveState143375
2018-11-20 15:34:50.769 3787-6300/? D/SamsungAlarmManager: setExact Listener 
(T:2/F:9/AC:false) 20181120T153645 - CU:1000/CP:3787
2018-11-20 15:34:50.770 3787-4015/? D/WifiP2pService: P2pEnabledState143375

我也刚刚在LogCat中找到了这个:(也许是问题所在?)

2018-11-20 16:01:01.546 3384-3384/? E/audit: type=1400 
audit(1542726061.545:3227): avc:  denied  { read } for  pid=16998 
comm="Analytics-Norma" name="boot_id" dev="proc" ino=3571102 
scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:proc:s0 
tclass=file permissive=0 SEPF_SM-G955F_8.0.0_0017 audit_filtered
2018-11-20 16:01:01.549 16998-17153/? E/msgr.BootIdReader: Error reading 
boot_id from procfs
java.io.FileNotFoundException: /proc/sys/kernel/random/boot_id (Permission 
denied)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:200)
    at java.io.FileInputStream.<init>(FileInputStream.java:150)
    at java.io.FileInputStream.<init>(FileInputStream.java:103)
    at java.io.FileReader.<init>(FileReader.java:58)
    at X.2MN.a(:372675)
    at X.46q.a(:579709)
    at X.0Hi.A(Unknown Source:26)
    at X.0IH.a(:60614)
    at X.0IH.b(:60638)
    at X.41k.a(:569612)
    at X.1y8.a(:339223)
    at X.1rz.a(:330137)
    at X.0dC.a(:118033)
    at X.0d9.a(:117958)
    at X.0fP.d(:123017)
    at X.0fP.c(:122985)
    at X.0fP.handleMessage(:123035)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
2018-11-20 16:01:02.739 16656-16708/? E/Fabric: Settings request failed.
+...
2018-11-20 16:04:25.446 16779-16855/? E/BtGatt.GattService: [GSIM LOG]: 
gsimLogHandler, msg: MESSAGE_SCAN_STOP, appName: com.enbw.ev, scannerId: 7, 
reportDelayMillis=0
2018-11-20 16:04:25.472 4777-5949/? E/ExecutionCriteria: Package unavailable 
for task: com.tumblr/com.firebase.jobdispatcher.GooglePlayReceiver{u=0 
tag="retryServiceTag" 
trigger=window{start=3429s,end=3610s,earliest=-3609249s,latest=-3609069s} 
requirements=[NET_ANY] attributes=[PERSISTED,RECURRING] scheduled=-3612679s 
last_run=N/A jid=N/A status=PENDING retries=0 
client_lib=FIREBASE_JOB_DISPATCHER-1}
2018-11-20 16:04:36.402 3787-4392/? E/Watchdog: !@Sync 2164 [2018-11-20 
16:04:36.402]
2018-11-20 16:04:39.119 5626-5793/? E/ContactsProvider_EventLog: Flush 
buffer to file cnt : 1 size : 1Kb duration : 2ms lastUpdatedAfter : 60132 ms 
mFlush_time_threasold : 2000 mCurrentSize : 800
2018-11-20 16:04:55.481 4777-5949/? E/ExecutionCriteria: Package unavailable 
for task: com.tumblr/com.firebase.jobdispatcher.GooglePlayReceiver{u=0 
tag="retryServiceTag" 
trigger=window{start=3429s,end=3610s,earliest=-3609279s,latest=-3609099s} 
requirements=[NET_ANY] attributes=[PERSISTED,RECURRING] scheduled=-3612709s 
last_run=N/A jid=N/A status=PENDING retries=0 
client_lib=FIREBASE_JOB_DISPATCHER-1}
2018-11-20 16:04:55.551 16779-16855/? E/BtGatt.GattService: [GSIM LOG]: 
gsimLogHandler, msg: MESSAGE_SCAN_START, appName: com.enbw.ev, scannerId: 7, 
reportDelayMillis=0
2018-11-20 16:05:00.596 16779-16855/? E/BtGatt.GattService: [GSIM LOG]: 
gsimLogHandler, msg: MESSAGE_SCAN_STOP, appName: com.enbw.ev, scannerId: 7, 
reportDelayMillis=0

0 个答案:

没有答案