how to listen a connection from a specific IP address?

F

felix.citycs

Why I cannot do with this code and exception is thrown with "the
requested address is not valid in its context"

try
{
IPAddress inputDNS_IP = Dns.Resolve(inputDNS_IP).AddressList[0];

// start the data port and listen for connection from input host
this.dataListener = new TcpListener(inputDNS_IP, dataPort);

this.dataListener.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


inputDNS_IP is an intranet IP here.
 
F

felix.citycs

I also found that it is okay to listen to self IP, but not other IP
inside the same network (LAN).

However, I would like to listen to a specific IP only. Is there any
other way to achieve this?

Thanks a lot~
 
H

harifajri

I also found that it is okay to listen to self IP, but not other IP
inside the same network (LAN).

However, I would like to listen to a specific IP only. Is there any
other way to achieve this?

Thanks a lot~

Hi,

hope this help..
you must use dot net framework 2.0 (minimum)
using System.Net;
using System.Net.Sockets;
....

IPAddress[] LocalIPaddress =
Dns.GetHostAddresses(Dns.GetHostName());
TcpListener sockServer = new
TcpListener(LocalIPaddress[0], Port);
 
F

felix.citycs

Hi,

hope this help..
you must use dot net framework 2.0 (minimum)
using System.Net;
using System.Net.Sockets;
...

IPAddress[] LocalIPaddress =
Dns.GetHostAddresses(Dns.GetHostName());
TcpListener sockServer = new
TcpListener(LocalIPaddress[0], Port);

Hi, thanks very much.

Actually, I would like to know the overloading constructor of
TCPListener, TCPListener(IPAddress localAddr, int port), support
listening to a given input IP address rather than localhost?
From your given code, Dns.GetHostName() will return my PC's host name,
and lookup its IPaddress.

However, what I want to do is to listen the connection from others.
e.g. 192.168.1.11 (where my PC maybe 192.168.1.4)
I have tried to initialize TCPListener object by "new
TCPListener(IPAddress.Parse("192.168.1.11"), 10000)", but there is a
throw of exception "
requested address is not valid in its context". I have tired with LAN
IP, or even Internet address, but all those throw exception.

Thanks.
 
F

felix.citycs

I am sorry. Maybe I am still not having a clear concept on what
TCPListener is doing.

May I know if it is the same for

1. new TCPListener(IPAddress.Any, Port);
2. IPAddress[] LocalIPaddress =
Dns.GetHostAddresses(Dns.GetHostName());
TcpListener sockServer = new TcpListener(LocalIPaddress[0],
Port);

I am expecting the first one, IPAddress.Any is accepting connection
from any IP. The second one is accepting IPAddress from itself
(localhost) only.

May I know if my concept is correct?

Sorry for that as I am new to network programming.
 
F

felix.citycs

I think I have found out what misunderstand of TCPListener that I have
made.

So sorry about my troublesome. Thanks a lot
 
P

Peter Duniho

I also found that it is okay to listen to self IP, but not other IP
inside the same network (LAN).

However, I would like to listen to a specific IP only. Is there any
other way to achieve this?

From your most recent post, it seems you have already figured out your
error. However, you failed to actually *document* that error in this
thread, something that is useful and polite when you've introduced a
thread so that others who come later and look at the thread hoping to find
an answer to *their* question might be able to.

The answer:

You cannot listen for TCP connections on any IP address except one
assigned to the local computer. The IP address used for the listening
socket is the IP address connecting clients will use to connect, not the
IP address from which the listening socket will accept connections. Using
the "any" version of the IP address simply ensures that your listening
socket will be able to receive connection requests on all valid IP
addresses assigned to that computer.

To filter connections by IP address of the connecting client, you need to
check the remote IP address of each connection request and reject
connections that don't meet your accepting criteria. In Win32 Winsock,
you could use the WSAAccept() function to include a callback that could be
used to inspect the connection information before actually connecting the
socket, but it appears to me that in .NET you must first accept the
connection and then check the remote IP address information. If it's not
a connection you want to accept, you then would close it immediately.

Pete
 

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