Page 1 of 2
Sockets
Posted: Wed Jul 26, 2017 6:43 am
by MaxV
I tried this code, but doesn't work, the server doesn't listen:
########CODE to copy and paste#######
on mouseUp
if the hilite of me then
accept connections on port 12345 with message "clientConnected"
else
close socket 12345
end if
end mouseUp
on clientConnected pSocket
read from socket pSocket until "§" with message "messageReceived"
end clientConnected
on messageReceived pSocket, pMsg
answer "<b>Messaggio:</b> " & pMsg
put (char 1 to 10 of pMsg) & "..." into pMsg2
write ("Ricevuto " & pMsg2) to socket pSocket
read from socket pSocket until "§" with message "messageReceived"
end messageReceived
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-5#####
What is wrong?
Re: Sockets
Posted: Wed Jul 26, 2017 12:57 pm
by bogs
Code: Select all
on mouseUp
if the hilite of me then
if the hilite of me... I don't seen a comparator, equals, <> , is empty? Doesn't that need to be in before anything else can happen?
Re: Sockets
Posted: Wed Jul 26, 2017 1:11 pm
by Klaus
Hi Bogs,
no need for a comparator, if you leave that one out, the engine, obviously an optimist, presumes you mean
= TRUE!
Best
Klaus
Re: Sockets
Posted: Wed Jul 26, 2017 1:26 pm
by bogs
Ahhh, I see. Continue on then

Curious though, what happens in the debugger when you step through it?
Re: Sockets
Posted: Wed Jul 26, 2017 1:49 pm
by Klaus
Why don't YOU try and then tell US?

Re: Sockets
Posted: Wed Jul 26, 2017 2:00 pm
by bogs
Thats a fair question. The reason(s) I didn't just try it are [ not in any order ]
1. didn't know which vers. of Lc was being used (this one IS in order).
2. didn't know if any other code not listed was in use that might further affect it
3. don't know the configuration of Max's networking, which is an assumption on my part, but I suspect may have something to do with how code works when reaching out to sockets (firewalls, routers, etc). I could *assume* that he has that particular port open, but I don't *know* it.
4. dunno what the mouseUp refers to, button? field? random graphic object?
Just some of the reasons.
However, when I run it in 6.5.2, using a button, on the mouseUp, the test if/then goes straight to the "close socket", so it is failing in the if/then section and not progressing past the mouse up. His statement looks correct according to the dictionary, however that I always take with a grain of salt, since examples in the dictionary don't appear to be 100% accurate or even all inclusive.
The only bit I could find about the accept statement was
Note: When the accept command creates a socket, it assigns a number as the connection name. If you are using both the open socket command and the accept command to connect to the same port on the same host, make sure to use a non-numeric connection name that won't conflict with the numbers assigned by the accept command. This ensures that you can always refer to two different sockets by distinct socket identifiers.
And it appears he is assigning the number manually, if I'm reading the code correctly. *Edit, I am not apparently reading it correctly

Re: Sockets
Posted: Wed Jul 26, 2017 2:16 pm
by Thierry
bogs wrote:
if the hilite of me... I don't seen a comparator, equals, <> , is empty? Doesn't that need to be in before anything else can happen?
Mmm, no feelings involved in this line of code
an IF statement conditionally executes code depending on the truth value of an expression.
It has the following form:
if ( expression ) then
statement
end if
For instance:
Code: Select all
put true into I_am_in_Holidays
if I_am_in_Holidays then
put "enjoy you day!"
else
put "Work hard; holidays will come..."
end if
Regards,
Thierry
Re: Sockets
Posted: Wed Jul 26, 2017 2:17 pm
by Klaus
Sorry, my fault, I completely misunderstood your last question!
Re: Sockets
Posted: Wed Jul 26, 2017 2:32 pm
by bogs
Thierry wrote:bogs wrote:
if the hilite of me... I don't seen a comparator, equals, <> , is empty? Doesn't that need to be in before anything else can happen?
an IF statement conditionally executes code depending on the truth value of an expression.
It has the following form:
if ( expression ) then
statement
end if
For instance:
Code: Select all
put true into I_am_in_Holidays
if I_am_in_Holidays then
put "enjoy you day!"
else
put "Work hard; holidays will come..."
end if
Regards,
Thierry
Thank you for expounding further on that Thierry, in all the other languages I've used, I've always filled out the complete expression. I never would have guessed that you did not need to return the evaluation, my code would always look like this (using your example):
Code: Select all
...put true into I_am_in_Holidays...
if I_am_in_Holidays = "true" then
// I think, even with what you and Klaus have pointed out here, that for clarity I will still type it this way...
put "enjoy your day!"
else
put "Work hard; holidays will come..."
end if
It is good to know, though, since I will not regard it as a mistake anymore
Klaus wrote:Sorry, my fault, I completely misunderstood your last question!
No problems there at all, my question as I said was merely a curiosity, however if he does go into it, it would give anyone looking at it more information, including himself

