| 知乎专栏 | 多维度架构 |
package cn.netkiller.ethereum;
import java.io.IOException;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
public class Web3JClient {
// TODO Auto-generated method stub
public static void main(String[] args) {
String url = "http://172.16.0.1:8545/";
Web3j web3j = Web3j.build(new HttpService(url)); // defaults to http://localhost:8545/
try {
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println(clientVersion);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果
Geth/v1.8.8-stable-4bb3c89d/linux-amd64/go1.10.2
除了 TCP 方式连接,还支持 IPC 方式。这种方式比较少用,可以使用 localhost 替代。
// OS X/Linux/Unix:
Web3j web3 = Web3j.build(new UnixIpcService("/path/to/socketfile"));
...
// Windows
Web3j web3 = Web3j.build(new WindowsIpcService("/path/to/namedpipefile"));
...