我正在學習Java編程的Socket編程。我得到java.net.SocketException: Connection reset
。獲取錯誤「java.net.SocketException:連接重置」
客戶端代碼
package com.socket;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientSock {
public static void main(String[] args) throws Exception {
Socket skt = new Socket("localhost", 8888);
String str = "Hello Server";
OutputStreamWriter osw = new OutputStreamWriter(skt.getOutputStream());
PrintWriter out = new PrintWriter(osw);
osw.write(str);
osw.flush();
}
}
//Server Side Code:
package com.socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSock {
public static void main(String[] args) throws Exception {
System.out.println("Server is Started");
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.println("Client Says : " + str);
}
}
這裏被運行後,客戶端代碼我的控制檯,我得到異常連接復位,在那裏我做錯了嗎?
Server is Started
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at com.socket.ServerSock.main(ServerSock.java:19)
可能重複[什麼導致我的java.net.SocketException:連接重置?](http://stackoverflow.com/questions/585599/whats-causing-my-java-net-socketexception-connection-reset) – halfer
[很多其他潛在的重複](https://stackoverflow.com/search?q=java.net.SocketException%3A+Connection+reset)。 – halfer