有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用多线程服务器向特定客户端发送java消息

我有一个多线程服务器,服务器正在等待所有可能的客户端连接。一旦客户端连接,它就会向服务器发送一个int(12345),服务器读取并显示它。现在,服务器使用使用哈希映射架构的客户端的特定IP地址,并向IP地址匹配的客户端发送消息。但是我的代码被困在while循环中,它没有转到函数messagetospecificclient(),如果它转到,它将显示null。对不起,我英语不好 我的密码是 服务器

public class ServerStart implements Runnable 
    {
        @Override
        public void run() 
        {
            try
            {
                HandleMultipleClients hmc=new HandleMultipleClients();
                hmc.connect();
                hmc.messagetospecificclients("172.20.3.122");
            } 
}
HandleMultipleClients
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package zeeshannisar210;



import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author Zeeshan Nisar
 */
public class HandleMultipleClients{
    Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket> ();
    Socket sock;
    ServerSocket serverSock;
     DataOutputStream dos;
     DataInputStream dis;
     String ip;

  public HandleMultipleClients()
  {

  }
  public void connect()
  {
      try
    {
        serverSock = new ServerSocket(2101);
        while (true) 
        {
            sock = serverSock.accept();
            clients.put(sock.getPort(), sock);
            dis=new DataInputStream(sock.getInputStream());
            int s=dis.readInt();
            System.out.print(s);
            messagetospecificclients(ip);


        }

    }
    catch(Exception e)
    {

    }
  }
  public void messagetospecificclients(String ipaddress) throws IOException
        {

            System.out.print(ipaddress);

            for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext(); )
{
    int key = iter.next();
     System.out.print("ok1");
    ip=ipaddress;
     System.out.print(ip);
     System.out.print("ok2");

    java.net.Socket client = clients.get(key);

    InetAddress zee = client.getInetAddress();
    String s = zee.getHostAddress();
     System.out.print("ok3");
      System.out.print(s);
    if (s.equals(ipaddress))
            {
                System.out.print("ok4");
                dos =new DataOutputStream(client.getOutputStream());
                dos.writeUTF("Some message");

            }
}
        }

    public static void main(String[] args) {
        new HandleMultipleClients();
    }

}
Client code is
 public class messagefromserver implements Runnable 
    {
        @Override
        public void run() 
        {
            try
            {
                sock = new Socket("localhost",2101);
                System.out.println("Success");
                dos=new DataOutputStream(sock.getOutputStream());
                dos.writeInt(12345);
//                Basicinfosend bis=new Basicinfosend(sock);
//                Thread t1=new Thread(bis);
//                t1.start();
//                Thread.sleep(1000);
//                Systeminfosend sis=new Systeminfosend(sock);
//                Thread t2=new Thread(sis);
//                t2.start();
//                Thread.sleep(1000);
//                Thread p = new Thread(new Process());
//                p.start();
                while(true)
                {
                    String s=dis.readUTF();
                    System.out.print(s);
                }
            }

共 (0) 个答案