当我的客户端(下面的代码)将数据发送到我的服务器(下面的代码)时,我得到以下SocketException
注意:客户端和服务器编译为单独的jar文件。
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 Main.main(Main.java:16)
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)
客户来源:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
try {
Socket myClient = new Socket("localhost", 9998);
PrintWriter writer = new PrintWriter(myClient.getOutputStream());
writer.write("Testing");
writer.flush();
} catch (IOException ioex) {
System.out.println(ioex);
}
}
}
服务器来源:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
try {
ServerSocket myServer = new ServerSocket(9998);
Socket clientSocket = myServer.accept();
while(true) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String clientData = "";
clientData = reader.readLine();
System.out.println(clientData);
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
/* while(true) {
//Apparently, this will print a list of connections made... Useful in future?
System.out.println(myServer.accept());
}*/
} catch (IOException ioex) {
ioex.printStackTrace();
System.exit(1);
}
}
}
提前致谢!
我找到了一个解决方案,并将发布一个链接到我明天在这里使用的源。