HTTP request and IP address - java

I have got the ip address from the HTTP request object using
request.getRmeoteAddr() => 127.0.0.0
However im using netty and when I use
SocketAddress socketAddress = channel.getRemoteAddress();
InetSocketAddress inetAddr = (InetSocketAddress)socketAddress;
ipAddress = inetAddr.getAddress().toString();
=> 0.0.0.0.0.1
This is causing me problems when trying to compare, i want them in the same fomrat...
any ideas?

When you've a class that represents something that can be represented as lots of different strings, then don't compare the strings; compare objects of that class.

use getHostAddress(); that should do it.
InetSocketAddress inetAddr = (InetSocketAddress)socketAddress;
String address = inetAddr.getAddress().getHostAddress();
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html#getHostAddress()

Related

How to bind java.net.MulticastSocket to localhost

I would like to bind a MulticastSocket to the address 127.0.0.1 (Socket should only be reachable within the current host) but with the following code example i got a
java.net.SocketException: Network is unreachable: Datagram send failed exception
Is there a way to fix the problem? Here is my code
int port = 6677;
InetAddress group = InetAddress.getByName("232.0.1.10");
try(MulticastSocket s = new MulticastSocket(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),port))){
String msg = "Hello";
s.joinGroup(group);
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),group, port);
s.send(hi);
}
Multicast is a little odd when compared to traditional UDP communication. The whole point is to share data on a known "channel", simultaneously, to anyone who wants access. This sharing is "signaled" to the network by using an IP address in the range 224.0.0.1 to 239.255.255.255. If you try to bind to 127.0.0.1, you just aren't doing Multicast anymore. And if you take a minute and think about it, that makes sense - you can't share the internal interface with other computers.

Using InetSocketAddress correctly

I am attempting to use the InetSocketAddress that is part of Java.net. Although when I try to actually give it data for its perimeters it does not work. The documentation does not actually specify how to define the IP address and port. It states it goes as follows: InetSocketAddress(InetAddress addr, int port) , addr being the IP address. I have tried the following ways:
InetSocketAddress("0.0.0.0", 0000)
InetSocketAddress(0.0.0.0, 0000)
InetSocketAddress(0.0.0.0:0000)
Obviously none of these work (using arbitrary values), all but the last, the port works cause it's just an int but I can't figure out how to format the IP address correctly. The docs I have read over in search of a solution are as follows:
InetAddress
Inet4Address
InetSocketAddress
None of these actaully have an example of how to format the IP address (unless I'm blind of course).
InetSocketAddress wants an InetAddress object for the first parameter. So you need to send it one with something like:
InetSocketAddress(InetAddress.getByName("0.0.0.0", 0))
I know, getByName() doesn't sound like it should be used with an IP address, but it works with both an address or host name.
You can use this.
InetSocketAddress address=new InetSocketAddress(InetAddress.getByName("0.0.0.0"),0000);
InetSocketAddress has no parameter is String, int constructor

Why do we have to use InetSocketAddress to enter our port number for using ServerSocketChannel

why do we have to create an object of InetSocketAddress?
but for ServerSocket we just use int to enter port number
Example:
try(
ServerSocketChannel listener = ServerSocketChannel.open();
ServerSocket serverSocket = listener.socket()
){
serverSocket.bind(new InetSocketAddress(2266));
//we can't use serverSocket.bind(2266);
}catch (IOException e){
e.printStackTrace();
}
Your question is somewhat unclear.
ServerSocket#bind() establishes the local end of the connection. If your system has more than one network adapter and/or more than one IP address, AND you wanted to connect the socket using a specific local adapter or IP address, then you would provide a complete InetSocketAddress(host,port) with both host and port, where the host part was one of your local IP addresses.
In the default case where you have only one IP address, or have more than one but don't care which one is used as the source, you can omit the host and just specify the port, as in your example.
The API is defined this way. A TCP connection is symmetrical, and is defined by its two endpoints. An endpoint is a pair (host,port). This applies to BOTH ends of the connection. Thus the bind() call takes an InetSocketAddress parameter. For convenience, InetSocketAddress will assume the default host that specifies "use any available interface" if you provide only a port.
The API designers could have added a bind(int port) method to build the InetSocketAddress(port) behind the scenes, but clearly didn't feel it was necessary.
InetSocketAddress creates a socket address where the IP address is the wildcard address and the port number a specified value
wildcard is special IP address which can be used for binding. If you don't want to listen "everything" but maybe spesific IPs, say, 5.5.5.5 for example, you will be binded to that IP address but not others.
But without the IP part to it means any IP will be listened so pretty much no difference for this particular usage.

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 send request from virtual IP address to a server?

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);

Categories

Resources