Bringing back the tiny chain project of learning how block chain stuff works :3
This commit is contained in:
12
socket/Makefile
Normal file
12
socket/Makefile
Normal file
@@ -0,0 +1,12 @@
|
||||
all: setup client-server
|
||||
|
||||
setup:
|
||||
mkdir -p bin/
|
||||
|
||||
client-server:
|
||||
g++ transaction.cc client.cc -o bin/client
|
||||
g++ transaction.cc client.cc -o bin/server
|
||||
|
||||
clean:
|
||||
rm -f bin/server
|
||||
rm -f bin/client
|
||||
37
socket/client.cc
Normal file
37
socket/client.cc
Normal file
@@ -0,0 +1,37 @@
|
||||
// Sample of a client which sends up preconstructed data to our server
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "transaction.h"
|
||||
|
||||
#define PORT 6969
|
||||
|
||||
int main(void) {
|
||||
sockaddr_in server_addr;
|
||||
Transaction sample_ta;
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(sock < 0) {
|
||||
std::cerr << "Could not create socket\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = PORT;
|
||||
if(inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) {
|
||||
std::cerr << "Invalid address\n";
|
||||
return 1;
|
||||
}
|
||||
if(connect(sock, (sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
|
||||
std::cerr << "Conection failed\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
send(sock, &sample_ta, sizeof(Transaction), 0);
|
||||
char buffer[1024] = {0};
|
||||
int response = read(sock, buffer, 1024);
|
||||
std::cout << "BEGIN BUFFER\n" << buffer << "\nEND BUFFER\n";
|
||||
return 0;
|
||||
}
|
||||
13
socket/server.cc
Normal file
13
socket/server.cc
Normal file
@@ -0,0 +1,13 @@
|
||||
// sample server which merely parses out transactions
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "transaction.h"
|
||||
|
||||
|
||||
int main(void) {
|
||||
return 0;
|
||||
}
|
||||
7
socket/transaction.cc
Normal file
7
socket/transaction.cc
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "transaction.h"
|
||||
|
||||
Transaction::Transaction(void) {
|
||||
this->magic = MAGIC;
|
||||
this->meta = 0;
|
||||
this->amount = 0;
|
||||
}
|
||||
9
socket/transaction.h
Normal file
9
socket/transaction.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stdint.h>
|
||||
#define MAGIC 0x54314E59 // T1NY in hex btw
|
||||
|
||||
struct Transaction {
|
||||
uint32_t magic;
|
||||
uint16_t meta;
|
||||
uint64_t amount;
|
||||
Transaction(void);
|
||||
};
|
||||
Reference in New Issue
Block a user