Question The requested address is not valid in its context

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
"The requested address is not valid in its context" come up my way not to get a good result to achieve my project done so how I can handle that?
The program purposes to run on a couple difference computers that one of them is a host the other one is a client that they run in a same network to communicate each other.

Note: The code does not have normally any error until trying to add below code
Code:
Dim ipendofpoint As IPEndPoint = New IPEndPoint("192.168.1.171", 8000)
        Dim tcpListener As New TcpListener(ipendofpoint)
instead of theese code lines:
Code:
  Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
    Dim tcpListener As New TcpListener(localAddr, portNumber)

Imports System.Net.Sockets
Imports System.Text
Imports System.Net

Public Class Form1

    Dim strPass As String = String.Empty

    Private Sub client_Send()
        Dim tcpClient As New System.Net.Sockets.TcpClient
        'Dim ipenpoint As IPEndPoint = New IPEndPoint("127.0.0.1", 8000)
        tcpClient.Connect("127.0.0.1", 8000)
        Dim networkStream As NetworkStream = tcpClient.GetStream
        If networkStream.CanWrite And networkStream.CanRead Then
            ' do a simple write
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("[Client] is anybody there")
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' read the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' output the date received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            ListBox1.Invoke(Function() ListBox1.Items.Add("[Client] host returned: " + returndata))
        Else
            If Not networkStream.CanRead Then
                ListBox1.Invoke(Function() ListBox1.Items.Add("Client] cannot not read data to this stream"))
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    ListBox1.Invoke(Function() ListBox1.Items.Add("Client] cannot not write data to this stream"))
                    tcpClient.Close()
                End If
            End If
        End If
        ' pause so user can view the console output
    End Sub

    Private Sub hosting_port()
        ' must listen on correct port - must be same as port client wants to connect on.
        Const portNumber As Integer = 8000
        Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

        ' alternative way to try my aim
        'Dim ipendofpoint As IPEndPoint = New IPEndPoint("192.168.1.171", 8000)
        'Dim tcpListener As New TcpListener(ipendofpoint)

        Dim tcpListener As New TcpListener(localAddr, portNumber)
        tcpListener.Start()
        strPass = "[SERVER] Waiting for connection..."
        AddTextToListBox()
        Try
            ' Accept the pending client connection and return
            ' a TcpClient initialized for communication.
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            ListBox1.Invoke(Function() ListBox1.Items.Add("[SERVER] Connection accepted."))
            ' get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            ' read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' return the data received from the client to the console.
            Dim responseString As String = "Connected to server."
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ListBox1.Invoke(Function() ListBox1.Items.Add("[SERVER] Message sent /> : " + responseString))
            ' any cummunication with the remote client using the TcpClient can go here.
            ' close TcpListener and TcpClient.
            tcpClient.Close()
            tcpListener.Stop()
            ListBox1.Invoke(Function() ListBox1.Items.Add("[SERVER exit"))
        Catch ex As Exception
            ListBox1.Items.Add(ex.Message)
        End Try
    End Sub

    Private Sub multi_client()
        Dim thread As System.Threading.Thread
        thread = New System.Threading.Thread(AddressOf client_Send)
        thread.Start()
    End Sub

    Private Sub multi_server()
        Dim thread As System.Threading.Thread
        thread = New System.Threading.Thread(AddressOf hosting_port)
        thread.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        multi_server()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        multi_client()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = True
    End Sub


    Private Sub AddTextToListBox()
        Dim text As String = strPass
        If ListBox1.InvokeRequired Then
            ListBox1.Invoke(New MethodInvoker(AddressOf AddTextToListBox), New Object() {Text})
            Exit Sub
        End If
        ListBox1.Items.Add(text)
    End Sub
End Class

 

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
It appears that is not a valid local ip, try IPAddress.Any.
Host side works but client side gets thrown. I used in IPAddress.Loopback in client_Send()
Code:
 Dim tcpClient As New System.Net.Sockets.TcpClient
Dim ipenpoint As IPEndPoint = New IPEndPoint(IPAddress.Loopback, 8000)
tcpClient.Connect(ipenpoint)
If I use IPAddress.Any then The requested address is not valid in its context, thrown
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,410
Location
Norway
Programming Experience
10+
In first post you asked about TcpListener, where you must specify the local ip and port to listen to, that is what I replied to. Any means that it will listen on all valid interfaces that is available on system.

In your last post you talk about TcpClient, where Connect method expects the remote ip and port to connect to. IPAddress.Any is not valid for TcpClient.Connect since Any means all interfaces and the client can only connect to one endpoint at a time.

And by the way, the specific ip interface for the local computer on network (for example 192.168.1.171) is not interchangable with IPAddress.Loopback, those are different interfaces.
 
Top Bottom