How to connect with difference Ip address - java

My server have many ipaddress (a full /30 block)
how to make socket with other ipaddress than default ip address
Socket socket = new Socket()

Use one the alternative constructors
e.g. Socket(java.net.InetAddress, int port)
For a server wishing to use a specific ip address and port, you should be using ServerSocket
Use bind(java.net.SocketAddress) to specify the local ip address and port.

Related

Java TCP/IP: Finding the IP address of an incoming connection while listening on a socket

I have a Java server implementation which listens for incoming TCP/IP connections on a specified port. Is there any way of finding the IP address of incoming/accepted connections?
Yes, upon accepting the incoming connection you would get a Socket instance, so you can get the address to which it is connected as follows:
String hostAddress = socket.getInetAddress().getHostAddress();

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.

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.

Get global IP address from java http server

I m starting a local http server using this code:
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/intro", new MyHandler());
server.setExecutor(null);
server.start();
now I want to hit the url /intro from some other server.The problem is I don't know the ip adress to hit.Doing server.getAddress() gives 0.0.0.0:8000.I want to find the global ip address.
You have to know what is your ip (cmd and ipconfig) and get IPv4 address. But remember that is only your local IP visible in your local network, so only users in the same network can see it. In global network you are identified by your internet provider IP.
InetSocketAddress(int port)
Creates a socket address where the IP address is the wildcard address and the port number a specified value.
InetSocketAddress(InetAddress addr, int port)
Creates a socket address from an IP address and a port number.
The wildcard is a special local IP address. It usually means "any" and can only be used for bind operations.The value of this IP address is 0.0.0.0.
so use another constructor of InetSocketAddress when you can put hostname
new InetSocketAddress(String hostname, int port) calls InetAddress.getByName(hostname).
Now you shall get your local ip address when you query server.getAddress()

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.

Categories

Resources