gpt4 book ai didi

java - 将字符串传递给 InetSocketAddress

转载 作者:行者123 更新时间:2023-12-02 07:08:53 25 4
gpt4 key购买 nike

有没有办法将字符串参数传递给 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?

最佳答案

您还必须将端口传递给构造函数,而不仅仅是 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);
}

关于java - 将字符串传递给 InetSocketAddress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com