Broadcast event when IP changes? - java

I'm developing an application that is sending data through tcp/ip
I've made my own Name server that the clients register their IP and listening port. All of this is working very well.
Right now the clients send a ping to the server to get the latest IP and updates it if necessary. Since the app is already using quite allot of network I would prefer to minimize it as much as possible.
My question is if the ConnectivityManager (or any other service) sends a broadcast when the IP changes? Or is this not possible for the phone to detect this locally?

For this you need run an service as background with the help of BroadcastReceiver,place the initial IP on DB or some shared preferences,check and update your IP by using background service.Refer the services here http://www.vogella.de/articles/AndroidServices/article.html

Related

Creating local server on an Android device

I have an idea for an application where one device is a server, and other devices connect to this server.
The clients would send data and call HTTP requests (or whatever is equal to them in this case), and the server would receive the request and act on it.
I understand there is such a thing as Sockets, but does this fulfill my requirement? Will I be able to call requests on the other device?
Example usage: The client clicks the + button, and the server increments the value.
Have a look at P2P, it uses Wifi Direct, so one device can be setup as a host server and the other devices can register to that as clients.
Check out the official documentation.

Seeing multiple servers on a network

I am busy with a project where I'm creating a basic client/server chat application which allows a user to create a server on their local network on a port of their choice and then have multiple clients connect to that server by specifying the IP and port number of the server(So far so good).
I would like to how I can have clients see all the possible servers they can connect to on their local network when there are multiple servers running on their local network over different ports and then allowing them to connect to one. I am using basic Java socket programming for this project.
You could have
use a UDP based protocol where each server publishes it's IP:port every second.
you could have a service where each server registers. You could chat with that to get the list of all services.
The nice approach of the later option is you can use one of your chat services for server discovery. When you want to get the list, you send a message to a channel on that server which all the servers are listening to and they respond with a chat message.

How to find specific desktop's ip address on network?

I'm coding an app which consist of two pieces. Desktop and android. There is one desktop and several android devices. (don't know the count.) I want to communicate android devices between desktop with TCP. However, android devices doesn't know desktop's lan ip address.
I thouht 2 ways:
1-Desktop app changes the local ip address on start. So android devices know the ip address. (I coded with that ip address)
2-Desktop app always tries to connect ip addresses (192.168.0.1 - 192.168.0.255) to sent desktop's ip address. And when an android device connect to the network accept the connection then know desktop's ip.
But there is some problems in both ways.
On first, you must be administrator to changing lan ip. So run command as admin with java is a problem. Because if I do this, when user start the program, uac always asks for it.
On second, I think there will be performance issues because of app always tries to connect. Exept this, when android device connect and dhcp gives it 192.168.0.5 , but loop is on 192.168.0.150. So android device have to wait for connection.
Is there a better way than these?
Look at this post Network discovery in Java using multicasting
I think this would be the best way to do it.
The server will listen for a broadcast message from client
the client sends a broadcast request asking for server ip
server receives request and replies back with server ip.
You can use the hostname. If the network is properly configured, the host name will point to the correct ip even if it changes

how to send messages using only IP address within LAN

I have two wifi modules that connects to my access point. my android phone connects to the same access point. Is there a way in Java to be able to send a message to any of the clients from my phone using just the IP address. I do not want to run a server on my phone.
I'll appreciate any assistance. Thanks
I feel there is a confusion about IP addresses and servers in general. An IP address is a way to find an entity on the network (it is an address). Since your phone is connected to the same access point, it is (very likely) on the same subnet. What that means is that it can talk to any of the other entities without going through a gateway, that is all.
While it is able to talk to them, there has still to be some entity listening. When your phone sends a message to a particular address (the other node on the network), the packet may make it there, but for the communication to proceed:
There has to be some listener on the other end that picks that incoming message
The communication has to follow a prescribed set of rules (protocol)
From what I gather, your phone is acting as the client, not the server. It initiates communication with the other entity on the network. Depending on which node on the network you are trying to talk to, that node is supposed to be expecting that communication and know how to carry it forward.
If you clarify the setup and your desire, may be you can get more to the point response.
What you were not understanding here is that you can send the message to that device with ip address but that data will not be shown on that device's screen because you didn't provided any port number for a process which is listening on that particular device. Your message will be sent but the device would not know what do with it.

Connecting to broadcast IP address

I am currently working on simple java program that should be able to seek out computer in a local network that runs my second java application, all using UDP networking. One of those apps opens DatagramSocket and starts a thread that processes all of the inputs. The other application connects to broadcast address of current LAN network (e.g. 192.16.0.255), sends a packet and receives the response. I'm not very familiar with the way this works but here is what I'm wondering:
If I launch two of those responding applications each on different computer of the same network and run client application on other computer, which of those will it connect to?
I thought it would connect and send packet to both but it connected just to one of them and sometimes not the same one.
Could you please explain this matter to me? I would appreciate it.
If I launch two of those responding applications each on different computer of the same network and run client application on other computer, which of those will it connect to?
Neither, UDP is a connectionless protocol.
I thought it would connect and send packet to both but it connected just to one of them and sometimes not the same one.
UDP is a lossy protocol, sometimes the data will go to both, one or neither. Your router could be setup to try to direct the broadcast traffic, but usually it will attempt to send all packets to all listeners.
BTW: All the listeners must be on the 192.168.0.255 C class subnet. A host with an IP address of 192.168.1.1 may not see this packet.

Categories

Resources