java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:209) at java.net.SocketInputStream.read(SocketInp ...
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
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.qq.view.TestServer.<init>(TestServer.java:25)
at com.qq.view.TestServer.main(TestServer.java:15)
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)
在寫java伺服器端時出現上述錯誤(在客服端去鏈接服務端的時候),發現是
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
需要換成
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
package com.qq.view; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; /** * Created by lijun on 2016/12/27. */ public class TestClient { public static void main(String a[]){ TestClient testClient = new TestClient(); } public TestClient(){ try { Socket socket = new Socket("127.0.0.1",9999); // OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); // BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); // bufferedWriter. PrintWriter printWriter = new PrintWriter(socket.getOutputStream()); printWriter.println("hello"); } catch (IOException e) { e.printStackTrace(); } } } package com.qq.view; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /** * Created by lijun on 2016/12/27. */ public class TestServer { public static void main(String a[]){ TestServer testClient = new TestServer(); } public TestServer(){ try { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("--------在9999監聽-------"); Socket socket = serverSocket.accept(); InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String info = bufferedReader.readLine(); System.out.println(info); } catch (IOException e) { e.printStackTrace(); } } }
既是需要自動刷新才能發送到伺服器端。
如果客服端鏈接後不發送消息,伺服器端報同樣的異常。