gpt4 book ai didi

java - 如何在java中向特定客户端发送响应?

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

我有一个带有多个客户端连接的java服务器。如果一个客户端向服务器发送一条消息,则服务器会向所有客户端广播该消息。我应该在代码中更改什么,以便它只应答发送消息的客户端?

代码如下:

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

EchoClient.java

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

服务器处理程序

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

客户端处理程序

import java.io.BufferedReader;
import java.io.IOException;

public class ClientHandler extends Thread {
private BufferedReader reader;

private EchoServer server;

public ClientHandler(BufferedReader reader, EchoServer server)
{
this.reader = reader;
this.server = server;
}

@Override
public void run() {
while (true) {
String inputLine;
try {
while ((inputLine = reader.readLine()) != null)
{
server.broadcast(inputLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

最佳答案

目前,当您收到来自客户端的消息时,您的 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();
}
}
}
}

关于java - 如何在java中向特定客户端发送响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56481574/

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