How do I test if an « open socket » is working or not?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
guylouis
Posts: 26
Joined: Mon Feb 01, 2016 11:45 pm

How do I test if an « open socket » is working or not?

Post by guylouis » Fri Feb 12, 2016 1:47 am

Hi,

I have a server accepting connections: accept connections on port "8010" with message ...

All is fine if from the client size I made socket connections on the IP address of the server. Sending and receiving data are fine.

My problem is to discover the IP address of my server.

I make a loop on all IP adresses of connected devices in my network. I open a connection on the socket but I cannot test if the connection is opened or not.

... list contains the list of IPs adresses of connected machines in my network (for this, I use "put shell("arp -n -a") into temp")

constant kPort = 8010
put the number of lines of list into n
repeat with i=1 to n
put "Searching server... (" & i & "/" & n & ")."
put line i of list into searchIP
put searchIP&":"&kPort into ts
open socket to ts
put the result into rs
// the result is always empty for all machines, thus I cannot see if the socket is open
end repeat

in the "How to communicate with other applications using sockets" tutorial, it's written that:

"If there has been an error connecting to the server the "socketError" message will be sent. (If the error is due to a problem finding the specified host, the error message is returned in the result, and no "socketError" message is sent.)"

Thus I add a socket error handler:

on socketError pSocket, pError
answerr pError
end socketError

NEVER call this handler.

Thus, my question is "How do I test that an open socket is working or not"?

Regards, Guy

scrabbles
Posts: 25
Joined: Sat Dec 20, 2014 4:32 am
Location: Melbourne, Australia

Re: How do I test if an « open socket » is working or not?

Post by scrabbles » Tue Feb 16, 2016 10:01 am

hi,
Do you need to add a callback to your "open socket" command? I see in the dictionary "If the callback parameter is specified then the call will return immediately and upon completion of the lookup, the callback will be invoked with the resolved address as a parameter"

You might be able to also (before invoking open socket) set a timeout "set the socketTimeoutInterval to milliseconds"

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do I test if an « open socket » is working or not?

Post by sturgis » Sun Jun 12, 2016 2:58 pm

In your repeat loop you put the result into rs. Every time you do that, it overwrites the previous value. If you want to keep a list of results look at "after" or "before" rather than into...

something like

Code: Select all

repeat however you need
--do your stuff here

put the result & cr after rs
end repeat
delete the last char of rs -- removes the trailing cr
You might also consider including "it" in your results list..

Code: Select all

put the result && it & cr after cr
Sometimes useful information goes to "it" rather than the result.

Finally, you can check the opensockets as a more direct method of seeing which sockets exist.

using the opensockets is a handy way to prepare to close your app..

repeat for each line tLine in the opensockets
close socket tLine
end repeat

You might check out the chatrev example so you can see a working chat client server application.
guylouis wrote:Hi,

I have a server accepting connections: accept connections on port "8010" with message ...

All is fine if from the client size I made socket connections on the IP address of the server. Sending and receiving data are fine.

My problem is to discover the IP address of my server.

I make a loop on all IP adresses of connected devices in my network. I open a connection on the socket but I cannot test if the connection is opened or not.

... list contains the list of IPs adresses of connected machines in my network (for this, I use "put shell("arp -n -a") into temp")

constant kPort = 8010
put the number of lines of list into n
repeat with i=1 to n
put "Searching server... (" & i & "/" & n & ")."
put line i of list into searchIP
put searchIP&":"&kPort into ts
open socket to ts
put the result into rs
// the result is always empty for all machines, thus I cannot see if the socket is open
end repeat

in the "How to communicate with other applications using sockets" tutorial, it's written that:

"If there has been an error connecting to the server the "socketError" message will be sent. (If the error is due to a problem finding the specified host, the error message is returned in the result, and no "socketError" message is sent.)"

Thus I add a socket error handler:

on socketError pSocket, pError
answerr pError
end socketError

NEVER call this handler.

Thus, my question is "How do I test that an open socket is working or not"?

Regards, Guy

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How do I test if an « open socket » is working or not?

Post by FourthWorld » Sun Jun 12, 2016 6:31 pm

Do you have confidence that the arp list is complete? I find arp results sometimes vary in surprising ways.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Internet”