簡體   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