| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <errno.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <sys/time.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netdb.h> |
| 14 |
#include <netinet/in.h> |
| 15 |
#include <stdarg.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
|
| 19 |
#define DFLTHOST "localhost" |
| 20 |
#define DFLTPORT 5501 |
| 21 |
#define MAXMSG 256 |
| 22 |
#define fgfsclose close |
| 23 |
|
| 24 |
|
| 25 |
void init_sockaddr(struct sockaddr_in *name, const char *hostname, unsigned port); |
| 26 |
int fgfswrite(int sock, char *msg, ...); |
| 27 |
const char *fgfsread(int sock, int wait); |
| 28 |
void fgfsflush(int sock); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
int fgfswrite(int sock, char *msg, ...) |
| 33 |
{ |
| 34 |
va_list va; |
| 35 |
ssize_t len; |
| 36 |
char buf[MAXMSG]; |
| 37 |
|
| 38 |
va_start(va, msg); |
| 39 |
vsnprintf(buf, MAXMSG - 2, msg, va); |
| 40 |
va_end(va); |
| 41 |
printf("SEND: \t<%s>\n", buf); |
| 42 |
strcat(buf, "\015\012"); |
| 43 |
|
| 44 |
len = write(sock, buf, strlen(buf)); |
| 45 |
if (len < 0) { |
| 46 |
perror("fgfswrite"); |
| 47 |
exit(EXIT_FAILURE); |
| 48 |
} |
| 49 |
return len; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
const char *fgfsread(int sock, int timeout) |
| 55 |
{ |
| 56 |
static char buf[MAXMSG]; |
| 57 |
char *p; |
| 58 |
fd_set ready; |
| 59 |
struct timeval tv; |
| 60 |
ssize_t len; |
| 61 |
|
| 62 |
FD_ZERO(&ready); |
| 63 |
FD_SET(sock, &ready); |
| 64 |
tv.tv_sec = timeout; |
| 65 |
tv.tv_usec = 0; |
| 66 |
if (!select(32, &ready, 0, 0, &tv)) |
| 67 |
return NULL; |
| 68 |
|
| 69 |
len = read(sock, buf, MAXMSG - 1); |
| 70 |
if (len < 0) { |
| 71 |
perror("fgfsread"); |
| 72 |
exit(EXIT_FAILURE); |
| 73 |
} |
| 74 |
if (len == 0) |
| 75 |
return NULL; |
| 76 |
|
| 77 |
for (p = &buf[len - 1]; p >= buf; p--) |
| 78 |
if (*p != '\015' && *p != '\012') |
| 79 |
break; |
| 80 |
*++p = '\0'; |
| 81 |
return strlen(buf) ? buf : NULL; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void fgfsflush(int sock) |
| 87 |
{ |
| 88 |
const char *p; |
| 89 |
while ((p = fgfsread(sock, 0)) != NULL) { |
| 90 |
printf("IGNORE: \t<%s>\n", p); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
int fgfsconnect(const char *hostname, const int port) |
| 97 |
{ |
| 98 |
struct sockaddr_in serv_addr; |
| 99 |
struct hostent *hostinfo; |
| 100 |
int sock; |
| 101 |
|
| 102 |
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 103 |
if (sock < 0) { |
| 104 |
perror("fgfsconnect/socket"); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
hostinfo = gethostbyname(hostname); |
| 109 |
if (hostinfo == NULL) { |
| 110 |
fprintf(stderr, "fgfsconnect: unknown host: \"%s\"\n", hostname); |
| 111 |
close(sock); |
| 112 |
return -2; |
| 113 |
} |
| 114 |
|
| 115 |
serv_addr.sin_family = AF_INET; |
| 116 |
serv_addr.sin_port = htons(port); |
| 117 |
serv_addr.sin_addr = *(struct in_addr *)hostinfo->h_addr; |
| 118 |
|
| 119 |
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { |
| 120 |
perror("fgfsconnect/connect"); |
| 121 |
close(sock); |
| 122 |
return -3; |
| 123 |
} |
| 124 |
return sock; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
int main(int argc, char **argv) |
| 130 |
{ |
| 131 |
int sock; |
| 132 |
unsigned port; |
| 133 |
const char *hostname, *p; |
| 134 |
|
| 135 |
hostname = argc > 1 ? argv[1] : DFLTHOST; |
| 136 |
port = argc > 2 ? atoi(argv[2]) : DFLTPORT; |
| 137 |
|
| 138 |
sock = fgfsconnect(hostname, port); |
| 139 |
if (sock < 0) |
| 140 |
return EXIT_FAILURE; |
| 141 |
|
| 142 |
fgfswrite(sock, "data"); |
| 143 |
fgfswrite(sock, "set /controls/engines/engine[%d]/throttle %d", 0, 1); |
| 144 |
fgfswrite(sock, "get /sim/aircraft"); |
| 145 |
p = fgfsread(sock, 3); |
| 146 |
if (p != NULL) |
| 147 |
printf("READ: \t<%s>\n", p); |
| 148 |
fgfswrite(sock, "quit"); |
| 149 |
fgfsclose(sock); |
| 150 |
return EXIT_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
|