Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试创建回显客户端的广播服务器_Java_Sockets_Server - Fatal编程技术网

Java 正在尝试创建回显客户端的广播服务器

Java 正在尝试创建回显客户端的广播服务器,java,sockets,server,Java,Sockets,Server,我试着做一个简单的群聊,其中有一个服务器和几个客户端。 目标是当客户机向服务器发送消息时,服务器只是将该消息发送回所有其他客户机。我让服务器发送一条由他编写的消息,发送给所有的客户端,但他没有发送客户端消息 服务器代码: package Group; import java.net.*; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; import

我试着做一个简单的群聊,其中有一个服务器和几个客户端。 目标是当客户机向服务器发送消息时,服务器只是将该消息发送回所有其他客户机。我让服务器发送一条由他编写的消息,发送给所有的客户端,但他没有发送客户端消息

服务器代码:

package Group;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.*;

public class GroupServer extends Thread {
    private ServerSocket server;
    protected List<ClientHandler> clients;

    public static void main(String[] args) throws IOException {
        new GroupServer(9876);
    }

    public GroupServer(int port) {
        try {
            this.server = new ServerSocket(port);
            System.out.println("New server initialized!");
            clients = Collections.synchronizedList(new ArrayList<ClientHandler>());
            this.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void run() {
    while (true) {
        try {
            Socket client = server.accept();
            System.out.println(client.getInetAddress().getHostName() + " connected");
            ClientHandler newClient = new ClientHandler(client);
            clients.add(newClient);
            new SendMessage(clients);

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

class ClientHandler {
protected Socket client;
protected PrintWriter out;
protected DataInputStream in;

public ClientHandler(Socket client) {
    this.client = client;
    try {
        this.out = new PrintWriter(client.getOutputStream());
        this.in = new DataInputStream(client.getInputStream());

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

class SendMessage extends Thread {
    protected List<ClientHandler> clients;
    protected String userInput;
    protected String sendMessage;
    protected BufferedReader stdIn;
    protected DataInputStream in;

    public SendMessage(List<ClientHandler> clients) {
        this.clients = clients;
        this.userInput = null;
        this.start();
    }

    public void run() {
        System.out.println("New Communication Thread Started");
        if (clients.size() == 1) {
            System.out.println("Enter message:");
        }
        try {
            if (clients.size() > 0) {
                this.stdIn = new BufferedReader(new InputStreamReader(System.in));

                while ((this.userInput = stdIn.readLine()) != null) {
                    if (userInput != null & userInput.length() > 0) {
                        for (ClientHandler client : clients) {
                            sendMessage = client.in.readUTF();
                            client.out.println(sendMessage);
                            client.out.flush();
                        Thread.currentThread();
                        Thread.sleep(1 * 1000);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
我认为错误在服务器上,更准确地说是在SendMessage类上。 感谢您的关注。

对于所有客户端,您只有一个线程“SendMessage”

第一次在循环中调用
client.in.readUTF()
时,线程会阻塞,直到该客户机发送了内容。因为所有其他客户端都由同一线程处理。它们也都被封锁了

每个客户端套接字有一个线程,或者使用nio选择器(首选)

同时修复@jingx提到的问题


对于同步arraylist,请使用CopyOnWriteArrayList。它专门针对此类用例。同步有助于并发添加和删除,但在并发迭代期间没有。CopyOnWriteArrayList解决了这个问题。

为什么每次有客户端连接时都要创建一个新的
SendMessage
?另外,如果服务器只应传播客户端消息,那么为什么代码在服务器端(在
SendMessage
中)而不是在客户端运行接受用户输入的代码?如果您仍然有问题,您可以很容易地从我的网络框架中获益:
package Group;
import java.net.*;
import java.io.*;
import java.util.logging.*;

public class GroupClient {
protected Socket client;
protected BufferedReader in;

public static void main(String[] args) {
    new GroupClient("Localhost", 9876);
}

public GroupClient(String hostName, int ip) {
    try {
        this.client = new Socket(hostName, ip);
        this.in = new BufferedReader(new InputStreamReader(
                this.client.getInputStream()));
        String buffer = null;
        while ((buffer = in.readLine()) != null) {
            System.out.println(buffer);
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}       
}