假設我有3個課程。類是Login,HandleMultipleClients和LiveMonitoring類。在登錄類中,我將服務器連接到多個客戶端併發送套接字來處理多個類在這個類中,我製作了連接到服務器並將這些客戶端放入數組的客戶端列表。在HandleMultipleClients中,還有一個函數將消息發送給基於ipaddress的特定客戶端。現在,在實時監控類中,我希望將消息發送到特定的客戶端,我將handlemultipleclients作爲新的Handlemultipleclients()調用,並創建一個對象,以便這個新對象沒有任何如上所述的客戶端列表。我想知道那裏是否存在由我不任何方法需要做出handlemultipleclients類的新對象 我的代碼是使用課程而不製作新實例
public class Login implements Runnable {
@Override
public void run() {
try {
serverSock = new ServerSocket(2101);
while (true) {
sock = serverSock.accept();
LiveMOnitoring l=new LiveMOnitoring(sock);
System.out.println(sock);
//clients.put(soc.getPort(),soc);
}
}
}
}
public HandleMultipleClients(Socket sock) {
soc=sock;
clients.put(soc.getPort(),soc);
}
public void messagetospecificclients(String ipaddress,String choice) throws IOException, InterruptedException {
System.out.print(ipaddress+"\n"+choice);
for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext();) {
System.out.print("ok1");
int key = iter.next();
java.net.Socket client = clients.get(key);
InetAddress zee = client.getInetAddress();
String s = zee.getHostAddress();
System.out.print(s);
if (zee.getHostAddress().equals(ipaddress)) {
System.out.print("ok2");
dos =new DataOutputStream(client.getOutputStream());
dos.writeUTF(choice);
}
}
}
public class LiveMOnitoring extends javax.swing.JFrame {
static Socket csocket;
DataOutputStream dos;
Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket>();
private void ApplicationsActionPerformed(java.awt.event.ActionEvent evt) {
h.messagetospecificclients();
}
}