Page 1 of 1

Making an ultra-simple Livecode server and client

Posted: Tue Feb 18, 2014 4:48 pm
by Livecoder
Hi everyone, i'm new to this forum but i'm stuck trying to build a simple server. I'm trying to be able to have a client receiving a variable from the server when a button is clicked. But I don't know where to start. If anyone could help that would be brilliant.

Re: Making an ultra-simple Livecode server and client

Posted: Tue Feb 18, 2014 8:14 pm
by FourthWorld
See the Chat Example included in the LiveCode install - in the IDE, click "Resources" in the toolbar, and you'll find the Internet Chat example near the bottom of the list in that window.

Re: Making an ultra-simple Livecode server and client

Posted: Tue Feb 18, 2014 8:15 pm
by bangkok

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 5:41 pm
by Livecoder
Thank you very much for your replies, I have come up with some code, but it doesn't seem to work could someone possibly have a look at it and tell me what I am doing wrong.

This is the button that connects to the server:

Code: Select all

on mouseUp
   open socket "localhost" & ":*" with message "chatConnected"

 
end mouseUp

on chatReceived s,data
   put data into field "got"
end chatReceived

on chatConnected s 
      read from socket s with message chatReceived
end chatConnected

This is the button that starts the server:

Code: Select all


on mouseUp
   local connected
   accept connections on port * with message chatConnected
   put s into connected
end mouseUp

on chatConnected s
   write "hello" to socket connected
end chatConnected
I am simply trying to get the field "got" to display "hello".

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 5:43 pm
by FourthWorld
I think you'll need to specify a port number rather than attempt all with "*".

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 5:47 pm
by Klaus
FourthWorld wrote:I think you'll need to specify a port number rather than attempt all with "*".
Yep, or just omit it completely if you can live with port 80 :D

From the docs about "socket":
...
Parameters:
host - The IP address or domain name of the host you want to connect to.
port - The port number you want to connect to. If you don't specify a port, port 80 is used.
(On most systems, port 80 is used for HTTP connections.).
...

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 5:50 pm
by Livecoder
FourthWorld wrote:I think you'll need to specify a port number rather than attempt all with "*".
I replaced the port I am using with a "*" for security purposes for when I was positing it on this forum. I do not know if it is safe to post which ports I am using online. Is it?

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 6:11 pm
by Klaus
Oh, sorry, we saw this as a "wildcard" :D

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 6:34 pm
by FourthWorld
Are you testing on a local network, or is the server behind a different router? If the latter you'll need to set up port forwarding on the router to allow connections.

If you've already done that you may need to check the value of "the result" after the connection attempt to see if you can get more info about what's going on.

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 7:18 pm
by Livecoder
Right, I think I found my problem, it comes from the client end,

Code: Select all

   read from socket s with message chatReceived
   answer "read it"

I do not get the pop-up.
What am i doing wrong here?
:?:

By the way, I am using both programs on the same computer

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 7:28 pm
by FourthWorld
Is the port in use by another program?

You may need to check the value of "the result" after the connection attempt to see if you can get more info about what's going on.

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 7:31 pm
by Livecoder
I think I solved that, the problem actually lies in the server end,

Code: Select all

   write "hello" to socket connected
   answer "Sent"

You do not get the sent pop-up, that explains why the client could not read it, it was never sent!
So my real question should be what am I doing wrong on the server end?

I do not know if the port is in use, I tried another one and still no luck

Re: Making an ultra-simple Livecode server and client

Posted: Thu Feb 20, 2014 9:09 pm
by FourthWorld
Sending data from the server is the third thing that happens. First the server sets itself up on the port, then the client connects to it. It may be worthwhile checking the earlier two events.

Re: Making an ultra-simple Livecode server and client

Posted: Tue Feb 25, 2014 3:31 am
by sefrojones
Maybe this example will give you some insight:

https://sites.google.com/a/pgcps.org/li ... uter-games

Good Luck! :wink: