Last active
May 6, 2020 13:18
-
-
Save florian-die/39fd5f3c5504ba8749d2366b6adaebb5 to your computer and use it in GitHub Desktop.
Read dump1090 port 30003 ADSB messages line by line (linux, C)
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 <stdio.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> // read | |
// Dump1090 ip + port number | |
#define IP "192.168.1.12" // modify accordingly | |
#define PORT 30003 | |
int main(int argc, char const *argv[]) | |
{ | |
// Initialize socket connection with dump1090 | |
int sock = 0; | |
struct sockaddr_in serv_addr; | |
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
{ | |
printf("\n Socket creation error \n"); | |
return -1; | |
} | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(PORT); | |
// Convert IPv4 and IPv6 addresses from text to binary form | |
if(inet_pton(AF_INET, IP, &serv_addr.sin_addr)<=0) | |
{ | |
printf("\nInvalid address/ Address not supported \n"); | |
return -1; | |
} | |
// Actualy connect to the socket | |
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) | |
{ | |
printf("\nConnection Failed \n"); | |
return -1; | |
} | |
char char_buffer = 0; | |
char str_buffer[200] = {0}; // never seen more than 100 char msg, but safety first | |
int cursor = 0; | |
// the goal is to read the socket one char at a time | |
// then fill the string buffer and ending the string when a newline is catched | |
while(1) | |
{ | |
// reading socket char by char | |
read(sock, &char_buffer, 1); | |
// if char is a new line => end of message | |
if(char_buffer == '\n') | |
{ | |
str_buffer[cursor] = '\0'; // end of string | |
cursor = 0; // reset cursor | |
printf("%s\n",str_buffer); // do what you want | |
} | |
else // char not a new line => message content | |
{ | |
str_buffer[cursor] = char_buffer; // fill char at cursor | |
cursor++; // increment cursor | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment