So I have this code
server = new ServerSocket(6789, 100);
showMessage("Your IP is: "+server.getInetAddress().getHostAddress()+"\n");
and I have also tried .getInetAddress().getCanonicalHostName() and some others, and they all display 0.0.0.0 or 0.0.0.0/0.0.0.0.
Anyone know why this is happening?
Also, when the client connects with the RIGHT ip, it works, it's just displaying it wrong.
0.0.0.0 means it is bound to all adapters, not just one of them like localhost or your network card.
I think you can use one of these approaches -
InetAddress.getLocalHost().getHostAddress()
if you have multiple interfaces you might prefer to use this one -
NetworkInterface.getNetworkInterfaces()
What Peter said. If you want to know about specific local interfaces, look at java.net.NetworkInterface static methods, and from there get a specific InetAddress. Then from there, if you want to use only a specific local interface, you can pass the InetAddress to the ServerSocket constructor.
Related
I'm developing a simple Spring Boot RESTful API for poll management. In a few words, it's possible to create public polls and other "users" can vote for it.
Now I've to make sure that each client just votes once per poll. Because I want to prevent using common authentication mechanisms like HTTP Basic or JWT, I thought about authorizing by clients IP address. Means I store en entity like the following in database:
public class Vote {
private Long pollId;
private Long choiceId;
private String ipAddress;
...
}
Using something like such an approach, I prevent the need of authentication and account management.
Is this the right approach or are there better ones to ensure each client votes just once? Also how to deal with IP spoofing? Hope for any recommendations.
Is this the right approach or are there better ones to ensure each
client votes just once?
Not really. The idea that each computer has a unique IP address is only partially true.
In reality, people have more than one device (e.g. phone, computer at work, computer at home). And each device could be connected to a different network, with a unique IP in each one. Also, IP address change quite frequently. Disconnect your home modem/router for a couple of minutes and you're likely to get a new IP address when it reconnects. So one can change his home IP and vote again. Moreover, many (if not most) clients are behind NAT devices, which means that their IP is shared with many other users. Under the scheme you propose, once someone behind the same NAT as you votes, no one else can.
Lastly, users can easily use VPNs, TOR and various other techniques to basically vote as many times as they want.
Also how to deal with IP spoofing?
IP address spoofing is not trivial if one is using TCP. However, getting an actual IP address that's different from your current one is quite easy (VPNs, TOR, etc), and there's little you can do about it.
I've tried searching for this but haven't really found a solution so decided to post a question.
I'm working on an application where a user will input an IP (an SNMP device) and my application needs to connect to it and work with it. During runtime of the application, the user may provide another IP address and then I need to connect to the second one also keeping both the connections alive (as singletons).
My problem is I'm not able to wrap my head around this conceptually. My connection module is right now something like the following:
#Override
Configure() {
String ip = first ip;
}
#Provides
Connect connect() {
// connect to ip
return connection;
}
Can anyone give me some hints here?
You should probably pass the IP address as an argument for your Connect class constructor. You can then look at FactoryModuleBuilder so that you can inject dependencies to your Connect class in your code. As for your requirement about singletons, I am not too sure of what you mean there. A singleton means, by definition, that there's only one instance of a class. Here, you want two (or possibly more). What I suspect is that you want at most a single Connect instance per IP String in the entire application. If that is the case, your factory needs to be a bit cleverer that the one created automatically by FactoryModuleBuilder. It could be a singleton itself and store an index (map? concurrent map? cache? It depends on your thread-safety requirements) of ip -> connect instances for those that have been already created.
Hope it helps.
I'm having a little trouble with Java RMI.
Am I able to check if a registry exists?
This line of code is supposed to give me the registry.
LocateRegistry.getRegistry(ip, Registry.REGISTRY_PORT);
But when I call it with a wrong IP address or an IP address where no registry can be found, the method gets stuck.
So my question is, can i somehow check if there is a registry at a certain IP address BEFORE calling getRegistry()?
No. In any case the best way to test the availability of any resource is to try to use it. In this case, call lookup() and catch the exception. You have to do that anyway, so doing it twice is pretty pointless.
I have following task, would you suggest, whether (and how if yes) it is possible to solve it:
A computer has 2 LAN adapters; each one is connected to different network provider.
Some information must be sent via first one and some information via second one.
Is it possible somehow to specify which adapter to use by initialization of a connection?
In Java you can use the NetworkInterface class, in conjunction with Socket.bind() to specify what interface to bind to.
Example, taken from this reference:
NetworkInterface nif = NetworkInterface.getByName("bge0");
Enumeration nifAddresses = nif.getInetAddresses();
Socket soc = new java.net.Socket();
soc.bind(nifAddresses.nextElement());
soc.connect(new InetSocketAddress(address, port));
Then by setting up two sockets, one per interface you can use both simultaneously.
The other way to solve this problem though is with interface bonding, which is a configuration issue (e.g. on Linux) and presents two physical interfaces as one virtual interface. (Bonding is the exact opposite of specifying which interface to use when creating a socket, but isn't a programming issue though)
I am rather new to java RMI and am trying to create a Peer 2 Peer bit torrent like application wherein multiple instances of the same peer may be on the same machine. This would mean that I would need to be able to have more than one remote object of the same type registered on the same machine. The RMI registry seems to only allow me to have one implementation of a Remote Object on any machine as the registry would not be able to differentiate between which of the objects it should be returning a reference to. Is there a way to bypass the registry such as by specifying an IP and port where I know the other peer is exposing its remote object? If not do you have any ideas how I would be able to create multiple instances of the same object on the same machine? Any help with this would be greatly appreciated...
You can either start multiple rmi registries on different ports, or better bind the instances of the object under different names multiple times. But the best way is probably to do the logic in your code and return a new remote object every time it is needed. E.g. dependent on a parameter:
public MyRemoteObject connect(String name) throws java.rmi.RemoteException {
if("first".equals(name)){
return firstinstance;
}else if("new".equals(name)){
return new MyRemoteObject();
}
...
}
or something like this...
I would suggest you to forget about RMI - IMHO this technique is not applicable for your use case.
Define yourself a network protocol including a serialization and deserialization logic and use this for sending and receiving data on a raw socket connection.