Page 1 of 1

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

Posted: Fri Feb 12, 2016 1:47 am
by guylouis
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

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

Posted: Tue Feb 16, 2016 10:01 am
by scrabbles
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"

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

Posted: Sun Jun 12, 2016 2:58 pm
by sturgis
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

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

Posted: Sun Jun 12, 2016 6:31 pm
by FourthWorld
Do you have confidence that the arp list is complete? I find arp results sometimes vary in surprising ways.