gpt4 book ai didi

java - 从服务器向所有连接的客户端显示在线用户

转载 作者:行者123 更新时间:2023-11-30 04:23:58 25 4
gpt4 key购买 nike

我正在构建简单的套接字服务器客户端应用程序,其中包括:服务器项目(在接受套接字时创建新的客户端处理程序线程)和客户端项目(与服务器建立连接并在消息线程打开的情况下显示 GUI)。

到目前为止,我可以使用多个客户端进行连接并聊天,没有任何问题。我设置了一个名为 !getusers 的命令,该命令应向请求客户端显示所有已连接的用户。当我这样做时,我突然无法继续聊天,客户端只是卡住了,但我确实获得了连接的用户列表。

对于请求,这里是整个客户端代码:

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChatClient extends JFrame implements ActionListener {
String uname;
PrintWriter pw;
BufferedReader br;
JTextArea taMessages, taUserList;
JTextField tfInput;
JButton btnSend, btnExit;
Socket client;

public ChatClient(String uname, String servername) throws Exception {
super("Connected as: " + uname); // set title for frame
this.uname = uname;
client = new Socket(servername, 18524);
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream(), true);
pw.println(uname); // send name to server
//bring up the chat interface
buildInterface();
new MessagesThread().start(); // create thread that listens for messages
}

public void buildInterface() {
btnSend = new JButton("Send");
btnExit = new JButton("Exit");
//chat area
taMessages = new JTextArea();
taMessages.setRows(10);
taMessages.setColumns(50);
taMessages.setEditable(false);
//online users list
taUserList = new JTextArea();
taUserList.setRows(10);
taUserList.setColumns(10);
taUserList.setEditable(false);
//top panel (chat area and online users list
JScrollPane chatPanel = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane onlineUsersPanel = new JScrollPane(taUserList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel tp = new JPanel(new FlowLayout());
tp.add(chatPanel);
tp.add(onlineUsersPanel);
add(tp, "Center");
//user input field
tfInput = new JTextField(50);
//buttom panel (input field, send and exit)
JPanel bp = new JPanel(new FlowLayout());
bp.add(tfInput);
bp.add(btnSend);
bp.add(btnExit);
add(bp, "South");
btnSend.addActionListener(this);
tfInput.addActionListener(this);//allow user to press Enter key in order to send message
btnExit.addActionListener(this);
setSize(500, 300);
setVisible(true);
pack();
}

@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == btnExit) {
pw.println("!end"); // send end to server so that server know about the termination
System.exit(0);
} else if(tfInput.getText().contains("!getusers")){
pw.println("!getusers");
}else{
// send message to server
pw.println(tfInput.getText());
}
}

public static void main(String args[]) {

// take username from user
String name = JOptionPane.showInputDialog(null, "Enter your name: ", "Username",
JOptionPane.PLAIN_MESSAGE);
String servername = "localhost";
try {
new ChatClient(name, servername);
} catch (Exception ex) {
out.println("Unable to connect to server.\nError: " + ex.getMessage());
}

} // end of main

// inner class for Messages Thread
class MessagesThread extends Thread {

@Override
public void run() {
String line;
try {
while (true) {
line = br.readLine();
taMessages.append(line + "\n");
taMessages.setCaretPosition(taMessages.getDocument().getLength());//auto scroll to last message
} // end of while
} catch (Exception ex) {
}
}
}
} // end of client

然后由服务器接受并处理,以下是整个服务器代码:

public class ChatServer {

Vector<String> users = new Vector<String>();
Vector<HandleClient> clients = new Vector<HandleClient>();

public void process() throws Exception {
ServerSocket server = new ServerSocket(18524);
out.println("Server Started...");
while (true) {
Socket client = server.accept();
//add incoming client to connected clients vector.
HandleClient c = new HandleClient(client);
clients.add(c);
} // end of while
}

public static void main(String... args) throws Exception {
new ChatServer().process();
} // end of main

public void broadcast(String user, String message) {
// send message to all connected users
for (HandleClient c : clients) {
c.sendMessage(user, message);
}
}

/*
* Inner class, responsible of handling incoming clients.
* Each connected client will set as it's own thread.
*/
class HandleClient extends Thread {

String name = "";//client name/username
BufferedReader input;//get input from client
PrintWriter output;//send output to client

public HandleClient(Socket client) throws Exception {
// get input and output streams
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream(), true);
// read name
name = input.readLine();
users.add(name); // add to users vector
broadcast(name, " Has connected!");
start();
}

public void sendMessage(String uname, String msg) {
output.println(uname + ": " + msg);
}

public void getOnlineUsers() {
for (HandleClient c : clients) {
for (int i = 0; i < users.size(); i++) {
broadcast("", users.get(i));
}
}
}

public String getUserName() {
return name;
}

public void run() {
String line;
try {
while (true) {
line = input.readLine();
if (line.equals("!end")) {
//notify all for user disconnection
broadcast(name, " Has disconnected!");
clients.remove(this);
users.remove(name);
break;
} else if(line.equals("!getusers")){
getOnlineUsers();
break;
}
broadcast(name, line); // method of outer class - send messages to all
} // end of while
} // try
catch (Exception ex) {
System.out.println(ex.getMessage());
}
} // end of run()
} // end of inner class
} // end of Server

我应该定义一个新的 PrintWriter 对象来处理 onlineUsers 请求吗?我确信我在这里遗漏了一些东西,但尚未弄清楚到底是什么。

最佳答案

啊,我已经解决了这个难题。

public void run() {
String line;
try {
while (true) {
line = input.readLine();
if (line.equals("!end")) {
// Blah
} else if(line.equals("!getusers")){
getOnlineUsers();
break; << This breaks your read loop
}
broadcast(name, line); // method of outer class - send messages to all
} // end of while
} // try
catch (Exception ex) {
System.out.println(ex.getMessage());
}
} // end of run()

run() 循环中的 break 语句终止读取循环,因此服务器不再“监听”客户端。我相信如果你删除break,一切都会好起来的。

关于java - 从服务器向所有连接的客户端显示在线用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16336123/

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