1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

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

Discussion in 'Programming' started by the_dude_abides, May 17, 2019 at 7:00 PM.

Tags:
  1. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    491
    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: May 18, 2019 at 12:46 AM
  2. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,491
    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_dude_abides likes this.
  3. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    491
    @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.
     
  4. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,491
    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.
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    491
    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.
     
  6. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,491
    Ok. But remember the GM sandbox file system. So you can make sure the exe get the correct path to the saved file.
     
    the_dude_abides likes this.
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    491
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice