Legacy GM Doing network functions: can you find if a computers port is free?

Hi.

I am looking into multi-threading. Having set up a project handling networking, I want to integrate it with "execute shell" (a marketplace asset which allows loading up a second instance of a project) The two versions will communicate with each other, and then I'll see how it can be handled after getting to that stage.

In the networking example that I followed, it had two versions on the same pc communicating. So I know that that works. However: it used manually finding my own IP address, and setting a port. I know that the loopback Internet protocol means I don't need an IP, but is finding a free port necessary?

If so: is there a way to know which ports are available?

Any help with this is appreciated. Thanks!
 
Last edited:

The-any-Key

Member
A simple way is just to start a socket and start listen. Ex:
Code:
port_is_free=network_create_socket_ext(protocol, port);
if port_is_free>-1
{
   // Port is free
   network_destroy(port_is_free);
}
else
{
   // Port is not free
}
As the other app need to know what port to connect to. You need some way to send this info to the other app. You can also try using a range of ports and the other app try connect from port 6510-6610. You can also use the LAN broadcast on app1 and let app2 scan a range of ports to find the port that app1 is using.
 
@The-any-Key
Thanks for answering that. I think I can see how to find a free port (loop through the numbers until one is available), though am not quite sure how to get that info between the "server" and the "client", as the "client" was looking for a fixed port. Will have to think on that.

In the LAN demo that YOYO has, I believe the port was number 8000 and it has no means of discovering any other. Do you know if that has any significance? Like: it's a port that is generally not used, or is specific to the loopback Internet protocol IP?

I can look into finding a free one, and then a way to have to make the two meet up, but am wondering if it's even necessary under those conditions.
 

The-any-Key

Member
You can make the "server" broadcast some info on the port you use and make the client loop some ports until it find the broadcast.

In general you should expect all ports to be used as many programs use ports. And you don't know what the end user might have. So the key is to make it as dynamic as possible.
 
You can make the "server" broadcast some info on the port you use and make the client loop some ports until it find the broadcast.

In general you should expect all ports to be used as many programs use ports. And you don't know what the end user might have. So the key is to make it as dynamic as possible.
I haven't as yet tested this, but it would probably work. I will include a file in the exe that is the "client". Before the "server" loads up the client it will test for a free port, and then write that detail into the included file. So when the client loads, and gets the info from the file, it should have the currently used port before it connects to the server.
 
That was how I originally planned to do this - through each version writing the others "commands", by passing it through in an ini file it was reading. But I either couldn't figure out which directory the ini file would be in, or it had some other problem, and it didn't work. And also that local networking might be better for simplicity.

It will presumably be the same problem with using an ini this time, but once I have that figured out it should be good to go. Thanks for all the help with this!
 
Top