Sockets

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Blanc
Posts: 2
Joined: Fri Dec 22, 2006 12:46 am

Sockets

Post by Blanc » Thu Jan 18, 2007 5:28 pm

I am a new comer to Revolution (but an old user of Hypercard). Till now I was using Applescript and Eudora to let my stacks communicate with a remote server. I would like to use sockets instead, but tried unsuccessfully to use the “write to socketâ€
Blanc

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

Post by Mark » Fri Jan 19, 2007 10:49 am

Hi Blanc,

Your script writes to and reads from the sockets, regardless of information being available in the socket. You need an application on the other side, which accepts a connection and replies. This can be an FTP server, a CGI server, etc, but usually it is easier to connect to such servers using a URL, without explicitly opening sockets. If you are making your own protocol, you might indeed want to make your own script that writes to and reads from sockets. Usually, it means that you also write your own server application. An example of this is ChatRev, where you can log in and ask the people there how sockets work :-)

Here is an example of a client script, which connects to a server:

Code: Select all

on foo
  -- change the following line as necessary
  -- the server ip address before the colon, the port after the colon
  put "192.168.0.1:61010" into mySock
  open socket mySock
  put the result
  write "time" & cr to socket mySock
  wait 200 millisecs
  read from socket mySock until cr with message "gotTime"
end foo

on gotTime theSock,theMsg
  beep
  answer theMsg
  close socket theSock
end gotTime
And here is a the script of the server that responds to the client:

Code: Select all

on startListening
  accept connections on port 61010 with message "receiveRequest"
end startListening

on receiveRequest theSock,theMsg
  read from socket theSock until cr with message "receiveMessage"
end receiveRequest


on receiveMessage theSock,theMsg
  if word 1 to -1 of theMsg is "time" then
    write the long date & cr to socket theSock
  end if
  close socket theSock
end receiveMessage
Just create a client and a server stack. Create a button in the client, which calls the "foo" handler. Create a button in the server, which calls the startListening handler. Open the stacks on two different computers on your local network and press the buttons, the server first. Have fun.

Mark
Last edited by Mark on Tue Jan 30, 2007 10:33 pm, edited 1 time in total.
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

Blanc
Posts: 2
Joined: Fri Dec 22, 2006 12:46 am

Post by Blanc » Wed Jan 24, 2007 2:10 pm

These client and server scripts were very useful to test that the client side was working correctly. The problem, now solved, lay in the server (an IBM mainframe).
Thanks.
Blanc

Post Reply

Return to “Mac OS”