gpt4 book ai didi

Java - DataInputStream readUTF 返回 EOFException

转载 作者:行者123 更新时间:2023-12-01 13:37:05 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的套接字客户端-服务器对,其中一个可以发送消息,但在我的一个运行方法中,我这样做:String servermessage = new String(dis.readUTF()); 它返回一个 java.io.EOFException 我做错了什么,有没有更好的方法在客户端和服务器套接字之间发送消息?

编辑:我现在知道当输入流在所有字节之前到达末尾时会发生 EOFException,但我不明白它们的意思,有人可以澄清吗?

服务器:

public class Server {
String host = "localhost";
int port = 2484;
static boolean launched = false;

ServerSocket server;
static DataOutputStream dos;

public static void main(String args[]) throws IOException {
final Server serv = new Server();

final Thread socketThread = new Thread(new Runnable() {
ServerSocket server;

@Override
public void run() {
try {
Server serv = new Server();
ServerSocket server = new ServerSocket(serv.port);
System.out.println("Server: Launched!");

Socket client = server.accept();
OutputStream clientOut = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(clientOut);

dos.close();
clientOut.close();
server.close();
client.close();

} catch (IOException e) {
e.printStackTrace();

}
}
});

Thread input = new Thread(new Runnable() { //Thread that reads from an input from the console
Server serv = new Server();

@Override
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Type Something!");

while (true) {
String input = scanner.next();

if (input.equalsIgnoreCase("Start")) {
if(!launched) { //Checks if the ServerSocket server is already launched, and if not, launches it
System.out.println("Launching the socket!");
socketThread.start();
input = scanner.next();

launched = true;
} else if(launched){
System.err.println("ServerSocket is already Launched!");

}
} else if (input.equalsIgnoreCase("Send")) {
System.out.println("What would you like to send?");
input = null;
input = scanner.next();
System.out.println("Server: " + input);

try {
dos.writeUTF(input); //Sends the input to the client

} catch (IOException e) {
System.err.println("Unable to send the message");
e.printStackTrace();

}
} else {
System.err.println("Unknown Input!");
input = null;
}
}
}
});

input.start();
}
}

套接字管理器:

package Socket_Swing_Test_2;

public class SocketManager implements Runnable {
ClientMain c = new ClientMain();
ClientSwing cs = new ClientSwing();

Socket client;
PrintWriter output;
BufferedReader input;
InputStream clientIn;
DataInputStream dis;



String host = "localhost";
int port = 2484;

JTextArea textArea = cs.textArea;

public SocketManager(ClientMain c) {
this.c = c;

try {
client = new Socket(host, port);
System.out.println("SocketManager: Connected to the server!");

clientIn = client.getInputStream();
dis = new DataInputStream(clientIn);

} catch (Exception e) {
System.err.println("Client: Socket failed to Connect");
e.printStackTrace();

}
}

public synchronized void send(String message) {
try {
output.println(message);
output.flush();

} catch (Exception e) {
e.printStackTrace();

}
}

public synchronized void connect() {
try {
client = new Socket(host, port);
} catch (Exception e) {
System.err.println("Client: Socket failed to Connect");
e.printStackTrace();
}
}

public synchronized void close() {
try {
client.close();

} catch (Exception e) {
System.err.println("Unable to close the socket!");
e.printStackTrace();
}

clientIn = null;
dis = null;
System.gc(); // Garbage Collector, recycles unused objects that are taking up RAM

}

public synchronized boolean checkConnection() {
return client.isConnected();

}

public synchronized void listenStream() { //Unused, keeping for reference
try {
while (input.ready()) {
System.out.println(input.readLine());

}
} catch (Exception e) {
e.printStackTrace();
}

}

public synchronized Socket getSocket() {
return client;

}

public void receive() {

}

@Override
public void run() {
System.out.println("SocketManager.run: Is running!");

try {
//Read for a message from the server

System.out.println("SocketManager.run: Checking for messages from the server");
String servermessage = new String(dis.readUTF());
textArea.append(servermessage);

} catch (Exception e) {
e.printStackTrace();

} finally {

try {
client.close();
close();

} catch (Exception e) {
e.printStackTrace();

}
}
}
}

你们可能不需要,但以防万一

ClientSwing:

public class ClientSwing extends JFrame {
JPanel panel;
JTextArea textArea;
JTextField textField;

int WIDTH = 640;
int HEIGHT = 480;

public ClientSwing() {
super("Client");

panel = new JPanel();
panel.setLayout(null);

textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(0, 0, WIDTH, HEIGHT - 15);
textArea.setFont(new Font("Impact", Font.PLAIN, 13 + 1/2));

textField = new JTextField();
textField.setLocation(0, HEIGHT - 47);
textField.setSize(WIDTH, 20);
textField.addActionListener(new ActionListener() { //I AM RIGHT HERE
@Override
public void actionPerformed(ActionEvent e) {
String input = textField.getText();

if(input.equalsIgnoreCase("Launch")) {
SwingWorker socketLaunch = new SwingWorker() {

@Override
protected Object doInBackground() throws Exception {
ClientMain main = new ClientMain();
main.run();
return null;
}
};

socketLaunch.execute();
}
}
});

panel.add(textArea);
panel.add(textField);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setResizable(false);

add(textField);
add(textArea);
}
}

客户端主:

public class ClientMain extends JFrame {
private SocketManager network;
int a = 1;

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
ClientSwing window = new ClientSwing();
window.setVisible(true);
}
});
}

public void run() {
network = new SocketManager(new ClientMain());

Thread thread = new Thread(network);
thread.run();
}

public SocketManager getSocketM() {
return network;
}
}

跟踪:

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at Socket_Swing_Test_2.SocketManager.run(SocketManager.java:114)
at java.lang.Thread.run(Unknown Source)
at Socket_Swing_Test_2.ClientMain.run(ClientMain.java:26)
at Socket_Swing_Test_2.ClientSwing$1$1.doInBackground(ClientSwing.java:46)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

摘自 Data Streams 上的教程:

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value.

数据流为原始数据值提供二进制 I/O;没有任何方法可以读取代表结束的值。

关于Java - DataInputStream readUTF 返回 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21191017/

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