Using InetSocketAddress correctly - java

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

Related

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.

Resolving ip-address of a hostname

I have the DNS server IP address and a hostname.
Using Java, how can I find the IP address of the hostname as returned by that DNS server using the IP address and the hostname?
Take a look at InetAddress and the getHostAddress() method.
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println(address.getHostAddress());
You can do it like this:
for(InetAddress addr : InetAddress.getAllByName("stackoverflow.com"))
System.out.println(addr.getHostAddress());
You can use InetAddress for this. Try the below code,
InetAddress address = InetAddress.getByName("www.yahoo.com");
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());
As suggested by all above, you can use
InetAddress.getByName("hostName") but this can give you a cached IP, Read the java documentation for the same.
If you want to get a IP from DNS you can use:
InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr("hostName");

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

HTTP request and IP address

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

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