Baz
Baz

Reputation: 13125

Sockets and multiple IP Addresses

I have two internet cards on my machine configured with two separate ip addresses. Both ip addresses are part of the same network. Am I right in saying that when I create a socket that it is specific for one of these ip addresses?

UPDATE:

Here's my situation:

I have a hardware device located at 192.168.0.10. It communicates via udp to (192.168.0.11, 50000) and (192.168.0.12, 50000). The hardware device does this via two ports, as follows: (192.168.0.10, 49000) and (192.168.0.10, 49001).

I create the ports (192.168.0.11, 50000) and (192.168.0.12, 50000) via a script so that I can log and manipulate the communication, the data is then forwarded to two simulators which are capable of talking with the hardware.

So in effect, my script is "the man in the middle" where I log and corrupt data for the purpose of testing the hardware.

Here are the socket interfaces for my script:

IP 1 - Channel 1

External_socket = (192.168.0.10, 49000) ** Hardware
External_side_socket = (192.168.0.11, 50000) ** Script
Simulator_side_socket = (192.168.0.11, 50001) ** Script
Simulator_socket = (192.168.0.11, 50002) ** Simulator

IP2 - Channel 2

External_socket = (192.168.0.10, 49001) ** Hardware
External_side_socket = (192.168.0.12, 50000) ** Script
Simulator_side_socket = (192.168.0.12, 50001) ** Script
Simulator_socket = (192.168.0.12, 60002) ** Simulator

And this works as expected, and the correct data is logged for each channel.

However, it stops working if I make two simple changes:

1) I change the second simulator's configuration file to 50002 from 60002, while keeping the ip address at 192.168.0.12.

2) In my script I also change 60002 to 50002, for IP2.

Suddenly the data from the second channel is arriving at the first simulator. How can this be? .net has an IPAny and I'm wondering if the simulator is using this somehow and ignoring the ip address in its config file. Do you think that might be the issue here?

Thanks,

Barry

Upvotes: 3

Views: 3995

Answers (2)

Zac
Zac

Reputation: 4695

You can bind using the value INADDR_ANY, which means not binding to any particular network interface, so you can work ignoring the IP address your program is running on.

References here and here.

Upvotes: 1

iikkoo
iikkoo

Reputation: 2856

I would say that it is right-ish. Depending on the level of abstraction you want to use in your statement.

When you create a socket it isn't bound to an IP address. Looking at the Wikipedia article on the Berkeley socket you see that first one create the socket (socket()) stating domain, socket type, and family. Then the socket is bound (bind()) to an address, for instance 10.0.0.1. Then, depending on what you want to use the socket for you can set it to listen, accept, or connect.

Localhost has an IP address as any other device, that is 127.0.0.1. You create the socket and bind it to 127.0.0.1. Then that socket works as any other socket that you can communicate with.

You could also read the Wikipedia article on Network Socket.

Upvotes: 0

Related Questions