Re: Sockets
Posted: Wed Jul 26, 2017 2:54 pm
by Thierry
bogs wrote:
Thank you for expounding further on that Thierry,
in all the other languages I've used,
I've always filled out the complete expression.
and me and some others the opposite...
It is good to know, though, since I will not regard it as a mistake anymore

Great!
that for clarity I will...
This is a personal feeling.
I and quite a lot of coders feel the opposite
Regards,
Thierry
Re: Sockets
Posted: Wed Jul 26, 2017 3:15 pm
by bogs
Yah, to each his own, I absolutely agree. It opened my eyes for sure. As I said I never would have guessed it
Now I will have to stop diverting poor Max's thread, I think I mangled it enough, sorry Max

Re: Sockets
Posted: Wed Jul 26, 2017 7:03 pm
by [-hh]
Hi Max,
you posted the server side only. What did the client side?
H.
Re: Sockets
Posted: Wed Aug 02, 2017 8:08 am
by MaxV
Hi,
I'm sorry but I was busy with work and friend marriages and this car:
https://youtu.be/Z1kaQ0-xYBA https://youtu.be/6t3YEOziAHQ (rented for the marriage)
The
server code is at the end of the code there is the livecode version (use my tool to share code, it's automatically added):
########CODE to copy and paste#######
on mouseUp
if the hilite of me then
accept connections on port 12345 with message "clientConnected"
else
close socket 12345
end if
end mouseUp
on clientConnected pSocket
read from socket pSocket until "§" with message "messageReceived"
end clientConnected
on messageReceived pSocket, pMsg
put the seconds & ": </b> " & pMsg & trturn after field "log"
write ("Ricevuto " & pMsg2) to socket pSocket
read from socket pSocket until "§" with message "messageReceived"
end messageReceived
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-5#####
and the
client code is:
########CODE to copy and paste#######
constant tSocket = "192.168.0.3:12345"
-- Disconnect the client from the broadcast server.
command broadcastClientStop
close socket tSocket
end broadcastClientStop
-- Sent once the client has connected to the broadcaset server.
-- Store the socket for futurure reference and begin reading data
-- from the server.
on broadcastClientConnected
read from socket tSocket until "§" with message "broadcaseClientMessageReceived"
end broadcastClientConnected
-- Sent when a message has been received from the server. Output the
-- message and continue reading data from the server.
on broadcaseClientMessageReceived pSocket, pMsg
put the seconds & ": " & pMsg & return after field "log"
read from socket sSocket until "§" with message "broadcaseClientMessageReceived" #so we continue to listen
end broadcaseClientMessageReceived
-- Sent when there is an error opening the socket. Log the error.
-- and close the socket.
on socketError pSocket, pError
close socket tSocket
put pError & return after field "log"
end socketError
on sendmessage
write (the text of field "mex") to socket tSocket
end sendmessage
on mouseUp
if the hilite of me then
open socket to tSocket with message "broadcastClientConnected"
else
close socket tSocket
end if
end mouseUp
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 8.1.1-rc-1#####
Re: Sockets
Posted: Wed Aug 02, 2017 11:16 am
by [-hh]
After doing the following corrections, your code works here.
- Typos of the code in your post:
replace "trturn" with "return"
replace "pMsg2" with "pMsg"
- Remove error-generating HTML code (bug of your "CodeSharer"?):
replace "§" with "§"
- As the router for the demo may use dynamic IPs, it may be better to use
"localhost:12345" instead of static host-IPs like "192.168.0.3:12345" for host:socket.
Or use something like "put the hostNameToAddress of the hostName into myHost".
- Minor. In handler "socketError" write
"close socket pSocket" instead of "close socket tSocket"
TMHO, a good learning path for using "sockets" is as follows.
(a) Start with
http://livecodeshare.runrev.com/stack/5 ... ket-server
http://livecodeshare.runrev.com/stack/5 ... ket-client
Read the scripts of these stacks and then start the sockets-tutorial
http://lessons.livecode.com/m/4071/l/12 ... ng-sockets
(b) Use the broadcast-part of the sockets-tutorial
to expand the stacks from [a].
http://lessons.livecode.com/m/4071/l/12 ... ast-server
http://lessons.livecode.com/m/4071/l/12 ... ast-client
(c) Then follow the advanced things explained in the sockets-tutorial
Re: Sockets
Posted: Wed Aug 02, 2017 1:39 pm
by MaxV
Thank your for your help,
after many tests on the same PC (just to speed up tests) I found very interesting information and different behaviour between client and server. (see after the codes)
First of all I found this code working:
SERVER:
########CODE to copy and paste#######
local pSocket
on mouseUp
if the hilite of me then
accept connections on port 12345 with message "clientConnected"
else
close socket "12345"
end if
end mouseUp
on clientConnected tSocket
put tSocket into pSocket
put the seconds & " connesso col socket: " & psocket & return after field "log"
read from socket pSocket until "§" with message "messageReceived"
end clientConnected
on messageReceived pSocket, pMsg
put the seconds & space & psocket & ": " & char 1 to -2 of pMsg & return after field "log"
write ("Ricevuto messaggio " & char 1 to -2 of pMsg & "§" ) to socket pSocket
read from socket pSocket until "§" with message "messageReceived"
end messageReceived
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-6#####
CLIENT:
########CODE to copy and paste#######
local pSocket
-- Disconnect the client from the broadcast server.
command broadcastClientStop
close socket pSocket
end broadcastClientStop
-- Sent once the client has connected to the broadcaset server.
-- Store the socket for futurure reference and begin reading data
-- from the server.
on broadcastClientConnected tSocket
put tSocket into pSocket
read from socket pSocket until "§" with message "broadcaseClientMessageReceived"
end broadcastClientConnected
-- Sent when a message has been received from the server. Output the
-- message and continue reading data from the server.
on broadcaseClientMessageReceived pSocket, pMsg
put the seconds & ": " & char 1 to -2 of pMsg & return after field "log"
read from socket pSocket until "§" with message "broadcaseClientMessageReceived" #so we continue to listen
end broadcaseClientMessageReceived
-- Sent when there is an error opening the socket. Log the error.
-- and close the socket.
on socketError pSocket, pError
close socket pSocket
put pError & return after field "log"
end socketError
on sendmessage
write (the text of field "mex") & "§" to socket pSocket
end sendmessage
on mouseUp
if the hilite of me then
open socket to "192.1.1.13:12345" with message "broadcastClientConnected"
else
close socket pSocket
end if
end mouseUp
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-6#####
WHAT'S HAPPENING?
Then I found that when I open a port with code:
Code: Select all
accept connections on port 12345 with message "clientConnected"
I don't get an connection socketID like
192.1.1.13:12345, but a random number like
192.1.1.13:43785.
Moreover on the server I can't close the connection with
or
but the only code working is:
P.S.
the code
Code: Select all
put the hostnametoaddress of the hostname
returns 127.0.0.1, it's useless.
To get the public IP on Linux I use:
Code: Select all
put last word of shell("nm-tool | grep DNS")
it returns 151.99.125.2
to get the private IP (or LAN IP) on Linux I use:
Code: Select all
put shell("hostname -I | cut -d' ' -f1")
it returns 192.1.1.13