java.net.SocketTimeoutException
02-25 20:18:03.856 13374-13390/com.example.studylog W/System.err: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
02-25 20:18:03.856 13374-13390/com.example.studylog W/System.err: at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-25 20:18:03.856 13374-13390/com.example.studylog W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-25 20:18:03.856 13374-13390/com.example.studylog W/System.err: at com.example.studylog.MainActivity$ClientRunnable.run(MainActivity.java:128)
02-25 20:18:03.856 13374-13390/com.example.studylog W/System.err: at java.lang.Thread.run(Thread.java:856)
出现时机:
先运行ServerSocket服务端,读取文本内容,通过Socket返回给客户端。
在客户端创建Socket,但是我socket.setSoTimeout(1000)
我上面时间设置的太短了,所以得到了上面的异常,可以将时间调到5000以上。
02-25 20:58:42.197 16861-16875/com.example.studylog W/System.err: java.net.SocketException: Socket input is shutdown
02-25 20:58:42.197 16861-16875/com.example.studylog W/System.err: at java.net.Socket.shutdownInput(Socket.java:606)
02-25 20:58:42.197 16861-16875/com.example.studylog W/System.err: at com.example.studylog.MainActivity$ClientRunnable.run(MainActivity.java:133)
02-25 20:58:42.197 16861-16875/com.example.studylog W/System.err: at java.lang.Thread.run(Thread.java:856)
出现时机:
我客户端Socket shutdownInput之后又shutdownInput一次,调用了两遍,抛出了上面的错误。
shutdownInput:
将此套接字的输入流置于“流尾”。 发送到套接字的输入流一侧的任何数据都会被确认,然后以静默方式丢弃。
如果您在套接字上调用shutdownInput()之后从套接字输入流读取,则该流将返回EOF。
这里EOF相当于inputStream.read()返回了-1。
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at libcore.io.IoBridge.sendto(IoBridge.java:475)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at java.io.OutputStream.write(OutputStream.java:82)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at com.example.studylog.MainActivity$ClientRunnable.run(MainActivity.java:134)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at java.lang.Thread.run(Thread.java:856)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: Caused by: libcore.io.ErrnoException: sendto failed: ECONNRESET (Connection reset by peer)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at libcore.io.Posix.sendtoBytes(Native Method)
02-25 21:23:29.364 18530-18555/com.example.studylog W/System.err: at libcore.io.Posix.sendto(Posix.java:151)
02-25 21:23:29.374 18530-18555/com.example.studylog W/System.err: at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
02-25 21:23:29.374 18530-18555/com.example.studylog W/System.err: at libcore.io.IoBridge.sendto(IoBridge.java:473)
02-25 21:23:29.374 18530-18555/com.example.studylog W/System.err: ... 6 more
出现时机:
我客户端Socket 调用shutdownOutput之后,又对outputStream.write(“1”.getBytes());写入了一些东西,引发了这个异常。
shutdownOutput:
禁用此套接字的输出流。 对于TCP套接字,任何先前写入的数据将在TCP的正常连接终止序列后发送。 如果在套接字上调用shutdownOutput()之后写入套接字输出流,则该流将抛出IOException。
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: java.io.IOException: read failed: EBADF (Bad file number)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at libcore.io.IoBridge.read(IoBridge.java:442)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.io.FileInputStream.read(FileInputStream.java:179)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at libcore.io.Streams.readSingleByte(Streams.java:41)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.io.FileInputStream.read(FileInputStream.java:175)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at com.example.studylog.MainActivity$SocketProcessorRunnable.run(MainActivity.java:231)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at java.lang.Thread.run(Thread.java:856)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
02-25 21:49:13.052 20314-20334/com.example.studylog W/System.err: at libcore.io.Posix.readBytes(Native Method)
02-25 21:49:13.062 20314-20334/com.example.studylog W/System.err: at libcore.io.Posix.read(Posix.java:123)
02-25 21:49:13.062 20314-20334/com.example.studylog W/System.err: at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149)
02-25 21:49:13.062 20314-20334/com.example.studylog W/System.err: at libcore.io.IoBridge.read(IoBridge.java:432)
02-25 21:49:13.062 20314-20334/com.example.studylog W/System.err: ... 9 more
出现时机:
我服务端输入流inputStream.close之后,又调用read(),引发了上述错误。
Exception in thread "main" java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.example.MyClass.main(MyClass.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
出现时机:从异常返回来看,是我读InputStream时候,socket发现自己关闭了,所以抛出异常。
原因是我在写入Socket的时候,把OutputStream给close掉了
Returns an output stream for this socket.
If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an java.nio.channels.IllegalBlockingModeException.
Closing the returned OutputStream will close the associated socket.
从注释上看,你关闭OutputStream和InputStream都会导致关联的Socket关闭掉。
所以导致了我上面的异常。