Page 1 of 2

Socket not working/connecting on another computer

Posted: Tue May 01, 2018 3:02 pm
by agraham147
Hi, I've got my two programs sockets working perfectly on the same computer, but I've tried to put one on another computer and they won't connect with each other, yet both programs work perfectly on the same computer. This is my code for the accept connections and open socket. Do I need to change anything or is localhost just for the same computer?

Server code:

Code: Select all

accept connections on port 8080 with message "someoneConnected"
Client code:

Code: Select all

open socket to "localhost:8080"
if the Result <> "" then
      put "result:" && the result
      set visible of field "disconnectedLabel" of card "scoreboard" to false
   end if
Would really appreciate some help, thanks!

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 3:59 pm
by bogs
Localhost is just a way of talking about accessing what ever network services the machine you are on is hosting through the loopback adapter (a virtual network card). So yes, you'd have to change the socket opening to the machine you want to connect to and port and it should work.

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 4:39 pm
by agraham147
Thanks very much, but could you try reposting the images or code you sent to see what you mean please, cause I can't see them?

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 6:37 pm
by FourthWorld
Is this to be limited to communication between systems on your local network, or do you anticipate needing to connect over the Internet?

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 7:31 pm
by agraham147
No internet needed at the minute for my system, just communication between the computers on the network.

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 7:34 pm
by bogs
agraham147 wrote:
Tue May 01, 2018 4:39 pm
could you try reposting the images or code you sent to see what you mean
Um, there are no pictures in my post, it is a link to an article explaining what the term 'Localhost' means. To see the article, simply click on the link.

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 7:35 pm
by agraham147
Oh ok, it's just it's coming up with image then a cross both times, no link unfortunately

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 7:42 pm
by FourthWorld
FWIW this is my favorite go-to lesson for getting started with socket comms in LiveCode:

http://lessons.livecode.com/m/4071/l/12 ... ng-sockets

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 8:01 pm
by agraham147
I have tried opening a socket to the other computer. I have looked up its IP address and coded it like this:

Code: Select all

open socket to "127.45.154.259:8080"
   if the Result <> "" then
      put "result:" && the result
      set visible of field "disconnectedLabel" of card "scoreboard" to false
   end if
It doesn't make a connection and that is definitely the IP address but also IP address change if you are using your software somewhere else so how can code it to find the IP address you are currently in?

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 8:20 pm
by bogs
127.x is a very unusual address, which sounds more like a router assigned one (makes sense if your on a local network).

If you (on that system) go to whatismyip.com, what does that report your address as?

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 9:31 pm
by FourthWorld
agraham147 wrote:
Tue May 01, 2018 8:01 pm
...IP address change if you are using your software somewhere else so how can code it to find the IP address you are currently in?
DNS. :)

Or to discover local services, Bonjour or other discovery protocol.

Most discovery protocols are UDP-based, and for whatever reason I've never wrapped my head around UDP well enough to write a good discovery mechanism, though I have with TCP by testing ports across an IP range. Sure, not recommended, but it works so who am I to complain.

I keep threatening to finish that up so it could become useful to others, but two things stop me:

1. UDP is the better method for local discovery, so on the rare day when I have a moment to think about that problem I find myself thinking about UDP instead of TCP.

2. I rarely have any interest in the problem at all any more at all, because P2P is only really useful on local nets, and most of what I'm working on needs to work both locally and on the Internet, where client-server is much more reliable and easier to build for.

In your case, if you're certain your system will never need to work across the Internet, local network discovery via UDP is widely considered your best bet.

But if there's any chance at all you may need to cross over to the Internet, client-server makes everything so much simpler, robust, predictable, monitorable, manageable, scalable, and every other adjective I can think of that I can't recommend it enough.

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 10:32 pm
by agraham147
In replying to Bogs, yes my domain has changed to 90.206.191.28 I will try it on my program. But in replying to Fourth World, how would I get my server and client to go through a DNS to get whatever IP address my programs are connected to, in other words how would I code it so my clients and server connect over any network and is this what other client/server programs have to do if they are used in different places?

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 10:40 pm
by bogs
I think a lot of this is going to depend on the router your going through. You usually have to enable (even for data passed between 2 systems connected to it) the router to allow the traffic to pass in both directions, on some routers you can be more specific than others, some less so.

In the router config, you should be able to find the different setups for UDP, TCP, etc., although it has been a long time since I set one up for these kinds of specific tasks. Once your router is setup correctly, both computers will have to have (in this case port 8080) open and confgured for listening and response.

Re: Socket not working/connecting on another computer

Posted: Tue May 01, 2018 11:05 pm
by agraham147
Isn't there a function in LiveCode which can return the IP address of the router that you're currently connected to the Internet on? And then just put that in a variable and code it to open a socket to that IP Address and port 8080, as long as the server is connected to the same router and port number (which in my case it will be)?

Re: Socket not working/connecting on another computer

Posted: Wed May 02, 2018 12:10 pm
by zaxos
Hi agraham147.
The attached stack will search your network and will try to connect to any socket that accepts connection on that port. I would suggest that you use a different port than 8080 since it is a very common port and it's being used by many programs. Other than that you can get the ip of your current computer with the folowing livecode command:

Code: Select all

put the hostnameToAddress of the hostname