繁体   English   中英

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

[英]Why is my message sent only once in Java socket server?

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

因此,当客户端连接时,他被添加到客户端阵列。 当服务器收到消息后,它将发送给所有客户端。 当连接一个客户端时,它可以完美工作,但是当我同时有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才能实际发送数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM