Created
June 9, 2017 06:26
-
-
Save siegfried/3ebcc20383beae58e1fcece7c8fb0e0b to your computer and use it in GitHub Desktop.
Demo C port program for Erlang/Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdint.h> | |
#include <errno.h> | |
#include <arpa/inet.h> | |
int | |
read_packet(unsigned char *buffer) | |
{ | |
ssize_t nr; | |
uint16_t packet_size; | |
if ((nr = read(0, &packet_size, 2)) != 2) { | |
return nr; | |
} | |
packet_size = ntohs(packet_size); | |
return read(0, buffer, packet_size); | |
} | |
int | |
write_packet(unsigned char *buffer, uint16_t packet_size) | |
{ | |
ssize_t nr; | |
uint16_t _packet_size; | |
_packet_size = htons(packet_size); | |
if ((nr = write(1, &_packet_size, 2)) != 2) { | |
return nr; | |
} | |
return write(1, buffer, packet_size); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
unsigned char buffer[65535]; | |
ssize_t nr; | |
while ((nr = read_packet(buffer)) != -1 && nr != 0) { | |
write_packet(buffer, nr); | |
} | |
if (errno) { | |
perror("error"); | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment