Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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多客户端简单聊天(非GUI)服务器_Java_Multithreading_Sockets_Serversocket - Fatal编程技术网

使用线程的Java多客户端简单聊天(非GUI)服务器

使用线程的Java多客户端简单聊天(非GUI)服务器,java,multithreading,sockets,serversocket,Java,Multithreading,Sockets,Serversocket,我无法确定如何阻止消息在两个客户端屏幕上出现两次。 实际输出应该是这样的: 运行代码的步骤: 1.在一个终端上运行服务器 2.在两个不同的终端上运行两个客户端 当我运行Server-main方法时,会创建一个服务器对象: public static void main(String[] args) throws IOException { Server server = new Server(); } 服务器构造函数: Server() throws IOE

我无法确定如何阻止消息在两个客户端屏幕上出现两次。

实际输出应该是这样的:

运行代码的步骤:
1.在一个终端上运行服务器
2.在两个不同的终端上运行两个客户端

当我运行Server-main方法时,会创建一个服务器对象:

public static void main(String[] args) throws IOException {
            Server server = new Server();
    }
服务器构造函数:

Server() throws IOException {
            Date dNow = new Date();
            System.out.println("MultiThreadServer started at " + String.format("%tc", dNow));
            System.out.println();

            ServerSocket server = new ServerSocket(8000);
            ClientSockets = new Vector<Socket>();


            while (true) {
                    Socket client = server.accept();
                    AcceptClient acceptClient = new AcceptClient(client);
                    System.out.println("Connection from Socket " + "[addr = " + client.getLocalAddress() + ",port = "
                                    + client.getPort() + ",localport = " + client.getLocalPort() + "] at "
                                    + String.format("%tc", dNow));
                    System.out.println();
                    //System.out.println(clientCount);

            }
            //server.close();
    }
import java.io.IOException;
import java.net.*;
import java.util.Vector;
import java.io.*;
import java.util.*;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.*;
import java.util.Scanner;



public class Server {
    static Vector<Socket> ClientSockets;
    int clientCount = 0;
    //int i = 0;


    Server() throws IOException {
            Date dNow = new Date();
            System.out.println("MultiThreadServer started at " + String.format("%tc", dNow));
            System.out.println();

            ServerSocket server = new ServerSocket(8000);
            ClientSockets = new Vector<Socket>();


            while (true) {
                    Socket client = server.accept();
                    AcceptClient acceptClient = new AcceptClient(client);
                    System.out.println("Connection from Socket " + "[addr = " + client.getLocalAddress() + ",port = "
                                    + client.getPort() + ",localport = " + client.getLocalPort() + "] at "
                                    + String.format("%tc", dNow));
                    System.out.println();
                    //System.out.println(clientCount);

            }
            //server.close();
    }

    public static void main(String[] args) throws IOException {
            Server server = new Server();
    }

    class AcceptClient extends Thread {
            Socket ClientSocket;
            DataInputStream din;
            DataOutputStream dout;

            AcceptClient(Socket client) throws IOException {
                    ClientSocket = client;
                    din = new DataInputStream(ClientSocket.getInputStream());
                    dout = new DataOutputStream(ClientSocket.getOutputStream());

                    //String LoginName = din.readUTF();
                    //i = clientCount;
                    clientCount++;
                    ClientSockets.add(ClientSocket);
                    //System.out.println(ClientSockets.elementAt(i));
                    //System.out.println(ClientSockets.elementAt(1));

                    start();
            }

            public void run() {

                            try {
                                    while (true) {
                                    String msgFromClient = din.readUTF();
                                    System.out.println(msgFromClient);
                                    for (int i = 0; i < ClientSockets.size(); i++) {
                                            Socket pSocket = (Socket) ClientSockets.elementAt(i);
                                            DataOutputStream pOut = new DataOutputStream(pSocket.getOutputStream());
                                            pOut.writeUTF(msgFromClient);
                                            pOut.flush();
                                    }
                            }

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

            }
    }

}之所以这样做,是因为您正在向服务器发送客户端在控制台中写入的内容,服务器将其发送回所有客户端(包括发送方)

因此,您正在控制台中编写一条消息(您可以看到它),然后您将它作为一个客户端接收回来(您可以再次看到它)

一个简单的修复方法是不将刚收到的消息发送回客户端(他已经在控制台中看到了)。将此添加到
Server.AcceptClient#run
方法:

for (int i = 0; i < ClientSockets.size(); i++) {
    Socket pSocket = (Socket) ClientSockets.elementAt(i);

    if(ClientSocket.equals(pSocket)){
        continue;
    }
    ...
for(int i=0;i
for (int i = 0; i < ClientSockets.size(); i++) {
    Socket pSocket = (Socket) ClientSockets.elementAt(i);

    if(ClientSocket.equals(pSocket)){
        continue;
    }
    ...