本质上,我构建了一个套接字服务器,多个客户端可以连接到该服务器,当它们向服务器发送消息时,该消息会发送给所有其他客户端。问题是它还没有发挥作用。我需要整理“SingletonClients”和“getClients”,以便它们返回所有已连接的用户,以便我向他们发送消息。这只是一个基本问题,但我根本不知道如何去做,这是我正在做的学生项目的最后一部分(主要部分是 Obj-C 客户端而不是 Java 服务器)。如果有人能为我做到这一点,我将永远感激不已。
这是迄今为止的代码:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedList;
import java.io.*;
import java.net.*;
class ClientWorker implements Runnable {
private Socket client;
private JTextArea textArea;
BufferedReader in = null;
PrintWriter out;
ClientWorker(Socket client, JTextArea textArea) {
this.client = client;
this.textArea = textArea;
String line = in.readLine();
LinkedList<ClientWorker> clients = SingletonClients.getClients();
for(int i = 0; i < clients.size(); i++) {
ClientWorker c = clients.get(i);
//The client doesn't need to get it's own data back.
if(c == this){
continue;
}
c.writeString(line);
}
}
public void writeString(String s) {
try {
out.println(s);
} catch(IOException ex) {
}
}
public void run(){
String line;
out = null;
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("in or out failed");
System.exit(-1);
}
while(true){
try{
line = in.readLine();
//Send data back to client
out.println(line);
textArea.append(line);
} catch (IOException e) {
System.out.println("Read failed");
System.exit(-1);
}
}
}
}
class SocketThrdServer extends JFrame{
JLabel label = new JLabel("Text received over socket:");
JPanel panel;
JTextArea textArea = new JTextArea();
ServerSocket server = null;
SocketThrdServer(){ //Begin Constructor
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", label);
panel.add("Center", textArea);
} //End Constructor
public void listenSocket(){
try{
server = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
while(true){
ClientWorker w;
try{
w = new ClientWorker(server.accept(), textArea);
Thread t = new Thread(w);
t.start();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
}
protected void finalize(){
//Objects created in run method are finalized when
//program terminates and thread exits
try{
server.close();
} catch (IOException e) {
System.out.println("Could not close socket");
System.exit(-1);
}
}
public static void main(String[] args){
SocketThrdServer frame = new SocketThrdServer();
frame.setTitle("Server Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
提前致谢!
最佳答案
LinkedList<ClientWorker> clients = SingletonClients.getClients();
由于您似乎需要返回 ClientWorker 集合的内容,因此我建议您查看代码以查找创建 ClientWorker 的位置,并尝试将它们放入集合中。
这有意义吗?
关于java - 需要服务器和链接列表帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216698/