I'm writing an application that has 10 (as an Example) threads running. each thread creates a connection using HttpURLConnection to external host. I need to use a different IP address for each connection in different threads. can I do it considering the fact that there are about 10 valid static IP addresses assigned to my server?
note that I have seen something like same question here and here but in first one though it has an answer checked as correct by asker, but I see too many comments telling that Its not working and in second one is talking about How to make the JVM use a given source IP by default? which does not help me I guess.
beside I don't want to risk and pay for the server and then find out I cant do such a thing!
Related
I'm working on a project, and basically I need this program to find a list of all connected IP addresses to a network, such as going to your network connections in Windows. I've looked all over the internet, and I cant find what I'm looking for and I hope someone can!
This program cannot just crunch numbers (such as pinging all possible IP addresses), it needs to be faster than that. Being able to just connect to the network and obtain a "list" of all IP's that it can see would be perfect. Using the docs.oracle.com webpage did what it was supposed to, but there were too many extra results (including Eth10, etc). Also, the only IP address it listed that wasnt my own didnt match any active computers, so I dont know what was up with that... (192.168.0.9 was listed, the only other active connection was at 192.168.0.10, maybe I'm missing something?).
What is a way to do this? Sorry if I'm unclear, I'm able to be clearer if there's something specific, hope this helps!
Thanks!
EDIT: I hope this helps; I want to do this in java, because for my designed program to work it needs a list of active IP addresses connectable to the active machine. Meaning that if I run it on a laptop at a school, I need all the IP addresses that I could connect to on the network. It is a network thing, but it does need to be done in java. The purpose is for a file manager, otherwise its pretty tough to explain. Does that help?
The only way to do what you are asking is via brute force. It can be done somewhat quickly by creating multiple threads that fire off TCP SYN requests to random IPs on the network (this is what NMap does). If you had access to the DHCP server then you could likely get a list from that point. I would warn you to tread very lightly. Running NMAP or doing port scans on a network is considered an attack by many network admins... you could get yourself in to trouble (they WILL notice).
I have been given a project in which I have to share files with peers without any intermediate server. I want to know how to know how many hosts are online on the network and how to connect to them. I have to roughly make Routing table for the hosts in my computer through Java.
You need not to keep any track of routing. All you need is to know the endpoints addresses.
You may find out if a host is connected to the network by establishing a connection with this host.
You mentioned that there will be no intermediate server, so the NAT hole punching is out of scope of your question.
As the starting point look at java.net.Socket class documentation.
I'm going to assume that this is on a private network. Something that's relatively contained. Letting them find each other over the internet sounds like a nightmare.
So, given this, one model for your peers to find each other might be to select a standardised sequence of ports and an alive signal. Then when a peer searches for others, it simply goes through all the IPs on the network and makes a request on that port number. If it gets an alive signal it adds it to a table. The signalled computer would have to record the new comer as well.
You'd have to select the port sequence such that they're generally unused on the network. (Quite large) I say sequence so that there's a preference in ordering to speed up the search.
To speed up the search even more, when an alive signal is sent, it could also send it's peers table and the IPs it checked (and the ones it received in the same way). Then the new peer would only have to check new IPs on the network since old IPs with a new instance of the program would establish themselves.
Hope that helps
i'd suggest you not to go with mac address,it will be better if you turn your dhcp off of your router (if you can include one of course........but if you only can include an hub you'll be forced i guess to use mac address).and use manual addressing for your ipv4 address,and you will be able to easily connect to the other computers.
I want to find all systems that are on the same local network i.e. that have the same subnet mask.
I think I must work with InetAddress Class in Java but I don't know what should I do.
My algorithm is:
Find all available system in a local network
Check if they run on a my requested port
Keep in mind the IP of system which runs on this port
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
System.out.println(networkInterface);
networkInterface.getByInetAddress(localHost);
for (){
networkInterface.getByInetAddress(localHost)
}
I think that the networkInterface.getByInetAddress(localHost) can give me all available IP addresses in a subnet, but I don't how I can do this for all available systems, and what is the condition for the for clause.
Finding all of the possible IP addresses in an IP subnet is simple. Just write a loop that enumerates all possible byte[] representations in the subnet and constructs InetAddress instances for them.
That's the easy bit, the hard bit is solving these two problems:
how do you find all if the live IP addresses on a subnet, and
for a given IP address, how do you figure out if it is using a particular port.
The first problem can be solved in two ways:
You could use the InetAddress.isAlive() method to see if the host responds to a ping. The problem is that some hosts may be configured to not respond to pings, or the pings may be blocked by an internal firewall. This also doesn't work if the subnet is large. The problem is that you need to ping a large number of hosts, and that will take a long time and generate a lot of network traffic ... especially if there are multiple application instances doing this.
The smarter alternative is to examine your local machine's ARP cache, and extract all IP addresses that it knows about. The problem is that 1) your application may not have the access rights to access the ARP cache, 2) some IP addresses may not be in the cache, and 3) there is no way to access the ARP cache in pure Java.
The second problem depends on the nature of the service on the port:
If it is a TCP-based service, then you can attempt to connect to an IP/port using a plain socket. If the connection is accepted, then the port is in use by something. It may or may not be the service you expect, but in general the only way to tell that is to try to use the service.
If it is a UDP-based service, then there's now way in general to know if something is using the port.
I'm going to hope that you mean the following:
"I wish to discover all systems on the same local network as (the computer running my program)'s primary IP address which are listening to a certain TCP port."
With UDP, this is a slightly harder problem - you could look for bounces, but your packet may be eaten or lost (as could the reply).
Now, assuming TCP, you have three steps.
Get the IP address of the local interface you believe is the "primary" one.
Enumerate hosts on the same subnet as this address.
For each host, determine if the appropriate port is listening.
For step 1, use InetAddress.getLocalHost().
For step 2, get the subnet portion of the retrieved address, and then try every possible host within that subnet. Let's hope it's a Class C and there are only 254 hosts. There is no 100% sure way to get all the hosts without trying each one. A non-100%-certain (and more complex) way is to use ARP tables to get a router's view of the network, to send a broadcast ping to the local net, or to fall down to layer 2 and send a broadcast Ethernet packet.
For step 3, open a connection to each host on the appropriate port and then close it. If the connection times out or is denied, scratch the host. Otherwise, add it to the "listening" list. You probably want to try more than one connection at a time and set the timeout interval to something small, since most people won't be listening.
Step 4: congratulations! You've just made a very feature-poor version of nmap in Java.
I have been struggling with this for the entire day now, I hope somebody can help me with this.
My problem is fairly simple: I wish to transfer data (mostly simple commands) from one PC to another over the internet.
I have been able to achieve this using sockets in Java when both computers are connected to my home router. I then connected both computers to the internet using two different mobile phones and attempted to transmit the data again. I used the mobile phones as this provides a direct route to the internet and if I use my router I have to set up port forwarding, at least, that is how I understand it.
I think the problem lies in the method that I set up the client socket. I used:
Socket kkSocket = new Socket(ipAddress, 3333);
where ipAddress is the IP address of the computer running the server. I got the IP address by right-clicking on the connection, status, support. Is that the correct IP address to use or where can I obtain the address of the server? Also, is it possible to get a fixed name for my computer that I can use instead of entering the IP address, as this changes every time I connect to the internet using my mobile phone?
Alternatively, are there better methods to solving my problem such as using http, and if so, where can I find more information about this?
EDIT:
Would it be possible to have the server program running on a server on the internet somewhere. My original server would then be a client that send information to this server. This server would then pass this information to my original client and vice versa. That way, the IP address of my computer won't matter, as I only need to know the address of the server hosted somewhere on the web. Does this seem like a better solution? Where do I begin implementing such a solution?
Thanks!
When you connected to the server that serves StackOverflow, did you type in the IP address? It's 64.34.119.12, if that jogs your memory.
You probably didn't. You probably typed "stackoverflow.com". There's a huge, complex, clever, and in some ways, poorly implemented system called DNS that translates sensible and human-readable names into IP addresses.
One problem with DNS, though, is you need a "static IP", which is exactly what it sounds like: an IP address that doesn't change, which is exactly what you don't have.
So, what can you do?
You can buy a static IP account from your ISP (pretty expensive)
You can use some proxy out in the Internet (a machine that does have a static IP and is willing to bounce your packets back and forth -- I'm not aware of any service that does this for you; you could write one and put it up on Amazon Web Services or Google App Engine, both of which would be free at your level of usage, but they'd be slow, since every packet trying to cross your living room would have have to go via some data-center in Virginia).
You can keep doing what you're doing, looking in the net-configuration of your machine.
You could speed (3) up a little by having your server program look up its own IP address and print it out where you could see it and type it into the server by hand.
You can use DynDNS, as Sergey mentioned (this is the "right" solution, in that it's very general, it just might be a little complicated to set up)
You can use multi-casting.
Multi-casting is an interesting solution, and it may work for you. The idea is, when your server starts up, it announces to the net, "Here I am, I'm providing X server, here's my IP address, talk to me." The problem is, a multi-cast won't leave your living room. Obviously, if every multi-cast were spread to every computer on the Internet, the whole thing would collapse, so your router will ignore, and not route, multi-cast packets. That may or may not be a deal-breaker for you. EDIT Re-reading your question, I see it is a deal-breaker for you. I'd go with #5, but be aware there may be routing issues (address translations that prevent a server from knowing the address that other computers can find it at) or fire-wall issues (that is, your ISP may prevent your server from receiving incoming packets even if the address is correct).
using a direct socket connection with a port like 3333 is usually complicated because different network configurations.
firewalls will make a pleasure preventing the connection, or killing it from time to time.
maintaining a 2-way connection can be a nighmare. the SIP protocol is struggling with this kind of problems.
For a simple application, i suggest you look into the comet technology, where your clients can establish an http connection with a shared server. The server can then bridge commands between them.
html5 will also bring the websocket protocol to the table.
I got the IP address by right-clicking
on the connection, status, support.
Not sure about the "support" part, and I'm not on a Windows machine right now, but I think that the most easy and reliable way to figure out the IP address on Windows is to run "ipconfig" from the command line (Win+R, type "cmd", then "ipconfig" in the opened window). This, of course, should be done on the server side.
However, the problem is that depending on the ISP your IP address may be not within the Internet, but within a local ISP network (so-called NAT). In this case, you'll need to use some sort of black magic called TCP hole punching, which is very complicated and not guaranteed to work. You can figure out if your address is local or not by looking at it. For IPv4 local addresses are almost always like 10.x.x.x or 172.16-31.x.x, or 192.168.x.x. Don't know about IPv6.
You can also check your IP by visiting one of the special sites like www.whatismyip.com. If the address they tell you is different from the one you see by running "ipconfig" or looking at the connection properties, then you're almost certainly behind a NAT (or your ISP is using a transparent proxy, but that's rare).
If you are directly connected to Internet (no local addresses and NAT), then you should also check if you have any firewall software and either to configure it to allow connections to the port you use, or make sure it's in "ask the user" (and not "silently reject") mode, or just disable it completely (this may put your computer at risk, especially if there is no anti-virus software or the system isn't up-to-date).
Also, is it possible to get a fixed
name for my computer that I can use
instead of entering the IP address, as
this changes every time I connect to
the internet using my mobile phone?
Yes, it's possible. There is the thing called DynDNS, and there are DynDNS providers like DynDNS.com, where you can get a third-level domain name for free (like mycoolpc.dyndns.org). You'll have to install and configure some DynDNS client on your PC that will tell the DynDNS server its new IP each time each changed. I don't know anything about particular clients to use because I'm using the one built-in in my home router.
No need to write networking code for this, unless it really floats your boat. Take a look at SCP. http://amath.colorado.edu/computing/software/man/scp.html. There is a windows implementation where you can download putty (windows ssh client), and it is on most linux distributions. Alternatively, you could set up an FTP or SSH server on one or both of the machines.
"a fixed name for my computer that I can use instead of entering the IP address" would be a domain name, these are purchasable online for a few bucks.
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).
Yes, you can create a socket using the ip address, you can do so like this: new Socket("192.168.1.00",8888)
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.
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.
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.
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.
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.