gpt4 book ai didi

java - Java套接字将多个对象发送到同一服务器

转载 作者:行者123 更新时间:2023-12-03 11:55:21 25 4
gpt4 key购买 nike

我正在尝试通过套接字将多个对象发送到Java服务器。

为了具有通用类型,我将消息转换为Message类的实例,然后将此对象发送到服务器。
我编写了一个小的testclass,它将三个对象发送到服务器。
问题是,只有一个对象到达服务器。
我几乎尝试了所有事情,但没有成功。

我的服务器:

public class Server {
private ServerConfig conf = new ServerConfig();
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
Logger log = new Logger();
ServerSocket socket;
Chat chat = new Chat();

public static void main(String[] args) {
Server s = new Server();
if (s.runServer()) {
s.listenToClients();
}
}

public boolean runServer() {
try {
socket = new ServerSocket(port);
logToConsole("Server wurde gestartet!");
return true;
} catch (IOException e) {
logToConsole("Server konnte nicht gestartet werden!");
e.printStackTrace();
return false;
}
}

public void listenToClients() {
while (true) {
try {
Socket client = socket.accept();
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
Thread clientThread = new Thread(new Handler(client, writer));
clientThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void logToConsole(String message) {
System.out.print(message);
}

public class Handler implements Runnable {

Socket client;
ObjectInputStream reader;
ObjectOutputStream writer;
User user;

public Handler(Socket client, ObjectOutputStream writer) {
try {
this.client = client;
this.writer = writer;
this.reader = new ObjectInputStream(client.getInputStream());
this.user = new User();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
while (true) {
Message incomming;
try {
while ((incomming = (Message) reader.readUnshared()) != null) {
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
logToConsole(
"Vom Client: \n" + incomming.getType() + "-----" + incomming.getValue().toString());
handle(incomming);
}
} catch (SocketException se) {
se.printStackTrace();
Thread.currentThread().interrupt();

} catch (IOException ioe) {
ioe.printStackTrace();
Thread.currentThread().interrupt();

} catch (ClassNotFoundException e) {
e.printStackTrace();
Thread.currentThread().interrupt();

}

}
}

private void handle(Message m) throws IOException {
String type = m.getType();
if (type.equals(config.ConstantList.Network.CHAT.toString())) {
chat.sendMessage(m);
} else if (type.equals(config.ConstantList.Network.LOGIN.toString())) {
System.out.println(user.login(m.getValue().get(0), writer));
System.out.println(m.getValue().get(0));
}
}
}

}

客户端:
public class Connect {

Socket client = null;
ObjectOutputStream writer = null;
ObjectInputStream reader = null;
private Config conf = new Config();
//private String host = conf.loadProp("ServerIP");
String host = "localhost";
private int port = Integer.parseInt(conf.loadProp("ServerPort"));

public boolean connectToServer() {
try {
client = new Socket(host, port);
reader = new ObjectInputStream(client.getInputStream());
writer = new ObjectOutputStream(client.getOutputStream());
logMessages("Netzwerkverbindung hergestellt");
Thread t = new Thread(new MessagesFromServerListener());
t.start();
return true;
} catch (Exception e) {
logMessages("Netzwerkverbindung konnte nicht hergestellt werden");
e.printStackTrace();
return false;
}
}

public boolean isConnectionActive() {
if (client == null || writer == null || reader == null){
return false;
}else{
return true;
}
}

public void sendToServer(Message m) {
try {
writer.reset();
writer.writeUnshared(m);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}

}

我尝试使用该类发送对象:
public void sendChatMessage(String username, String message) throws InterruptedException {
ChatMessage cm = new ChatMessage();
cm.setChat(username, null, message);
Message m = new Message(cm);
conn.sendToServer(m);
System.out.println("SENDED");
}

public static void main(String[] args) throws InterruptedException {
String username = "testuser";
String chatmessage = "Hallo Welt!";
connect.connect();
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
}

我知道这总是相同的消息,但这仅用于测试目的。
消息是它们可序列化的对象,只有一个对象按设计工作。

有人能看到我在哪里犯错了吗?

最佳答案

while ((incomming = (Message) reader.readUnshared()) != null) {

在这里,您正在读取一个对象,并阻塞直到它到达。
    logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");

在这里,您正在读取另一个对象,并阻塞直到它到达,然后将其错误地记录为您在上一行中已经读取的对象。

而不是记录 reader.readObject(),您应该记录 incoming的值,该值也有误。

并且循环是不正确的。 readObject()在流的末尾不返回null:它抛出 EOFException。每当您写入null时,它都可以返回null,因此将其用作循环终止条件是完全错误的。您应该捕获 EOFException并中断。

关于java - Java套接字将多个对象发送到同一服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568276/

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