gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:21:44 25 4
gpt4 key购买 nike

我无法弄清楚如何阻止该消息在客户端的屏幕上出现两次。

enter image description here

实际输出应该是这样的:
enter image description here

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

当我运行服务器时 - 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();
}

我正在使用 Socket 连接到服务器。这是我的服务器代码。

Server.java

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();
}

}
}

}

Client.java

import java.net.Socket;
import java.util.Scanner;
import java.io.*;
import java.net.*;


public class Client implements Runnable{

Socket socketConnection;
DataOutputStream outToServer;
DataInputStream din;


Client() throws UnknownHostException, IOException {

socketConnection = new Socket("127.0.0.1", 8000);
outToServer = new DataOutputStream(socketConnection.getOutputStream());
din = new DataInputStream(socketConnection.getInputStream());


Thread thread;
thread = new Thread(this);
thread.start();

BufferedReader br = null;
String ClientName = null;
Scanner input = new Scanner(System.in);
String SQL = "";
try {
System.out.print("Enter you name: ");
ClientName = input.next();
ClientName += ": ";
//QUERY PASSING

br = new BufferedReader(new InputStreamReader(System.in));
while (!SQL.equalsIgnoreCase("exit")) {
System.out.println();
System.out.print(ClientName);

SQL = br.readLine();
//SQL = input.next();
outToServer.writeUTF(ClientName + SQL);
//outToServer.flush();
//System.out.println(din.readUTF());

}

} catch (Exception e) {
System.out.println(e);
}
}

public static void main(String[] arg) throws UnknownHostException, IOException {
Client client = new Client();
}


public void run() {
while (true) {
try {
System.out.println("\n" + din.readUTF());

} 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;
}
...

关于java - 使用线程的 Java 多客户端简单聊天(非 GUI)服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49869553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com