Thank you very much everyone! I've been reading all your advice, I made this
program that sends data to FlightGear and my protocol.


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

///   CLIENT

int main(int argc, char *argv[])
{
    printf("This is the client program\n");

    int sockfd;
    char data[1000];
    int len, heading;
    struct sockaddr_in address;
    int result;
    int rc = 1;

   //Create socket for client.
    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        perror("Socket create failed.\n") ;
        return -1 ;
    }
    //Name the socket as agreed with server.
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(7734);
    len = sizeof(address);

    result = connect(sockfd, (struct sockaddr *)&address, len);
    if(result == -1){
        perror("Error has occurred");
        exit(-1);
    }
    heading=0;
    printf("%d ",heading);
    while (heading < 360 && rc !=-1) {

        heading += 20;
        sprintf(data,"%d\n",heading);
        usleep(100000);
        //Read and write via sockfd
        rc = write(sockfd,data, strlen(data));
        printf("%s",data);
    }
    close(sockfd);
    exit(0);
}



<?xml version="1.0"?>
<PropertyList>
  <generic>
    <input>
      <line_separator>tab</line_separator>

       <chunk>
         <name>heading bug</name>
         <type>int</type>
         <node>/autopilot/settings/heading-bug-deg</node>
         <relative>false</relative>
         <min>1</min>
         <max>360</max>
         <wrap>true</wrap>
     </chunk>

   </input>
 </generic>
</PropertyList>

:)


2012/6/25 Geoff McLane <ubu...@geoffair.info>

> Hi Sanny,
>
> If you want to communicate via sockets,
> and 'get' or 'set' properties then this is the
> page -
>
> http://wiki.flightgear.org/Telnet_usage
>
> But to expand that a little
>
> 1. server - Need to run fgfs with a command -
>
>  --telnet=<port>
>
> This connects fgfs to localhost:port, but the read/write
> is only POLLED a few times a second, forget the exact
> default hertz, but it is very SLOW in relative terms...
>
> The full generic 'telnet' command is -
>
>  --telnet=foo,bar,<POLLRATE>,foo,<PORT>,bar
>
> The 'foo' and 'bar' can be anything, and are ignored.
>
> If you want the interface polled at your current frame
> rate, then the POLLRATE used should be greater than
> your frame rate...
>
> I usually use 100, but that does NOT mean it is polled
> faster than your frame rate ;=)) but at least AT your
> frame rate...
>
> 2. client - a tcp client to connect to the PORT
>
> Below I give a working examples in perl, but it should be
> easy to translate that to C/C++, or any other language
> of your choice...
>
> You have to get to 'know' the hundreds of properties
> that can be read, and set. Use fgfs MENU Debug ->
> Browse Internal Properties to see all what is
> available...
>
> Of course setting some property will have
> no effect if over-written by some other control,
> input, like setting the elevators, and then moving
> the joystick, or up-arrow key...
>
> And some properties are 'tied' to others, which can
> effect what happens, and you need to have some awareness
> of what is returned - a string, double, bool, etc...
>
> Lots of fun in learning ;=))
>
> And if thought suitable, maybe the below perl
> tested example could be added to the wiki...
>
> HTH.
>
> Regards,
> Geoff.
>
> <fg-telnet.pl>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> use IO::Socket;
> use Term::ReadKey;  # for ReadMode
>
> # run fgfs with TELNET enabled
> # --telnet=foo,bar,100,foo,5555,bar
>
> # otions
> my $send_run_exit = 0; # if you want fgfs to shut down
>
> my ($FGFS_IO); # Telnet IO handle
>
> my $PORT = 5555;
> my $HOST = 'localhost'; # or IP address
> my $TIMEOUT = 3;
>
> # some properties
> my $get_Altitude = "/position/altitude-ft";
> my $get_Altitude_AGL = "/position/altitude-agl-ft";
> my $get_Latitude_deg = "/position/latitude-deg";
> my $get_Longitude_deg = "/position/longitude-deg";
> my $get_Track = "/orientation/track-magnetic-deg";
> my $nav_stg = "/instrumentation/nav/frequencies/selected-mhz";
> # Did not show - maybe the wrong property...
> #my $hdg_bug_stg = "/autopilot/settings/heading-bug-deg";
> my $hdg_bug_stg = "/instrumentation/heading-indicator/heading-bug-deg";
>
> my @warnings = ();
>
> sub prt($) { print shift; }
> sub prtw($) {
>     my $msg = shift;
>     push(@warnings,$msg);
>     prt($msg);
> }
>
> # ### FG TELNET CREATION and IO ###
> # ==================================
>
> sub fgfs_connect($$$) {
>     my ($host,$port,$timeout) = @_;
>     my $socket;
>     STDOUT->autoflush(1);
>     prt("Connect $host, $port, timeout $timeout secs ");
>     while ($timeout--) {
>         if ($socket = IO::Socket::INET->new(
>                 Proto => 'tcp',
>                 PeerAddr => $host,
>                 PeerPort => $port)) {
>             prt(" done.\n");
>             $socket->autoflush(1);
>             sleep 1;
>             return $socket;
>         }
>         prt(".");
>         sleep(1);
>     }
>     prt(" FAILED!\n");
>     return 0;
> }
>
> sub fgfs_send($) {
>     print $FGFS_IO shift, "\015\012";
> }
>
> sub fgfs_set($$) {
>     my ($node,$val) = @_;
>     fgfs_send("set $node $val");
>     return 1;
> }
>
> sub pgm_exit($$) {
>     my ($val,$msg) = @_;
>     # ...
>     if ($send_run_exit) {
>         fgfs_send("run exit"); # YAHOO! THAT WORKED!!! PHEW!!!
>         sleep(5);
>     }
>     prt("Closing telnet IO ...\n");
>     if (defined $FGFS_IO) {
>         close $FGFS_IO;
>         undef $FGFS_IO;
>     }
>     ReadMode('normal'); # not sure this is required, or what it does
> exactly
>     if (@warnings) {
>         prt(join("\n",@warnings)."\n");
>     }
>     prt($msg) if (length($msg));
>     exit($val);
> }
>
> sub get_exit($) {
>     my ($val) = shift;
>     pgm_exit($val,"fgfs get FAILED!\n");
> }
>
> sub fgfs_get($$) {
>     my ($txt,$rval) = @_;
>     fgfs_send("get $txt");
>     eof $FGFS_IO and return 0;
>     ${$rval} = <$FGFS_IO>;
>     ${$rval} =~ s/\015?\012$//;
>     ${$rval} =~ /^-ERR (.*)/ and (prtw("WARNING: $1\n") and return 0);
>     return 1;
> }
>
> sub fgfs_get_aero($) {
>     my $ref = shift;    # \$aero
>     fgfs_get("/sim/aero", $ref) or get_exit(-2); # string
>     return 1;
> }
>
> ### main ###
> # get the TELENET connection
> $FGFS_IO = fgfs_connect($HOST, $PORT, $TIMEOUT) ||
>    pgm_exit(1,"ERROR: can't open socket! Is FG running, with TELNET
> enabled?\n");
>
> ReadMode('cbreak'); # not sure this is required, or what it does exactly
>
> fgfs_send("data");  # switch fgfs exchange to data mode
>
> # now you can read/write properties
> # =================================
>
> # read a few...
> my ($aero,$alt,$agl,$lat,$lon,$track,$bug,$nav);
> fgfs_get_aero(\$aero); # I like to setup little subs
> fgfs_get($get_Altitude,\$alt) or get_exit(-2); # but direct is ok
> fgfs_get($get_Altitude_AGL,\$agl) or get_exit(-2);
> fgfs_get($get_Latitude_deg,\$lat) or get_exit(-2);
> fgfs_get($get_Longitude_deg,\$lon) or get_exit(-2);
> fgfs_get($get_Track,\$track) or get_exit(-2);
> prt("Presently flying [$aero], at $lat,$lon, $alt feet, $agl AGL, on
> $track\n");
>
> # and say you have set the autopilot to follow the
> # heading bug, or navaid...
> $bug = int($track + 0.5);
> $bug += 20; # swing 20 degrees
> $bug -= 360 if ($bug > 360);
> $nav = '118.5';
> prt("Setting heading bug to $bug, nav to $nav\n");
> fgfs_set($hdg_bug_stg,$bug); # hmmm, no show - maybe wrong property!
> fgfs_set($nav_stg,$nav); # this works...
>
> # etc etc etc until exit
>
> pgm_exit(0,"The End\n");
>
> # eof
> </fg-telnet.pl>
>
> On Sun, 2012-06-24 at 12:04 -0500, Sanny wrote:
> >
> > Hello!
> >
> > My name is Sanny, i'm ecuadorian, i speak spanish and i'm trying to
> > improve my english apologize me for this :)
> >
> > I'm trying to make a flight simulator, but there are certain things
> > that i don't know. I want to have on my computer a client-server
> > communication via socket, I want to send data and modify the
> > properties of the simulator from my socket server, I would like to
> > know what the operation and what should I do.
> >
> > Can you help me? :)
> >
> > Thanks!
> >
> > Sanny :)
>
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Flightgear-devel mailing list
> Flightgear-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/flightgear-devel
>



-- 
Sanny :)
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to