gpt4 book ai didi

java - 为什么我的消息在 Java 套接字服务器中只发送一次?

转载 作者:行者123 更新时间:2023-12-02 11:09:24 26 4
gpt4 key购买 nike

有一个服务器被认为可以同时为多个客户端提供服务。

所以当客户端连接时,他被添加到客户端数组中。当服务器收到消息时,会将其发送给所有客户端。当一个客户端连接时它工作得很好,但是当我同时有2个客户端时,消息只发送一次,之后就不再工作了。有什么问题吗?

服务器

static DataInputStream inputStream;
static DataOutputStream outputStream;

static ServerSocket serverSocket;
static final int PORT = 3003;

static Socket someClient;

static List<Socket> clients = new ArrayList<>();

public Server()
{
start();
}

public static void main(String[] args) throws IOException
{
try{
serverSocket = new ServerSocket(PORT);

print("Server started on " + serverSocket.getInetAddress().getHostAddress());

while (true)
{
someClient = serverSocket.accept();

new Server();
}

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

@Override
public void run()
{
try{
clients.add(someClient);

print("Connected from " + someClient.getInetAddress().getHostAddress());

InputStream sin = someClient.getInputStream();
OutputStream sout = someClient.getOutputStream();

inputStream = new DataInputStream(sin);
outputStream = new DataOutputStream(sout);

String message;

while (true)
{
message = inputStream.readUTF();

print(message);

for (int i = 0; i < clients.size(); i++)
{
Socket client = clients.get(i);

OutputStream os = client.getOutputStream();

DataOutputStream oss = new DataOutputStream(os);

oss.writeUTF(message);
}
}
} catch (Exception e){
e.printStackTrace();
}
}

客户端

socket = new Socket("0.0.0.0", 3003);

InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();

inputStream = new DataInputStream(sin);
outputStream = new DataOutputStream(sout);

sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key != null && key.length() == 16)
{
Date date = new Date();

String msg = ">> " + nickname + ": " + messageField.getText()+" | " + date.getHours()+":"+date.getMinutes()+"\n";

try {
outputStream.writeUTF(Encrypt.AESEncrypt(key, msg));
} catch (IOException e1) {
e1.printStackTrace();
}

messageField.setText("");
}
else if(key == null)
JOptionPane.showMessageDialog(J_Frame, "Your key field is empty");
else if(key.length() != 16)
JOptionPane.showMessageDialog(J_Frame, "Key's length should be 16 symbols");
}
});

while (true)
{

String message;

message = inputStream.readUTF();

append("\n" + Encrypt.AESDecrypt(key, message));
}

} catch (Exception e1) {
clear();
append(">> Unable to connect to the server.");
hideButtons();
}

最佳答案

每次客户端连接到您的服务器时,它都会替换以前的连接:

while (true)
{
someClient = serverSocket.accept();
...
}

someClient 是静态的:

static Socket someClient;

这意味着它被所有线程共享。此外,对它的访问不会以任何方式同步,这意味着对其值的更改不能保证对其他线程可见。

Peter Lawrey评论中指出,流也需要是非静态的:

static DataInputStream inputStream;
static DataOutputStream outputStream;

实际上,您始终从“最新”inputStream 中读取的事实可能是您所描述的行为的主要原因。outputStream 似乎未使用,因此最好将其删除。

除此之外,OutputStreams 可能需要刷新才能实际发送数据。

关于java - 为什么我的消息在 Java 套接字服务器中只发送一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50700105/

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