我有一个具有多个客户端连接的Java服务器。如果一个客户端向服务器发送消息,则服务器将该消息广播给所有客户端。我应该在代码中进行哪些更改,以便仅回答发送邮件的客户端?
这里是代码:
EchoServer.java
dotnet publish
EchoClient.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class EchoServer
{
public static void main(String[] args) throws IOException
{
EchoServer server = new EchoServer();
server.start();
}
private ArrayList<PrintWriter> writers = new ArrayList<>();
public void start() {
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(5000);
while (true) {
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
writers.add(out);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Thread t = new ClientHandler(in, this);
t.start();
}
}
catch (IOException e)
{
System.out.println("Exception caught when trying to listen on port 5000 or listening for a connection");
System.out.println(e.getMessage());
}
finally
{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public synchronized void broadcast(String inputLine) {
for (PrintWriter writer : writers) {
writer.println(inputLine);
}
}
}
ServerHandler
import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String[] args) throws IOException
{
String hostName = "localhost";
int portNumber = 5000;
Socket echoSocket = null;
try
{
echoSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
Thread t = new ServerHandler(in);
t.start();
String userInput;
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
}
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host " + hostName);
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to " + hostName);
System.exit(1);
}
finally
{
echoSocket.close();
}
}
}
ClientHandler
import java.io.BufferedReader;
import java.io.IOException;
public class ServerHandler extends Thread {
private BufferedReader reader;
public ServerHandler(BufferedReader reader) {
super();
this.reader = reader;
}
@Override
public void run() {
String readLine;
try {
while((readLine = reader.readLine()) != null) {
System.out.println(readLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
当前,当您收到来自客户端的消息时,您的ClientHandler调用服务器的广播方法。这就是我只回声一位客户的方式。
1)将clientSocket传递到ClientHandler。因此,下面的代码将替换EchoServer中的代码。
Thread t = new ClientHandler(in, clientSocket, this);
t.start();
然后,您的客户端处理程序将有一个套接字,它也可以写回。
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler extends Thread {
private BufferedReader reader;
private Socket clientSocket;
private EchoServer server;
public ClientHandler(BufferedReader reader, Socket clientSocket EchoServer server)
{
this.reader = reader;
this.server = server;
this.clientSocket = clientSocket;
}
@Override
public void run() {
while (true) {
String inputLine;
try {
while ((inputLine = reader.readLine()) != null)
{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(inputLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}