gpt4 book ai didi

java - 连接到服务器,发送字符串并接收对象 - BufferedReader 不是 'ready' ?

转载 作者:行者123 更新时间:2023-12-01 06:22:35 25 4
gpt4 key购买 nike

服务器:

    public Server() {
gui = new GUI();
try {
socket = new ServerSocket(2000);
while (true) {
Socket client = socket.accept();
gui.append("Client accepted.");
if (inetAddrFound(client.getInetAddress().getHostName())) {
Thread thread = new Thread(new IncomingClient(client));
threads.put(client, thread);
thread.start();
}

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

public class IncomingClient implements Runnable {

BufferedReader reader;
Socket sock;

public IncomingClient(Socket socket) {
gui.append("Incoming socket class instantiated.");
try {
this.sock = socket;
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
} catch (Exception e) {
for (StackTraceElement s : e.getStackTrace()) {
gui.append(s.toString());
}
}
}

@Override
public void run() {
String message;
try {
while (true) {
gui.append("Trying to see if reader.readLine == null");
if (!reader.ready()) {
System.out.println("Reader is not ready.");
continue;
}
if ((message = reader.readLine()) != null && !Thread.currentThread().isInterrupted()) {
System.out.println("Reader is not null.");
System.out.println("Message = " + message);
gui.append(message);
if (idFound(message)) {
ObjectOutputStream stream = (ObjectOutputStream) sock.getOutputStream();
stream.writeObject(spooker);
stream.close();
threads.get(sock).interrupt();
threads.remove(sock);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

客户端:

private void connect() {
try {
socket = new Socket(decode(ADDR), Integer.parseInt(decode(PORT)));
ois = new ObjectInputStream(socket.getInputStream());
writer = new PrintWriter(socket.getOutputStream());
writer.println(String.valueOf(encode(getMotherboardSN())));
writer.flush;
writer.close;
} catch (Exception e) {
e.printStackTrace();
}
}

public class IncomingSpookerObject implements Runnable {

@Override
public void run() {
SpookerObject object;
try {
while ((object = (SpookerObject) ois.readObject()) != null && !Thread.currentThread().isInterrupted()) {
spooker = (SpookerObject) ois.readObject();
thread.interrupt();
System.out.println(spooker.getX());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

我的目标是连接到服务器,将用户cpu id(每台计算机的唯一标识符)发送到服务器(cpu id是一个字符串),服务器将读取cpu id以及用户是否注册(例如如果他们的 cpu id 在数据库上)那么它会向他们发送一个对象(Spooker 对象)。

spooker 对象是一个测试对象,它只保存 int x = 4 和一个返回 x 的方法 getX()。

我尝试这样做的原因是为了帮助阻止软件泄露。将来,spooker 对象将包含启动客户端软件的信息(例如 GUI 大小、GUI 标题、字符串值等)。这是为了使软件在连接到服务器并检索特定信息时更难泄漏。

我希望我已经尽可能清楚地表达了。

阿奇

编辑 - 更新的服务器对象。

服务器输出 -

Client accepted. Incoming socket class instantiated. Trying to see if reader.readLine == null

Reader is not ready. Reader is not ready. Reader is not ready. Reader is not ready. Reader is not ready. Reader is not ready.

它不断地打印出来——为什么读者从来没有准备好?

编辑;补充:

writer.println writer.flush(); writer.close();

最佳答案

您没有发送包含客户端数据的 CR,因此就服务器而言,还没有完整的行可供读取。尝试将 writer.write() 更改为 writer.println()。

关于java - 连接到服务器,发送字符串并接收对象 - BufferedReader 不是 'ready' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299000/

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