views:

90

answers:

2

I know that the Socket class can be instantiated in this way (as an example):

new Socket("taranis", 7);

where "taranis" is the name of the server in a local network. In this respect I have two questions:


1. If my computers do not form a local network but are connected to the Internet, can I use the IP addresses of computers to instantiate a socket?


2. How my computers get names in the local network? Do I give names to the computers when I create the network.

P.S. I herd that computers can establish a network by themself (using zeroconf). Who then gives names to the computers and how can I know these names in advance (I need them to write my code).

+2  A: 

Before socket programming, you need some background on networking. Unluckily the questions you ask are not simple to answer as it depends on the specific network configuration you have. Here are some short answers, but may be incorrect due to the dependency on the particular configuration. You will do better to read up on TCP/IP for example here.

  1. Yes. But I doubt that that your computers do not form a local network (LAN). In case they do, you can use their LAN IP address. To find the ipaddress, you can use "ipconfig" command on the "Command Prompt" on Windows, and "ifconfig" on unix. The output is the configuration of each network interface in the computer.

  2. Yes, you can configure the name of each computer on the computer.

For programming, usually you would use ipaddress (use name when the name can be dynamically assigned to one of the computers (using a Dynamic DNS)). IP Address may also be (and is quite commonly) dynamically assigned using a DHCP server.

Amit Kumar
Did I correctly understood? A computer can be in a local network and in Internet simultaneously. And than the computer can have two IP addresses? One is the LAN IP address and another one is from Internet?
Roman
+2  A: 
  1. Yes, you can create a socket using the ip address, you can do so like this: new Socket("192.168.1.00",8888)

  2. When you install an OS on your computer, usually one of the steps that the OS makes you go through is giving your machine a name. Each OS also has a way of changing these names after installation. So, everyone of your computers probably does have a name. However, the tricky part is getting one machine to know the name of the other. This can be done in a few ways. One is by using a DNS server. This kind of like a middle man which will translate the name (i.e Computer1) to its IP address (192.1.168.1.100). You can also keep these mapping locally, you can put this in the hosts file. This is a mapping between names and ip addresses, and if you use this method, you need to make sure that these hosts files line up across the machines.

  3. Zeroconf is an interesting protocol. The way it works is one computer creates a named service and the second computer just looks up the service by name, and once it finds the service it can connect to it. When the service is discovered, the connecting client and can query for the ip and port to connect to.

nstehr