| 知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
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"));
...