Skip to content

[Host] Suggest adding a function to obtain the local IP address #28

Discussion options

You must be logged in to vote

lol. The error is because you get Host from not inited server.

  • You must connect your server first
Well. When a Udp instance is created the Host is null because it has no past connection. So when you try to use udp.Host the value will be null. To solve this, your UDP must have already been connected at least once, in your example it is the same:
private string GetLocalIpv4()
  {
    string ipv4 = "";
 
      using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
      {
        socket.Connect("8.8.8.8", 65530); // if you remove this line, will have null ref exception
        ipv4 = (socket.LocalEndPoint as IPEndPoint).Address.ToString();
      }

      return

Replies: 5 comments 13 replies

Comment options

You must be logged in to vote
8 replies
@1101728133
Comment options

@1101728133
Comment options

@1101728133
Comment options

@alec1o
Comment options

Answer selected by alec1o
Comment options

You must be logged in to vote
4 replies
@alec1o
Comment options

@1101728133
Comment options

@alec1o
Comment options

@1101728133
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@1101728133
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
2 participants