Skip to content

Instantly share code, notes, and snippets.

@tinybike
Created September 25, 2015 23:34
Show Gist options
  • Select an option

  • Save tinybike/9f4994e8e2bf21d42d2f to your computer and use it in GitHub Desktop.

Select an option

Save tinybike/9f4994e8e2bf21d42d2f to your computer and use it in GitHub Desktop.
geth ipc connection
/**
* geth ipc connection
* @author Jack Peterson ([email protected])
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
const char IPC_PATH[] = "/home/jack/.ethereum/geth.ipc";
int main(void)
{
int sockfd, nbytes;
char buf[256];
struct sockaddr_un addr;
if ( (sockfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
puts("socket() failed");
return 1;
}
bzero(&addr, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), IPC_PATH);
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) != 0) {
puts("connect() failed");
return 1;
}
nbytes = snprintf(buf, 256, "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"eth_coinbase\",\"params\":[]}");
if (!write(sockfd, buf, nbytes)) {
puts("write() failed");
}
nbytes = read(sockfd, buf, 256);
buf[nbytes] = 0;
printf("geth says: %s\n", buf);
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment