Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java客户端套接字程序中了解客户端端口和ip地址_Java_Sockets_Network Programming_Client Server - Fatal编程技术网

如何在java客户端套接字程序中了解客户端端口和ip地址

如何在java客户端套接字程序中了解客户端端口和ip地址,java,sockets,network-programming,client-server,Java,Sockets,Network Programming,Client Server,我使用socket在java中创建了一个多客户端-服务器通信。我有一个服务器和一个客户端。我想用多个客户端测试我的程序。我计划创建一个模拟器,动态创建ip和端口。。为此,我只想知道如何在套接字程序中设置客户端ip和端口。有人能帮我吗。。我使用InetAddress.getByName获取客户端地址 public Socket(Proxy proxy) { if (proxy != null && proxy.type() == Proxy.Type.SOCKS)

我使用socket在java中创建了一个多客户端-服务器通信。我有一个服务器和一个客户端。我想用多个客户端测试我的程序。我计划创建一个模拟器,动态创建ip和端口。。为此,我只想知道如何在套接字程序中设置客户端ip和端口。有人能帮我吗。。我使用InetAddress.getByName获取客户端地址

 public Socket(Proxy proxy)
  {
     if (proxy != null && proxy.type() == Proxy.Type.SOCKS) 
   { 
    SecurityManager security = System.getSecurityManager();
         InetSocketAddress epoint = (InetSocketAddress) proxy.address();
         if (security != null) {
             if (epoint.isUnresolved())
                epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort());
            if (epoint.isUnresolved())
                security.checkConnect(epoint.getHostName(),
                                      epoint.getPort());
            else
                security.checkConnect(epoint.getAddress().getHostAddress(),
                                      epoint.getPort());
        }
         impl = new SocksSocketImpl(proxy);
        impl.setSocket(this);
    } else {
        if (proxy == Proxy.NO_PROXY) {
            if (factory == null) {
                impl = new PlainSocketImpl();
                impl.setSocket(this);
             } else
                setImpl();
        } else
            throw new IllegalArgumentException("Invalid Proxy");
     }
}
试试这个

clientString = "Remote client: " + socket.getRemoteSocketAddress().toString().substring(1);

简单。要创建客户端IP和端口,您应该在主程序中传递如下参数

就这样试试吧

           java Client localhost 1112 //To run

           (ip: localhost
           port: 1112)

            // Code for main

                 public static void main(String[] args)

            {

          try

             {

                    ipAdd=args[0];

                    portNo=Integer.parseInt(args[1]);

                    Client s=new Client();  

             }

           catch (Exception e)

                {

                        System.out.println(e);
                }

}

这段代码似乎来自java.net.Socket。与你的问题有联系,我想不起来。我不明白你的问题本身。