Sockets
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Sockets
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?
########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?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: Sockets
Code: Select all
on mouseUp
if the hilite of me then

Re: Sockets
Hi Bogs,
no need for a comparator, if you leave that one out, the engine, obviously an optimist, presumes you mean = TRUE!
Best
Klaus
no need for a comparator, if you leave that one out, the engine, obviously an optimist, presumes you mean = TRUE!

Best
Klaus
Re: Sockets
Ahhh, I see. Continue on then 
Curious though, what happens in the debugger when you step through it?

Curious though, what happens in the debugger when you step through it?

Re: Sockets
Why don't YOU try and then tell US? 

Re: Sockets
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
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
And it appears he is assigning the number manually, if I'm reading the code correctly. *Edit, I am not apparently reading it correctlyNote: 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.

Last edited by bogs on Wed Jul 26, 2017 2:40 pm, edited 2 times in total.

Re: Sockets
Mmm, no feelings involved in this line of codebogs 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
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Sockets
Sorry, my fault, I completely misunderstood your last question!
Re: Sockets
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):Thierry wrote:an IF statement conditionally executes code depending on the truth value of an expression.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?
It has the following form:
if ( expression ) then
statement
end if
For instance:Regards,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
Thierry
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

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 himselfKlaus wrote:Sorry, my fault, I completely misunderstood your last question!


Re: Sockets
and me and some others the opposite...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.
Great!It is good to know, though, since I will not regard it as a mistake anymore
This is a personal feeling.that for clarity I will...
I and quite a lot of coders feel the opposite

Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Sockets
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

Now I will have to stop diverting poor Max's thread, I think I mangled it enough, sorry Max


Re: Sockets
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#####
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#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: Sockets
After doing the following corrections, your code works here.
(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
- 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"
(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
shiftLock happens
Re: Sockets
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:
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
returns 127.0.0.1, it's useless.
To get the public IP on Linux I use:
it returns 151.99.125.2
to get the private IP (or LAN IP) on Linux I use:
it returns 192.1.1.13
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"

Moreover on the server I can't close the connection with
Code: Select all
close socket "192.1.1.13:12345"
Code: Select all
close socket "192.1.1.13"
Code: Select all
close socket "12345"


P.S.
the code
Code: Select all
put the hostnametoaddress of the hostname
To get the public IP on Linux I use:
Code: Select all
put last word of shell("nm-tool | grep DNS")
to get the private IP (or LAN IP) on Linux I use:
Code: Select all
put shell("hostname -I | cut -d' ' -f1")
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w