Librería para enchufes en C.
at main 47 lines 926 B view raw
1#include <stdio.h> 2#include <unistd.h> 3#include "lib/enchufe.h" 4#include "lib/log.h" 5#define BUF_LEN 0x100 6 7int main() { 8 Enchufe enchufe = enchufa((IPv4){127,0,0,1}, htons(42069)); 9 log_info("Connecting to socket.\n"); 10 conecta(enchufe); 11 12 char data1[BUF_LEN] = {0}; 13 Buffer in_buf = { 14 .buf = (Byte*)data1, 15 .len = BUF_LEN, 16 }; 17 18 char data2[BUF_LEN] = {0}; 19 Buffer out_buf = { 20 .buf = (Byte*)data2, 21 .len = BUF_LEN, 22 }; 23 24 log_info("Reading from socket.\n"); 25 26 recibe(enchufe, in_buf); 27 log_info("Received a message: %s", in_buf); 28 memset(in_buf.buf, 0, in_buf.len); 29 30 while (1) { 31 memset(out_buf.buf, 0, out_buf.len); 32 memset(in_buf.buf, 0, in_buf.len); 33 34 log_info("Sending message: "); 35 36 char *r = fgets((char*)out_buf.buf, in_buf.len - 1, stdin); 37 if (r == NULL) try(-1); 38 39 zumba(enchufe, out_buf); 40 recibe(enchufe, in_buf); 41 42 log_info("Received a message: %s", in_buf); 43 } 44 45 desenchufa(enchufe); 46 return 0; 47}