I am trying to workout an issues I am having with the following code. Each time I get to the TCPListener I get an error saying that the "SokeyException was unhandled - The requset address is nt valid in its context". I am trying to communication to another PC that is in my network. I selected port 5679 because it said it was a listnening port when I did the following command C:\> netstat -ao |find /i "listening" However I do not get past the tcpListener.Start() call. When I have the programs on the same PC everything works fine.
Public Class Form1
Const portNumber As Integer = 5679
Private tcpListener As New TcpListener(CType(Dns.Resolve("LMI111").AddressList(0), IPAddress), portNumber)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Listening must be on the same port that the client is connected on.
'"Localhost" string is used when the client and the listener are on the same computer.
'If the listener is listening at a computer that is different from the client, then provide the host name of the computer
'where the listener is listening.
'Comment the previous line and uncomment the following line if you are using Visual Basic .NET (2003).
'Dim tcpListener As New TcpListener(portNumber)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Console.WriteLine("TCP Server is up and waiting for Client connection...")
Me.Label1.Text = "TCP Server is up and waiting for Client connection..."
''Accept the pending client connection and return a TcpClient for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
' Console.WriteLine("Connection accepted.")
' Get the data stream.
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the data 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 clientdata As String = Encoding.ASCII.GetString(bytes)
Me.Label1.Text = "Client sent: " + clientdata
Dim responseString As String = "Successfully connected to TCP server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'Console.WriteLine(("Message Sent by TCP Server /> : " + responseString))
'Close TcpListener and TcpClient.