|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Understanding tcpListenermy 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() Jerry Spence1 wrote:
> I have the following example of a tcpListener. I have a device external to Change this...> 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 to:> Dim address As IPAddress = Dns.GetHostByName (Dns.GetHostName ()).AddressList (0) That will let you listen on the network.... -- Tom Shelton [MVP]
Show quote
Hide quote
> Change this... That's wonderful. Thanks Tom.>> 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.... > > -- > Tom Shelton [MVP] > -Jerry
Show quote
Hide quote
"Jerry Spence1" <jerry.spe***@somewhere.com> wrote in message Actually I've found a flaw in this. When I plug in my PDA I get another news:44697dba$0$18215$ed2619ec@ptn-nntp-reader01.plus.net... >> 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.... >> >> -- >> Tom Shelton [MVP] >> > > That's wonderful. Thanks Tom. > > -Jerry 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 Jerry Spence1 wrote:
Show quoteHide quote > "Jerry Spence1" <jerry.spe***@somewhere.com> wrote in message Jerry, what you have here is a multi-homed client. In other words, you> news:44697dba$0$18215$ed2619ec@ptn-nntp-reader01.plus.net... > >> 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.... > >> > >> -- > >> Tom Shelton [MVP] > >> > > > > 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? 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 -- Tom Shelton
Show quote
Hide quote
"Tom Shelton" <t**@mtogden.com> wrote in message Thanks Tom. I will present the IP addresses to the user and get them to news:1148184632.504838.130430@j73g2000cwa.googlegroups.com... > > Jerry Spence1 wrote: >> "Jerry Spence1" <jerry.spe***@somewhere.com> wrote in message >> news:44697dba$0$18215$ed2619ec@ptn-nntp-reader01.plus.net... >> >> 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.... >> >> >> >> -- >> >> Tom Shelton [MVP] >> >> >> > >> > 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, 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 > > -- > Tom Shelton > select which one is right and then save it, as you say, in a config file, or registry. -Jerry |
|||||||||||||||||||||||