Socket not working/connecting on another computer

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 3:02 pm

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!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Socket not working/connecting on another computer

Post by bogs » Tue May 01, 2018 3:59 pm

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.
Image

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Re: Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 4:39 pm

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?

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

Re: Socket not working/connecting on another computer

Post by FourthWorld » Tue May 01, 2018 6:37 pm

Is this to be limited to communication between systems on your local network, or do you anticipate needing to connect over the Internet?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Re: Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 7:31 pm

No internet needed at the minute for my system, just communication between the computers on the network.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Socket not working/connecting on another computer

Post by bogs » Tue May 01, 2018 7:34 pm

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.
Image

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Re: Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 7:35 pm

Oh ok, it's just it's coming up with image then a cross both times, no link unfortunately

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

Re: Socket not working/connecting on another computer

Post by FourthWorld » Tue May 01, 2018 7:42 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Re: Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 8:01 pm

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?

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Socket not working/connecting on another computer

Post by bogs » Tue May 01, 2018 8:20 pm

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?
Image

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

Re: Socket not working/connecting on another computer

Post by FourthWorld » Tue May 01, 2018 9:31 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Re: Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 10:32 pm

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?

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Socket not working/connecting on another computer

Post by bogs » Tue May 01, 2018 10:40 pm

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.
Image

agraham147
Posts: 50
Joined: Thu Apr 19, 2018 6:18 am

Re: Socket not working/connecting on another computer

Post by agraham147 » Tue May 01, 2018 11:05 pm

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)?

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: Socket not working/connecting on another computer

Post by zaxos » Wed May 02, 2018 12:10 pm

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
Attachments
socketSearch.zip
(1.88 KiB) Downloaded 196 times
Knowledge is meant to be shared.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”