0
    {
        ipAddress = IPAddress.Parse("127.0.0.1");
        remoteEndPoint = new IPEndPoint(ipAddress, portAddress);

        socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(remoteEndPoint);
        OnConnected();
    }

The Exception error is thrown on the line "socket.Connect(...);

SocketException: The requested address is not valid in its context." NetworkManager.StartClient () (at Assets/Scripts/Networking/NetworkManager.cs:51)

(Its a unity project, the line is socket.Connect) Is there something I'm missing? I'm sure I've had this working before in different projects. I tried binding the socket first, but I was getting the exact same error and error code.

  • 1
    "The Exception error" - what error, precisely? Please include the *full* exception details (message and stack trace) in your question. – Jon Skeet Mar 11 '22 at 16:08
  • SocketException: The requested address is not valid in its context." NetworkManager.StartClient () (at Assets/Scripts/Networking/NetworkManager.cs:51) (Its a unity project, the line is socket.Connect i mentioned in the main post) – Brian Clason Mar 11 '22 at 16:20
  • Please edit that into the question, rather than it being in a comment. – Jon Skeet Mar 11 '22 at 16:21
  • You have to use a IP from your Network Interface. – Rui Caramalho Mar 11 '22 at 16:25
  • The server is running locally. Shouldn't local host work if the server is set to listen to any ip address? Using the computers local ip also didn't work. – Brian Clason Mar 11 '22 at 16:42
  • check https://stackoverflow.com/questions/30889972/the-requested-address-is-not-valid-in-its-context-when-i-try-to-listen-a-port – Rui Caramalho Mar 11 '22 at 17:02
  • Per [Windows Sockets Error Codes](https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2): "*WSAEADDRNOTAVAIL 10049 Cannot assign requested address. The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer. **This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer** (for example, address or port 0).*" What is the value of `portAddress` in this example? – Remy Lebeau Mar 11 '22 at 17:45
  • I just used 25565, minecrafts' port since I know there isn't anything eles running here... I also dont understand because I have another project ive worked on, with this exact code, and it worked then – Brian Clason Mar 11 '22 at 17:55

0 Answers0