将字符串传递给InetSocketAddress

时间:2013-04-02 23:32:54

标签: java string arguments

有没有办法将字符串参数传递给InetSocketAddress的参数?

这是生成代理的正确方法:

SocketAddress addr = new InetSocketAddress("127.0.0.1", 9050);  
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);  

但是我想创建一个方法来接受这样的字符串:

method("127.0.0.1",9050);  

public void method (String a){  
SocketAddress addr = new InetSocketAddress(a); //can't place string here is there away forit work?  
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);    
}  

有没有办法让新InetSocketAddress获取字符串IP?

1 个答案:

答案 0 :(得分:2)

您还必须将端口传递给构造函数,而不仅仅是IP地址:

method("127.0.0.1",9050);

public void method (String a, int port){  
    SocketAddress addr = new InetSocketAddress(a, port);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);    
}