简体   繁体   English

不同类之间的函数调用

[英]function call among different classes

I have three separate classes that are login.java, LiveMonitoring.java, and HandleMultipleClients.java. 我有三个单独的类,分别是login.java,LiveMonitoring.java和HandleMultipleClients.java。 Following is the code of all respective classes 以下是所有相应类的代码

Login.java Login.java

 try
            {
                serverSock = new ServerSocket(2101);
                while (true) 
                {
                    sock = serverSock.accept();
                    HandleMultipleClients hmc=new HandleMultipleClients();
                    hmc.Connect(sock); 
                }

HandleMultipleClients.java HandleMultipleClients.java

public class HandleMultipleClients
{
    Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket> ();
    Socket soc;
    ServerSocket serverSock;
    DataOutputStream dos;
    DataInputStream dis;
    public HandleMultipleClients()
    {

    }
    public void Connect(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);
            }

LiveMonitoring.java LiveMonitoring.java

HandleMultipleClients hmc=new HandleMultipleClients();
        try
        {
            hmc.messagetospecificclients("192.168.1.102","apps");
        }

Now the problem is when i call hmc.messagetospecificclients("192.168.1.102","apps"); 现在的问题是,当我调用hmc.messagetospecificclients(“ 192.168.1.102”,“ apps”); it goes to the HandleMultipleClients class and just print the IPaddress and apps but it is not going through the for loop.Kindly tell me what i am doing wrong. 它进入HandleMultipleClients类,仅打印IPaddress和应用程序,但没有通过for循环。请告诉我我在做什么错。

You're creating an instance of the object and adding a "client" to it here: 您正在创建对象的实例,并在此处添加“客户端”:

HandleMultipleClients hmc=new HandleMultipleClients();
hmc.Connect(sock);

But you never call messagetospecificclients() on that instance. 但是您永远不要在该实例上调用messagetospecificclients()

Then you create another instance and call that method here: 然后创建另一个实例并在此处调用该方法:

HandleMultipleClients hmc=new HandleMultipleClients();
try
{
    hmc.messagetospecificclients("192.168.1.102","apps");
}

But you never called Connect() on that instance to add clients to it. 但是您从未在实例上调用Connect()向其添加客户端。

(As an analogy... Imagine that you have two identical cars. Same make, same model, came off the same production line. You have put gas in one of those cars. And you're trying to drive the other one. But you can't, because it doesn't have gas in it.) (比喻……想像一下,您有两辆相同的汽车。相同的制造商,相同的型号,来自同一生产线。您在其中一辆汽车中加了汽油。而您正试图驾驶另一辆汽车。但是您不能,因为里面没有气体。)

Call messagetospecificclients() on the instance which has the values that have been set: 在具有已设置值的实例上调用messagetospecificclients()

HandleMultipleClients hmc=new HandleMultipleClients();
hmc.Connect(sock);
try
{
    hmc.messagetospecificclients("192.168.1.102","apps");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM