Socarsky
Well-known member
"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
instead of theese code lines:
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
VB.NET:
Dim ipendofpoint As IPEndPoint = New IPEndPoint("192.168.1.171", 8000)
Dim tcpListener As New TcpListener(ipendofpoint)
VB.NET:
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