How to send request from virtual IP address to a server? - java

Can any one tell me how could i send request to a server using different IP address. Let me explain the requirement; I have to create n no of different virtual IP address in my machine, and then I have to sent request to a different server using those individual virtual IP address. Like one request from one virtual IP address , one from another virtual IP address. Can anyone tell me how could I achieve this programatically ? I am developing my code in java so if you have any code or concept on this please share with me.

It's done the same way in Java as any other language: you bind the socket to the address you want to connect from, before you connect.
Socket s = new Socket();
/*
* Change the 192.168.0.1 to whatever IP address you want the connection
* to come from. If your IP address is stored in an InetAddress object,
* you can use that too, instead of a string.
*/
SocketAddress from = new InetSocketAddress("192.168.0.1", 0);
SocketAddress to = new InetSocketAddress("192.168.0.2", 80);
s.bind(from);
s.connect(to);

Related

Bind to AnyLocalAddress (0.0.0.0) in Java on IPv6-only network

In Java, if I write
DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress("0.0.0.0",162));
Does the wildcard work for IPv6-only networks also?
P.S: I don't have an IPv6-only network to test this.
"0.0.0.0" basically means all IP addresses. To ensure that you bind to all known addresses why not do this.
DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress(162));
Where not specifying the IP address causes it to bind to all ip addresses available on that host.
See - https://docs.oracle.com/javase/8/docs/api/java/net/InetSocketAddress.html
InetSocketAddress(int port)
Creates a socket address where the IP
address is the wildcard address and the port number a specified value.
The wildcard is a special local IP address. It usually means "any" and
can only be used for bind operations.

Accept all incoming requests on a ServerSocket

I'm temporarily using the following line to create a ServerSocket on my Android phone:
socketl = new ServerSocket(port, 0, InetAddress.getByName("192.168.0.108"));
But it's really annoying and, besides, not user-friendly to change the IP address manually every time the DHCP assigns a new IP to the phone. So I'm searching for a way to get the IP-Address the DHCP-Server gave to my phone.
Ive tries InetAddress.getLocalHost().getHostAddress(), but that only returned the IPv4 loopback (127.0.0.1).
Is there a way to either get the current local IP(v4) or a way to accept every request, no matter which IP is used to connected? If the IP-Address in the line above isn't the same as the one the client uses, it doesn't work.
You could use
socketl = new ServerSocket(port, 0);
or even
socketl = new ServerSocket(port);

How to establish connections to a ServerSocket from internet?

My ServerSocket listens to LAN Connections and accepts them well, but when I try to connect to the same through my Phone - using the 3G connection - it doesn't seem to connect.
I tried using getMyIP site to get the IP and try to connect to it, it does get the right IP (checked with my router) but then no connections are accepted at all.
I tried opening the port on windows 7 and on my router altogether.
I put those lines in my Server constructor:
ss = new ServerSocket(port);
host=ss.getInetAddress().getHostAddress();
and I get the ip on host to 0.0.0.0
Thanks for your help.
- While you are at LAN, you can use the Private IP as well as Public IP ranges
- But when you are using the Internet to access the Server which is at your place, then you need to have a static Public IP address.
- You can ask for a static Public IP address from your ISP at some extra cost, there are also some site over net that some how provides a static IP on the basis of your Dynamic IP.
Private IP ranges Can't be used over the Internet.
Class A - 10.0.0.0 - 10.255.255.255
Class B - 172.16.0.0 - 172.31.255.255
Class C - 192.168.0.0 - 192.168.255.255
You need to have a public IP address. If you have a router it must pass traffic for the port you want to expose to the internet to your machine. If you have a firewall, it must allow external connections to this port.
All the changes you do are the same regardless of language you use and there is nothing you can do from Java to work around needing to do these things.
Check your firewall if it allows incoming connection. You need to make and exception there.
you need to bind explicitly the IP address on your machine which is allocated for that instance of time by your ISP.
You can get the IP address allocated to you by running ipconfig command on windows command prompt.
Use the following code to bind to a specific IP address
InetSocketAddress insa = new InetSocketAddress("22.23.23.111", 9090);
ServerSocket ss = new ServerSocket();
ss.bind(insa);
String host=ss.getInetAddress().getHostAddress();
System.out.println(host);
This prints the IP address allocated to you.

Remotely connecting two non-local computers with sockets

This question seems like something very obvious to ask, and yet I spent more than an hour trying to find an answer.
First I host and wait for someone to connect. Then, from another instance of the application, I try to connect with a socket - for the constructor, I use InetAddress, port. The port is always right, and everything works if I use "localhost" for the address. However, if I type my IP (the one I got from Googling "what is my ip"), I get an IOException. I even sent the application to someone else, gave him my IP, and it didn't work.
The aim of the application is to connect two computers. It's in Java. Here is the relevant code.
Server:
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
Client:
InetAddress a = InetAddress.getByName(ip);
Socket s = new Socket(a, port);
I don't get past that. Obviously, the values of int port and String ip are taken from text fields.
Edit: the purpose of my application is to connect two non-local computers.
As mentionned by Greg Hewgill, if you are behind a NAT Device (Router, etc...) you will have to do some Port Forwarding.
Basically, your public IP Address that you get from using "What is my IP" from google is your public IP Address, but since you are using a router with multiple computers connected to it, there is a protocol that maps multiple computers to a single public address called NAT.
What you'll need to do is tell your router to forward the incoming packets on a certain port to a certain computer.
The way to do this is highlighted in this article http://www.wikihow.com/Port-Forward

Java Socket on Different Machine Does Not Work

I've tried many examples on web and one of them is this:
http://zerioh.tripod.com/ressources/sockets.html
All of the server-client socket examples work fine when they are tested with 127.0.0.1
BUT it never ever EVAR works on two different computers with actual raw real IP address ("could not connect to host" on telnet and "connection timed out" when tested on java client - the server program just waits for connection)
Note:
Firewall is turned off for sure
IP address from ipconfig didn't work
IP address from myipaddress.com (which is totally different for no reason than that from ipconfig) didn't work
What is it that I'm missing?
If I can only figure this out...
Try binding on 0.0.0.0. This tells your socket to accept connections on every IP your local can accept upon.
Based on the comment where the the following snippet of code is mentioned:
requestSocket = new Socket("10.0.0.5", 2004); // ip from ipconfig
it would be better to use the hostname instead of the IP address in the constructor, as the two-parameter Socket constructor with a String argument expects the hostname as the String, and not an IP address. A lookup of the IP address is then performed on the provided hostname.
If you need to pass in an IP address, use the two-parameter constructor that accepts the InetAddress as an argument. You can then provide a raw IP address to the InetAddress.getByAddress method, as shown in the following snippet:
InetAddress addr = InetAddress.getByAddress(new byte[]{10,0,0,5});
You'll need to be careful when specifying arguments via the byte array, as bytes are signed in Java (-127 through +128), and numbers beyond this range (but valid octets of IP addresses) may have to be specified using Integer.byteValue.
Finally, it should be noted that it is important to specify the IP address of the remote machine, as visible to the client. The IP address listed at myipaddress.com may be the address of a proxy, as that is the public IP of your entire network as visible to the host server at myipaddress.com. Therefore, you ought to be specify the IP address of the remote machine that is visible to your machine and not myipaddress.com.

Categories

Resources