Author Topic: Detect if port is already used  (Read 6530 times)

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Detect if port is already used
« on: July 17, 2016, 05:05:41 PM »
Is there a built in way to test if another application is using the port set (UDP and/or TCP) when starting a server?

if (TNServerInstance.isActive) of course will not work if the port is being used by another application...


Code I'm using:
  1. using (TcpClient tcpClient = new TcpClient())
  2. {
  3.         try
  4.         {
  5.                 tcpClient.Connect(Tools.localAddress, serverTcpPort);
  6.                 Debug.Log("Port open");
  7.         }
  8.         catch
  9.         {
  10.                 Debug.Log("Port closed");
  11.         }
  12. }
  13.  

[TNet] Udp.Start: Only one usage of each socket address (protocol/network address/port) is normally permitted.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Detect if port is already used
« Reply #1 on: July 17, 2016, 07:39:38 PM »
Ended up using the following code to detect:  network, internet and port

I'll try/catch when executing the server start function

Any optimization or feedback welcome...



  1. //Note: For LAN use
  2.  
  3.  
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using TNet;
  8. using UnityEngine;
  9.  
  10. //http://www.c-sharpcorner.com/uploadfile/nipuntomar/check-internet-connection/
  11. public class InternetCS
  12. {
  13.     //Creating the extern function...
  14.     [DllImport("wininet.dll")]
  15.     private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
  16.     //Creating a function that uses the API function...
  17.     public static bool IsConnectedToInternet()
  18.     {
  19.         int Desc;
  20.         return InternetGetConnectedState(out Desc, 0);
  21.     }
  22. }
  23.  
  24.  
  25.  
  26.  
  27. public bool isNetworkAvailable
  28. {
  29.         get
  30.         {
  31.                 return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
  32.         }
  33. }
  34.  
  35. public bool isInternetAvailable
  36. {
  37.         get
  38.         {
  39.                 return InternetCS.IsConnectedToInternet();
  40.         }
  41. }
  42.  
  43. public bool isPortAvailable
  44. {
  45.         get
  46.         {
  47.                 ServerList.Entry[] list = TNLobbyClient.knownServers.list.ToArray();
  48.                
  49.                 if (list.FirstOrDefault(i => i.internalAddress.Port == serverTcpPort) == null) return true;
  50.                 else return false;
  51.         }
  52. }
  53.  
  54.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect if port is already used
« Reply #2 on: July 20, 2016, 03:35:08 AM »
wininet.dll, eh? Hope you aren't planning for this to work somewhere other than Windows ;)

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Detect if port is already used
« Reply #3 on: July 20, 2016, 06:39:30 AM »
It was that or a Ping...  but, yeah - windows only.