J
Jerry Spence1
I have the following example of a tcpListener. I have a device external to
my PC which is sending a TCP request on port 10002. This is working OK as I
have a network sniffer on my PC and can see the data. This code hangs whilst
waiting for a connection on the Dim tcpCli As TcpClient =
tcpList.AcceptTcpClient() line and doesn't hear the incoming request.
I am wondering if this example code assumes that the client is on the same
PC as the server. I am expecting the incoming data to be on the Ethernet
port (192.168.0.8), and this seems to be listening on the local address
127.0.0.0. Am I right? How should I code it to listen to the Ethernet port?
-Jerry
Dim localhostAddress As System.Net.IPAddress = IPAddress.Loopback
Dim tcpList As New TcpListener(localhostAddress, 10002)
tcpList.Start()
listening = True
Do
' Wait for the next client to make a request.
Dim tcpCli As TcpClient = tcpList.AcceptTcpClient()
' Read data sent by the client (a CR-LF separated string in this case).
Dim ns As NetworkStream = tcpCli.GetStream
Dim sr As New StreamReader(ns)
Dim receivedData As String = sr.ReadLine()
' Process the data (just convert to upper case in this demo).
Dim resultData As String = receivedData.ToUpper
' Send it back to the client.
Dim sw As New StreamWriter(ns)
sw.WriteLine(resultData)
sw.Flush() ' This is VERY important.
' Release resources.
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
' Exit the loop if another thread set the listening variable to False.
Loop While listening
' Reject client requests from now on.
tcpList.Stop()
my PC which is sending a TCP request on port 10002. This is working OK as I
have a network sniffer on my PC and can see the data. This code hangs whilst
waiting for a connection on the Dim tcpCli As TcpClient =
tcpList.AcceptTcpClient() line and doesn't hear the incoming request.
I am wondering if this example code assumes that the client is on the same
PC as the server. I am expecting the incoming data to be on the Ethernet
port (192.168.0.8), and this seems to be listening on the local address
127.0.0.0. Am I right? How should I code it to listen to the Ethernet port?
-Jerry
Dim localhostAddress As System.Net.IPAddress = IPAddress.Loopback
Dim tcpList As New TcpListener(localhostAddress, 10002)
tcpList.Start()
listening = True
Do
' Wait for the next client to make a request.
Dim tcpCli As TcpClient = tcpList.AcceptTcpClient()
' Read data sent by the client (a CR-LF separated string in this case).
Dim ns As NetworkStream = tcpCli.GetStream
Dim sr As New StreamReader(ns)
Dim receivedData As String = sr.ReadLine()
' Process the data (just convert to upper case in this demo).
Dim resultData As String = receivedData.ToUpper
' Send it back to the client.
Dim sw As New StreamWriter(ns)
sw.WriteLine(resultData)
sw.Flush() ' This is VERY important.
' Release resources.
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
' Exit the loop if another thread set the listening variable to False.
Loop While listening
' Reject client requests from now on.
tcpList.Stop()