Java TCP Echo Server - 广播 [英] Java TCP Echo Server - Broadcast

查看:56
本文介绍了Java TCP Echo Server - 广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的回显服务器,我希望当连接的用户向服务器输入任何内容时,所有其他客户端和该客户端都会收到消息 +| MOD".

I have a simple echo server, and I want when a connected user types anything to the server, all other clients and that client will get a the message + " | MOD".

它现在不会发送给所有客户端,但它应该发送给所有客户端,我只是不知道我的代码有什么问题,所以现在它只会将消息 + "| MOD" 发送给发送消息的客户端,而不是所有客户端其他人也应该这样做.

It wont send to all clients now but it should and I just don't know what's wrong in my code, so now it will just send the message + " | MOD" to the client who sent the message but not to all others also as it should.

我只是不明白,我有一个遍历所有客户端的循环,但它仍然不会发送给所有客户端.

I just don't get it, I have a loop that goes through all clients, but it still won't send to all.

服务器:

package com.murplyx.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server {
    public static ServerSocket server;
    public static ArrayList<Socket> clients = new ArrayList<Socket>();

    public static void broadcast(String message) {
        try {
            for (Socket socket : clients) {
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

                out.println(message);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        try {
            server = new ServerSocket(9000);

            while (true) {
                clients.add(server.accept());

                for (Socket socket : clients) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                    String line = in.readLine();

                    if (line != null) {
                        broadcast(line + " | MOD");
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

客户:

package com.murplyx.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
    public static void main(String args[]) {
        try {
            while (true) {
                Socket socket = new Socket("localhost", 9000);

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

                out.println(input.readLine());

                System.out.println(in.readLine());

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

请帮忙.

非常感谢.

推荐答案

您遇到的问题之一是每个客户端都会重复执行读取标准输入、写入套接字、读取套接字、写入标准输出... 无止境.

One of the issues you have is that each client will repeatedly do read stdin, write socket, read socket, write stdout, ... ad infinitum.

当您广播所有其他时,客户端通常仍处于读取标准输入阶段,因此他们不知道套接字上有等待读取的内容.他们仍在等待用户输入内容.

When you broadcast all other clients are still typically sat in the read stdin phase, so they don't know that there's stuff waiting to be read on the socket. They're still waiting for the user to enter something.

最简单的选择之一是在每个客户端启动两个线程——一个只处理读stdin,写socket,...,另一个处理读socket,写stdout.

One of the simplest options is to start two threads in each client - one just handles read stdin, write socket, ... and the other handles read socket, write stdout.

[另一个(可能更复杂的)选项,我们使用 Java NIO 轮询套接字和标准输入同时].

[Another (potentially more sophisticated) option us to use Java NIO to poll both the socket and stdin for available input at the same time].

第二个问题是您阻塞了服务器中的 accept 调用,然后依次从每个套接字读取.您可以在一个线程中accept,然后让另一个线程每个客户端只从客户端读取,然后再广播给其他线程.NIO 在这里也是一个不错的选择 - 您可以轮询任何任何客户端的读取.

A second issue is that you're blocking in the accept call in the server, and then reading from each socket in turn. You might accept in one thread, and have another thread per client read from just the client, and rebroadcast to the others. NIO can also be a good option here - you can poll for reads any any client.

这篇关于Java TCP Echo Server - 广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