How can a .net socket server listen to all the IPAddresses of serv

  • Thread starter Thread starter Lokanath Chhatria
  • Start date Start date
L

Lokanath Chhatria

We have a socket server program and a socket client program.

The server, where the socket server program runs, has two network adapters
and hence two IP Addresses.

If the server program listens to an IP Address Endpoint which client can't
resolve, the client doesn't work.

The server program's code is like:

// Establish the local endpoint for the socket.

IPAddress hostaddress = IPAddress.Parse(socketInfo.SourceIP);

IPEndPoint localEndPoint = new IPEndPoint(hostaddress, socketInfo.SourcePort);

// Create a TCP/IP socket.
listenerSocket = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.Tcp);

listenerSocket.Bind(localEndPoint);

i.e it listens to one of the IP Addresses.

To solve the problem, I changed

IPEndPoint localEndPoint = new IPEndPoint(hostaddress, socketInfo.SourcePort);
to
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any,
socketInfo.SourcePort);

As "IPAddress.Any" would listen to both the IP Addresses for the given port.

But this did not work.

Can anyone tell me how to make a socket server listen to all the IP Address
of the server
 
Peter Duniho said:
[...]
IPEndPoint localEndPoint = new IPEndPoint(hostaddress,
socketInfo.SourcePort);
to
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any,
socketInfo.SourcePort);

As "IPAddress.Any" would listen to both the IP Addresses for the given
port.

But this did not work.

It should. If the rest of the code is otherwise correct and it doesn't
work with IPAddress.Any, you have a configuration problem.



Thanks for the reply..The rest of the code is fine and error free I beleive,
How should I detect a configuration problem?
The socket server program is running on Windows Server 2003 with 2 network
adapters, the ip address information for the two adapters is

1st network adapter:
IP Address ------- 10.0.30.6
Subnet mask ------255.0.0.0
Default Gateway---10.0.30.2

2nd network adapter:
IP Address -------- 192.168.112.142
Subnet mask ----- 255.255.255.0
Default Gateway ---192.168.112.2

The socket client program is running on Windows XP machine with the IP
Address:

IP Address ------ 10.0.30.10
Subnet mast ----255.0.0.0
Default Gateway--10.0.30.2

The client program gets to connect to the server when the server listens to
10.0.30.6, but if server listens to 192.168.112.142, the client doesn;t
connect.

Even when I have put IPAddress.Any, it doesn't seem to be working for
192.168.112.142.

Kindly help.
 
Peter Duniho said:
[...]
How should I detect a configuration problem?

It seems you have already _detected_ the problem. The question is how to
fix it.
The socket server program is running on Windows Server 2003 with 2
network
adapters, the ip address information for the two adapters is

1st network adapter:
IP Address ------- 10.0.30.6 [...]

2nd network adapter:
IP Address -------- 192.168.112.142 [...]

The socket client program is running on Windows XP machine with the IP
Address:

IP Address ------ 10.0.30.10 [...]

The client program gets to connect to the server when the server listens
to
10.0.30.6, but if server listens to 192.168.112.142, the client doesn;t
connect.

Even when I have put IPAddress.Any, it doesn't seem to be working for
192.168.112.142.

Your question is vague. Are you saying that you are trying to get a
client that has as its only IP address "10.0.30.10" to connect to your
server using IP address "192.168.112.142"?

If so, then there's probably no configuration problem, just wrong
expectations on your part.

The client on "10.0.30.10" is not in the same sub-net as the network
adapter using "192.168.112.142". It's just not going to be possible for
that client to access the server on that IP address.

My guess is that everything is working just fine. If you want to test the
server on the address "192.168.112.142", you will need to use a client
that's on the same subnet (i.e. has an IP address of "192.168.112.???"
where "???" is some actual number), or you need to configure the router
for that network segment to forward traffic to the server's
"192.168.112.142" address (and even then, whether you can get at the
router from your client using "10.0.30.10" is going to be configuration
dependent).

Pete


Thanks Pete,
I changed the subnet masks, so that it is same for both the IP Addresses.
It is working fine now :)
 
Back
Top