0
我有2個班消息給特定的客戶端使用套接字
public class ServerStart implements Runnable
{
@Override
public void run()
{
try
{
serverSock = new ServerSocket(2101);
while (true)
{
sock = serverSock.accept();
System.out.println(sock);
clients.put(sock.getPort(),sock);
HandleMultipleClients hmc=new HandleMultipleClients();
hmc.messagetospecificclients(String ipaddress,String choice)
}
第二類是
public class HandleMultipleClients
{
Socket soc;
ServerSocket serverSock;
DataOutputStream dos;
DataInputStream dis;
public HandleMultipleClients()
{
}
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);
}
我怎樣才能獲得通過的第2類功能,即messagetospecificclients的循環(字符串ip地址,字符串的選擇)當我將客戶添加到第一堂課時,請幫助我。我的代碼應該是這樣的,我應該添加客戶端到第一類,並應遍歷第二類的for循環