gpt4 book ai didi

java - 读取时连接重置()

转载 作者:可可西里 更新时间:2023-11-01 02:55:11 25 4
gpt4 key购买 nike

我正在尝试基于网络套接字技术实现服务器和客户端,但是当我运行 TCPDataServer 类时,出现以下错误:

java.io.IOException: connection reset
at java.io.DataInputStream.read(Unknown Source)
at org.server.ClientWorker.run(TCPDataServer.java:67)
at java.lang.Thread.run(Unknown Source)

我得到以下输出 你好,我是 Alex!

TCP数据客户端

package org.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPDataClient {

public static void main(String[] args) {
try {
TCPDataClient obj = new TCPDataClient();
Socket obj_client = new Socket(InetAddress.getByName("127.0.1"), 1000);
DataInputStream din = new DataInputStream(obj_client.getInputStream());
DataOutputStream dout = new DataOutputStream(obj_client.getOutputStream());
byte[] buffer = obj.createDataPacket("Hello, I am Alex!".getBytes("UTF8"));
dout.write(buffer);

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private byte[] createDataPacket(byte[] data) {

byte[] packet = null;
try {
byte[] initialize = new byte[1];
initialize[0] = 2;
byte[] separator = new byte[1];
separator[0] = 4;
byte[] data_length = String.valueOf(data.length).getBytes("UIF8");
packet = new byte[initialize.length + separator.length + data_length.length + data.length];

System.arraycopy(initialize, 0, packet, 0, initialize.length);
System.arraycopy(data_length, 0, packet, initialize.length, data_length.length);
System.arraycopy(separator, 0, packet, initialize.length + data_length.length, separator.length);
System.arraycopy(data, 0, packet, initialize.length + data_length.length + separator.length, data.length);


} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return packet;

}

}

TCP数据服务器

package org.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.logging.Level;

import com.sun.istack.internal.logging.Logger;

public class TCPDataServer {

public static void main(String[] args) {

try {
ServerSocket server_socket = new ServerSocket(1000);
while (true) {
new Thread(new ClientWorker(server_socket.accept())).start();
}
} catch (IOException e) {
System.err.println("Error");
System.err.println(e);
System.err.println("\n2");
System.err.println(e.getMessage());
System.err.println("\n3");
System.err.println(e.getLocalizedMessage());
System.err.println("\n4");
System.err.println(e.getCause());
System.err.println("\n5");
System.err.println(Arrays.toString(e.getStackTrace()));
System.err.println("\n6");
e.printStackTrace();
}

}

}

class ClientWorker implements Runnable {
private Socket target_socket;
private DataInputStream din;
private DataOutputStream dout;

public ClientWorker(Socket recv_scket) {

try {
target_socket = recv_scket;
din = new DataInputStream(target_socket.getInputStream());
dout = new DataOutputStream(target_socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void run() {
while (true) {
byte[] initilize = new byte[1];
try {
din.read(initilize, 0, initilize.length);
if (initilize[0] == 2) {
//This is line 67.
System.out.println(new String(readStream()));

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

private byte[] readStream() {
byte[] data_buff = null;

try {
int b = 0;
String buff_length = "";
while ((b = din.read()) != 4) {
buff_length += (char) b;
}
int data_length = Integer.parseInt(buff_length);
data_buff = new byte[Integer.parseInt(buff_length)];
int byte_read = 0;
int byte_offset = 0;
while (byte_offset < data_length) {
byte_read = din.read(data_buff, byte_read, data_length - byte_offset);
byte_offset += byte_read;

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return data_buff;

}

}

最佳答案

在您的服务器程序中,您使用了 din 作为从套接字读取的输入流读取器。在您的客户端程序中,您使用 dout 作为写入底层套接字的输出流编写器在您的 ClientWorker 类中,您使用 din 从套接字中进行了多次读取操作,而您使用 dout 对流进行了一次写入操作对象。

din 对象将阻塞/等待从客户端接收数据,但不幸的是,您可能已经关闭(程序结束)客户端上的套接字连接,这就是 的原因DataInputStream 抛出 IOException

如果您的底层套接字/流已关闭但您正在尝试使用它进行读取,您的 din 对象将抛出 IO 错误。

尝试在任何一侧(客户端/服务器)保持相同数量的读取和写入。

关于java - 读取时连接重置(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com