为什么我得到 "java.net.SocketExcpecption: Connection reset"?
Why am I getting "java.net.SocketExcpecption: Connection reset"?
我正在尝试编写一个 tcp 客户端和服务器程序。服务器工作正常,它正常打开套接字,但是当我 运行 客户端程序时,我收到以下错误:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at server.Client.main(Client.java:22)
谁能告诉我如何解决它?提前致谢
这是我的客户端代码
public class Client {
private final static String serverIP = "192.168.56.1";
private final static int serverPort = 50000;
private final static String fileOutput ="D:\Julian\Kancolle.7z";
public static void main(String args[]) throws UnknownHostException, IOException {
Socket sock = new Socket(serverIP, serverPort);
byte[] byte_arr = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(fileOutput);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(byte_arr, 0, byte_arr.length);
bos.write(byte_arr, 0, bytesRead);
bos.close();
sock.close();
}
}
和服务器代码
public class Server implements Runnable {
private final static int serverPort = 50000; // reserves port
private final static String fileInput = "D:\Julian\Kancolle"; // destination
public static void main(String args[]) throws IOException{
int bytesRead; // buffer variable
ServerSocket servsock = new ServerSocket(serverPort);
File myFile = new File(fileInput);
while (true) {
Socket sock = servsock.accept();
InputStream in = sock.getInputStream();
OutputStream output = new FileOutputStream(myFile);
byte[] buffer = new byte[1024]; // buffer
while((bytesRead = in.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);;
}
output.close();
servsock.close();
}
}
public static void start(){
Server upd = new Server();
Thread tupd = new Thread(upd);
tupd.start();
}
@Override
public void run() {
}
}
这没有任何意义。双方都从套接字读取并复制到 FileOutputStream
。没有人通过套接字发送任何东西。所以真正应该发生的是第一次连接后的互读死锁。
您还错误地关闭了 accept()
循环中的 ServerSocket
。因此,您的服务器在尝试接受第二个连接时将获得未公开的 SocketException: socket closed
,退出,OS 将关闭泄漏的已接受套接字,并向对等方传播 FIN 或重置,具体取决于平台.
我正在尝试编写一个 tcp 客户端和服务器程序。服务器工作正常,它正常打开套接字,但是当我 运行 客户端程序时,我收到以下错误:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at server.Client.main(Client.java:22)
谁能告诉我如何解决它?提前致谢
这是我的客户端代码
public class Client {
private final static String serverIP = "192.168.56.1";
private final static int serverPort = 50000;
private final static String fileOutput ="D:\Julian\Kancolle.7z";
public static void main(String args[]) throws UnknownHostException, IOException {
Socket sock = new Socket(serverIP, serverPort);
byte[] byte_arr = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(fileOutput);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(byte_arr, 0, byte_arr.length);
bos.write(byte_arr, 0, bytesRead);
bos.close();
sock.close();
}
}
和服务器代码
public class Server implements Runnable {
private final static int serverPort = 50000; // reserves port
private final static String fileInput = "D:\Julian\Kancolle"; // destination
public static void main(String args[]) throws IOException{
int bytesRead; // buffer variable
ServerSocket servsock = new ServerSocket(serverPort);
File myFile = new File(fileInput);
while (true) {
Socket sock = servsock.accept();
InputStream in = sock.getInputStream();
OutputStream output = new FileOutputStream(myFile);
byte[] buffer = new byte[1024]; // buffer
while((bytesRead = in.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);;
}
output.close();
servsock.close();
}
}
public static void start(){
Server upd = new Server();
Thread tupd = new Thread(upd);
tupd.start();
}
@Override
public void run() {
}
}
这没有任何意义。双方都从套接字读取并复制到 FileOutputStream
。没有人通过套接字发送任何东西。所以真正应该发生的是第一次连接后的互读死锁。
您还错误地关闭了 accept()
循环中的 ServerSocket
。因此,您的服务器在尝试接受第二个连接时将获得未公开的 SocketException: socket closed
,退出,OS 将关闭泄漏的已接受套接字,并向对等方传播 FIN 或重置,具体取决于平台.