Skip to content

Instantly share code, notes, and snippets.

@codemodify
Last active January 17, 2019 12:12
Show Gist options
  • Save codemodify/70ffdc5f6f873773955dd17566731647 to your computer and use it in GitHub Desktop.
Save codemodify/70ffdc5f6f873773955dd17566731647 to your computer and use it in GitHub Desktop.
qosmicparticles-io-samples.c
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
// libcurl Example
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "http://qosmicparticles.io:4444/FetchGobs");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\n \"version\": \"2.0\",\n \"key\": \"rcd1JN+CkZo2+KKR802bXTujubMbiZARQcyTR8Ku8haqdyaz8pA8Z1kbrWJO2J2CiwFdnr\",\n \"gobSize\": 10\n}");
CURLcode ret = curl_easy_perform(hnd);
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
// Native Sockets Example
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
const char* doPOST(const char* baqeIP, const int baqePort, const char* baqePostPayload);
void baqe_seed_gen(void);
void baqe_seed_gen(void) {
// BAQE setup
const char* baqeIP = "qosmicparticles.io";
const int baqePort = 4444;
const char baqePostPayload[] =
"POST /FetchGobs HTTP/1.1\r\n"
"Host: 127.0.0.1:4444\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 113\r\n\r\n"
"{\"version\": \"2.0\", \"key\": \"rcd1JN+CkZo2+KKR802bXTujubMbiZARQcyTR8Ku8haqdyaz8pA8Z1kbrWJO2J2CiwFdnr\", \"gobSize\": 8}";
const char* postReply = doPOST(baqeIP, baqePort, baqePostPayload);
printf(postReply)
}
#define BAQE_ERROR "BAQE_ERROR"
const char* doPOST(const char* baqeIP, const int baqePort, const char* baqePostPayload) {
// Prep server connection
struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(baqePort);
if (inet_pton(AF_INET, baqeIP, &serverAddress.sin_addr) <= 0) {
writeToDebugFile("inet_pton error occured");
return BAQE_ERROR;
}
// Connect
int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0) {
return BAQE_ERROR;
}
if (connect(socketDescriptor, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) {
return BAQE_ERROR;
}
// Request BAQE
int total = strlen(baqePostPayload);
int sent = 0;
do {
int bytes = write(socketDescriptor, baqePostPayload + sent, total - sent);
if (bytes < 0) {
return BAQE_ERROR;
}
if (bytes == 0)
break;
sent += bytes;
} while (sent < total);
// Read BAQE
char response[4096];
memset(response, 0, sizeof(response));
total = sizeof(response) - 1;
int received = 0;
int bytes = read(socketDescriptor, response + received, total - received);
if (bytes < 0) {
writeToDebugFile("ERROR reading response from socket");
return BAQE_ERROR;
}
if (received == total) {
writeToDebugFile("ERROR storing complete response from socket");
return BAQE_ERROR;
}
// Cleanup
close(socketDescriptor);
return strdup(response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment