creating a socket to connect to existing servers

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Mon Nov 08, 2010 7:03 pm

Hi maverickalex,

You definitely posted your question in the wrong section of the forum. Sockets are NOT a beginners subject.

You will have to ask the FG experts what kind of data the server expects exactly, or read the documentation in detail.

The XML file is NOT the protocol. The protocol is much larger than that. The XML file would just be the content of the data component of the command sent to the server. The command and port etc. are defined by the protocol.

The map is very cool, but it doesn't tell us anything about the techy stuff behind all it.

Yes, since you already have a server, you are now making a client to communicate with it.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Tue Nov 09, 2010 11:12 am

i found a couple more pages possibly that may help explain.

http://wiki.flightgear.org/index.php/Te ... e#Commands

from another page.
FlightGear: Remote control
FlightGear has several interfaces that provide access to internal parameters. The HTTP interface is best suited for human interaction via a web browser. The telnet interface is the ideal choice for remotely controlling FlightGear by means of external programs.

To activate FlightGear's telnet server capabilities, call it with a --telnet specifiaction:

$ fgfs --telnet=socket,bi,5,localhost,5501,tcp



socket: FlightGear protocol
bi: bidirectional
5: polling frequency in Hertz
localhost: server name or IP-address
5501: server port
tcp: internet protocol type



In newer versions of FlightGear just type:
$ fgfs --telnet=5501


To learn more about the supported commands, connect to FlightGear with a telnet program and type in "help<RETURN>". This is what you'll get:


$ telnet localhost 5501
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
help

Valid commands are:

help show help message
ls [<dir>] list directory
dump dump current state (in xml)
cd <dir> cd to a directory, '..' to move back
pwd display your current path
get <var> show the value of a parameter
show <var> synonym for get
set <var> <val> set <var> to a new <val>
data switch to raw data mode
prompt switch to interactive mode (default)
quit terminate connection

/>

Now you can browse in the property system like in a Linux file system with cd, ls, pwd.

Here you can download a sample script written in Perl, that shows how to access and manipulate FlightGear's internal parameters. It can be started before FlightGear (in which case it tries up to 2 minutes to connect) or afterwards. Then it picks a random AGL altitude and checks every 5 seconds if the aircraft has already climbed at this height. Now it starts to empty all four tanks, one after the other, until the engines stop working. Try to find a place where you can land safely. :-)

$ fgfsscript&
$ fgfs --telnet=socket,bi,5,localhost,5501,tcp

The script defaults to localhost and port 5501, but you can let the script control FlightGear on another host and under another port.

$ fgfsscript some.host.org 1234&

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Tue Nov 09, 2010 11:22 am

maverickalex,

Do you have any remaining questions?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Tue Nov 09, 2010 2:52 pm

well, ok. i have connected to the server.

Which command do i put to retrieve data in a readable format?

Would i use the XMLRPC_Create_Request if i want the info?

i tried this command

Code: Select all

on mouseUp
   open socket to"127.0.0.1:5501|ava-acars"
   read from socket sSocket with message "connected to server"
   get data <node>callsign
   put data <node>callsign into field "Event Log"
      close socket pSocket

   

--  put "started" into field "My Status"
  reStart
end mouseUp
and somthing hapens because the word "true" appears in my Event Log

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Tue Nov 09, 2010 3:31 pm

maverickalex,

First, you need to follow my advice regarding socket communications. If you don't, then I can't help you anymore.

Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Tue Nov 09, 2010 3:36 pm

apologies, i thought we had cracket the socket communication side. Just need how to get the info from the server?



{{edited to remove uneeded info}}
Last edited by maverickalex on Tue Nov 09, 2010 4:12 pm, edited 1 time in total.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Tue Nov 09, 2010 4:01 pm

maverickalex,

This isn't a Java forum and I already know exactly what you want and need and how to do it. Any Java code is useless.

I told you, you need to grasp the basics of sockets. I have provided you will all the information you need and told you already what you need to change. Making a simple chat application would be a very good start to understand sockets. Once you understand all the ins and outs of sockets, you can go ahead with your project.

One of the things you really need to understand is how to keep sockets open during a session. It is not that difficult, but you really need to spend a few days with it. As I already said, you can search this forum for ideas and examples. There should be examples in RevOnline as well and also elsewhere on the net. Really, I have nothing new to add to this subject.

Surely, when you make an attempt to get sockets right and you get stuck again, feel free to ask more questions.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Tue Nov 09, 2010 4:46 pm

I know its not a Java forum, (i removed the java stuff) i thought it might assist in getting an idea.

I'm trying to understand sockets i really am and i thank you for your help so far.

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Tue Nov 09, 2010 5:15 pm

I just need the pointer in the right direction. Its difficult being a beginner to understand which bits of info i need.

I at least know i am connected as when i send "help" to the server, in the command box of FG i get.

Code: Select all

pos in msg = -9999,-9999
lat = -9999 lon = -9999
lat_dir = -1  lon_dir = -1
pos in msg = 52,9
lat = 52 lon = 9
lat_dir = 0  lon_dir = 0
progress at least
ps i can't seem to find anything in runrev on keeping the socket open in either the opensocket or closesocket dictionary

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Wed Nov 10, 2010 11:19 am

Code: Select all

on mouseUp
open socket to"127.0.0.1:5501"
open socket to sSocket with message "connected"
end mouseUp

on connected theSock
write "you are connected" & cr to socket theSock
read from socket thesock until cr with message "bar"
end connected

on bar theSock,theData
put theData into fld "Event Log"
close socket theSock
end bar
I think i'm travelling in the right direction?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Wed Nov 10, 2010 11:24 am

maverickalex,

Yes, that looks good. The line

Code: Select all

write "you are connected" & cr to socket theSock
probably doesn't mean anything to the server and at the end of the bar handler you need to read from the same socket again with the bar message.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Wed Nov 10, 2010 11:46 am

Code: Select all

on mouseUp
open socket to"127.0.0.1:5501"
open socket to sSocket with message "connected"
end mouseUp

on connected pSocket
write "connected" & cr to socket sSocket
read from socket pSocket until cr with message "bar"
end connected

on bar pSocket,theData
put theData into fld "Event Log"
close socket sSocket
end bar
hows that look?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Wed Nov 10, 2010 5:31 pm

maverickalex,

That's exactly the same as your previous script (yes, really, because the problems are still the same). Also, sSocket doesn't seem to be defined...?

As I said, don't close the socket but read again with the same message.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: creating a socket to connect to existing servers

Post by maverickalex » Thu Nov 11, 2010 7:42 pm

I think perhaps im over my head, i have spent most of the last few days constantly reading and re-reading. i have just about been through all the posts and lessons involving sockets etc.
I tried asking for help on the FG forums but get the typical answer from the C++/Java etc programmers, what do you use Runrev/Livecode for! its annoying...

Mark i tried using one of your scripts from here to see if i could get any reply from the server:

from here

http://blog.schonewille.tk/telnet-clien ... for-runrev

put couldnt get any reply, i changed the cr to crlf.

so i looked at the script for the card (borrowed from the chat client. and changed couple bits) i now get "success opened"

Code: Select all

local lHostPort

on openCard
  put empty into field "Event Log"
  put "Click the Start button to start" into field "My Status"
end openCard


on reStart
  local W
  if the openSockets <> empty then
    -- would usually do 
    --    resetAll
    -- but this causes problems with testing when you have both a client
    -- and a server within the IDE of Rev    
    -- so here we will only reset the opensockets which have a 'host' part
    -- intending this to be the ones which we have a current 'open'
    repeat for each line sSocket in the opensockets
      if ":" is in sSocket then close socket sSocket
    end repeat
    
    put "reset" into field "My Status"
    wait for 100 millisecs
  end if

  put field "Host" & ":" & field "Port"  into lHostPort
  put "Started - will talk to host & port" && lHostPort into field "My Status"
  
  -- open socket: 
  open socket to lHostPort with message "gotConnOpen"
end reStart

on gotConnOpen pSocket
  put "Success: opened" && psocket into line 1 of field "My Status"
  read from socket pSocket until CR with message "gotPacket"
end gotConnOpen


-- Client mode handlers
on gotPacket pOtherOne
  mylog "Packet arrived" && paramCount() 
  repeat with i=1 to paramCount()
    mylog i && ":" && param(i)
  end repeat
  read from socket pOtherOne until CR with message "gotPacket"
end gotPacket


-- Because this card does the opens, it gets close and error notifications
on socketClosed s
  put "Received socket close " & s into line 2 of field "My Status"
end socketClosed


on socketError pSocket, pError
  put "Received socket error " & pSocket && pError into line 3 of field "My Status"
  put "    " & the opensockets & cr after field "Event Log"
end socketError

on sendPacket
  local t
  put field "Input" into t
  mylog "Sending : " & t
  write t & CR to socket lHostPort
end sendPacket

on mylog pToLog
  put pToLog & CR after field "Event Log"
end mylog

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: creating a socket to connect to existing servers

Post by Mark » Fri Nov 12, 2010 3:22 am

maverickalex,

As you can see in my telnet script and in the Rev dictionary, the message sent when data is read from the socket has two parameters. You need both parameters. I don't understand why you wait 100 millisecs.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply