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循环