gpt4 book ai didi

Java简单服务器客户端

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

我正在用 java 制作一个简单的线程服务器客户端消息传递应用程序。我的问题是,当客户端打开时,服务器会获取消息,但仅将消息回显给发送消息的客户端,而不是其他客户端。如何将其发送到连接到服务器的所有客户端。

服务器代码

import java.io.*;
import java.net.*;
public class ThreadedSever
{
private static final int port=5000;
//Threaded Server
public ThreadedSever(){
System.out.println("A multi-threaded server has started...");
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
}catch(Exception e){
System.out.println("Could not create a server socket: " + e);
}
//Chat with the client until breaks connection or says bye
try{
while(true){
System.out.println("Waiting for client communication");
Socket currentSocket = serverSocket.accept();
//Create a new thread to deal with this connection
new ServerThread(currentSocket);
}

}catch(Exception e){
System.out.println("Fatal Server Error!");
}
}
//Inner class to handle individual commincation
private class ServerThread extends Thread{
private Socket sock;
private DataInputStream in = null;
private DataOutputStream out = null;

public ServerThread(Socket sock){
try{
this.sock = sock;
in = new DataInputStream(this.sock.getInputStream());
out = new DataOutputStream(this.sock.getOutputStream());
System.out.print("New client connection established");
out.writeUTF("Type bye to exit, otherwise, prepare to be echoed");
start();
}catch(Exception e){
System.out.println("Oops");
}
}
public void run(){
try{
String what = new String("");
while(!what.toLowerCase().equals("bye")){
what = in.readUTF();
System.out.println("Client told me: " + what);
out.writeUTF(what);
}
}catch(Exception e){
System.out.println("Connection to current client has been broken");
}
finally{
try{
sock.close();
}catch(Exception e){
System.out.println("Error socket could not be closed");
}
}
}
}
public static void main(){
new ThreadedSever();
}
}

客户端代码

import java.io.*;
import java.net.*;
public class simpleClient
{
private static final int port= 5000;
private static String server = "localhost";
private static Socket socket = null;
private static DataInputStream input = null;
private static DataOutputStream output = null;
private static InputStreamReader inReader = null;
private static BufferedReader stdin = null;

public static void main(){
try{
socket = new Socket(server, port);
}catch(UnknownHostException e){
System.err.println("Unknow IP address for server");
System.exit(-1);
}
catch(IOException e){
System.err.println("No server found at specified port");
System.exit(-1);
}
catch(Exception e){
System.err.println("Something happened!");
System.exit(-1);
}
try{
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
inReader = new InputStreamReader(System.in);
stdin = new BufferedReader(inReader);
String what = new String("");
String response;
while(!what.toLowerCase().equals("bye")){
// Expect something from the server and output it when it arrives
response = input.readUTF();
System.out.println("Server said \"" + response + "\"");
//Read a line from the user and send it to the server
what = stdin.readLine();
output.writeUTF(what);
}
}
catch(IOException e){
System.err.println("Broken connection with server");
System.exit(-1);
}

}
}

最佳答案

您可以在每个ServerThread中定义一个方法echoMessage。每次实例化 ServerThread 时,都会将该实例添加到 ThreadedSever 中的列表中。并且还将 ServerThread 设为 observerServerThread接收数据时,观察者方法应该对ServerThread列表中的每个元素调用echoMessage。

关于Java简单服务器客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47129626/

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