Understanding tcpListener

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()
 
T

Tom Shelton

Jerry said:
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

Change this...
Dim localhostAddress As System.Net.IPAddress = IPAddress.Loopback

to:

Dim address As IPAddress = Dns.GetHostByName (Dns.GetHostName
()).AddressList (0)

That will let you listen on the network....
 
J

Jerry Spence1

Change this...
to:

Dim address As IPAddress = Dns.GetHostByName (Dns.GetHostName
()).AddressList (0)

That will let you listen on the network....

That's wonderful. Thanks Tom.

-Jerry
 
J

Jerry Spence1

Jerry Spence1 said:
That's wonderful. Thanks Tom.

-Jerry
Actually I've found a flaw in this. When I plug in my PDA I get another
network device which has an IP address. Your suggestion above picks up that
as my network IP Address rather than the Ethernet Card. How can I make sure
it picks up the right one?

-Jerry
 
T

Tom Shelton

Jerry said:
Actually I've found a flaw in this. When I plug in my PDA I get another
network device which has an IP address. Your suggestion above picks up that
as my network IP Address rather than the Ethernet Card. How can I make sure
it picks up the right one?

Jerry, what you have here is a multi-homed client. In other words, you
have multiple IP addresses. If you notice, Dns.GetHostByName will
return an AddressList - it can indeed have more then one IP.
Generally, you only have to worry about one :)

So - as I see it you have two choices. You can pass IPAddress.Any to
your TcpListener for it's address. This will tell it to listen on all
available interfaces. Or, you read the IPAddress from a config file.

Maybe someone will have a better suggestion, but I don't see any other
way around your problem at the moment.

HTH
 
J

Jerry Spence1

Tom Shelton said:
Jerry, what you have here is a multi-homed client. In other words, you
have multiple IP addresses. If you notice, Dns.GetHostByName will
return an AddressList - it can indeed have more then one IP.
Generally, you only have to worry about one :)

So - as I see it you have two choices. You can pass IPAddress.Any to
your TcpListener for it's address. This will tell it to listen on all
available interfaces. Or, you read the IPAddress from a config file.

Maybe someone will have a better suggestion, but I don't see any other
way around your problem at the moment.

HTH

Thanks Tom. I will present the IP addresses to the user and get them to
select which one is right and then save it, as you say, in a config file, or
registry.

-Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top