473,466 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Understanding tcpListener

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()
May 15 '06 #1
5 4002

Jerry Spence1 wrote:
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....

--
Tom Shelton [MVP]

May 16 '06 #2
> 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
May 16 '06 #3

"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:44***********************@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
May 20 '06 #4

Jerry Spence1 wrote:
"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:44***********************@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

May 21 '06 #5

"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...

Jerry Spence1 wrote:
"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:44***********************@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


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
May 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: zZ | last post by:
Hi All, I'm building a TCPListener as the server to accept TCPClient connections, using TCPListener.AcceptTcpClient. I'd like to retrieve client's IP after connected, but found no way to do...
2
by: Keith Langer | last post by:
I have a TCPListener which needs to listen on my LAN IP as well as the loopback address. In VS.Net 2002 the TCPListener constructor could accept the port as an argument and it automatically bound...
1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
4
by: Rob White | last post by:
OK, so I have a TcpListener that is waiting for sockets, this piece of code: IPAddress localAddress = Dns.GetHostByName(Dns.GetHostName()).AddressList; IPEndPoint localEP = new...
2
by: Dave Coate | last post by:
Hi, I am working on a client-server app. I can get two applications to talk to each other on the same machine using 127.0.0.1, but as soon as I try it using a computer name or actual IP address...
3
by: Bjørn Eliasen | last post by:
Hi, I have an application running on all pc's in our company. Basically it is a TCPListener awaiting for sockets to connect and on connection performs the required tasks. The app works fine, but...
4
by: Thos | last post by:
I have a TcpListener that gets started from a GUI button event handler: TcpListener listener; listener = new TcpListener(IPAddress.Any, 1234); listener.Start(backLog); As soon as the third...
1
by: Darwin | last post by:
Setting a server to listen on 8080 for incoming connections. Written in VS2005 on a Multi-homed machine. I get the warning: Warning 1 'System.Net.Sockets.TcpListener.TcpListener(int)' is obsolete:...
1
by: secutos | last post by:
When a TcpListener accepts a connection, it returns a TcpClient, which allows you to recieve data. But what about TcpListener.Server.Receive? Does that also allow to receive data in the same way? Or...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.