I want to develop an application that has socket connection with many clients or Android devices connected to one Android device as the server with TCP connection. The server receives information (name, IP address, device name) from the clients and saves it to the database.
Once the information was saved, the server will send a message to the clients and show the clients the list saved in the database (name, IP address, device name).
I'm new with socket programming and I want to implement KyroNet library that is working for Android too. Can anyone help me give a sample code to get started with this project?
I'm not familiar with the KyroNet library, but you should look at the BluetoothChat example application provided with Android. Bluetooth sockets are just about the same as a regular socket, so it should be nearly identical code-wise. The app also shows how to implement a Handler and Thread for the connections.
Related
Is it possible to do a chat between client and server (two android devices) using socket programming in android?If possible,anybody please offer me a help.Even a link is acceptable.
It is possible. you can use socket programming and create chat application with 2+ devices. one device must act as server and others have to be client. if your want to your server to be android device and don't want have web server side it does not need an internet connection. just a access point or one mobile hotsopt is enough. but all device must be connect to same hotspot or access point.
I want to to share information between multiple android devices running the same app under the same network.
I have used Network Service Discovery(NSD) framework and devices detect each other.
How can I then share data between them?
The protocol used by the NSD APIs allow you to exchange host addresses and port numbers. That means you can open a server socket on one device, advertise that as a service, and then connect to that port from the other machine.
As far as I can see, there's no higher level API provided in Android. Most of the existing APIs seem to be geared towards Cloud data exchange, not local data exchange.
I spent a week looking for a convenient answer to my question. I am trying to connect Android to Arduino through Cloud, Where it has 2 operations:
Arduino can trigger a push notification to the android device anywhere.
Android operates Arduino through Cloud.
now the first point is easy to be done, by having a 3rd party server where Arduino POST user credentials (can be configured when the device is bought) with the message to PHP server then the server will get GCM's Registeration ID from MySQL and POST it through cURL to GCM to push notification to the related Android device.
or by using Arduino YUN, we can directly send cURL from Arduino itself.
However for the second point where I am stuck, I can't figure out a way to maintain the IP address of the Arduino without actually making the user to enter the IP address of his Internet in the Android application interface.
The solutions I came up so far:
Connect another Android device to Arduino at all the time using ADK then it will become a communication between Android device with another Android device through GCM and the GCM will handle getting the IP addresses of both.
Using MAC address to get to the Arduino but this is insufficient approach because the MAC addresses are hard coded in Arduino, and then spoofing may occurs.
I am not convinced with the proposed solutions so is there a possible way to use GCM or any similar system like Parse in Arduino and make it have a unique registration ID just like android then I can map the user's Android registeration ID with the related Arduino registration ID. or can anybody give me another solution or method I can follow. Thanks in advance.
P.S: I dont want to use static IP address or Port forwarding, the idea of this project is to create a device that is user friendly and doesn't require a lot of configuration from the customer.
Knowing that your Arduino already communicates with a third-party server, you can take advantage of it and additionally get the IP address of the Arduino device making the GCM id registration (in PHP you'd do this by using $_SERVER['REMOTE_ADDR']).
This way you can keep a 1-to-1 identification of a GCM id and a public IP address to know to which Arduino device to send the signal.
This would need that you implement some timeout process (for instance, implement a keepalive system and if a device doesn't send a POST request each X time, just remove it from whithin the connected devices). Also, if the same device connects from two different IP addresses, just keep the last one so you're not mixing up.
Is there a way to push data from local server (hosted on intranet) to an Android application without internet connection? The app is supposed to work only on LAN over wifi. GCM is out of picture as internet connection is not available. However the app will be running on both the devices all the time.
The model, I am talking about, is something like this:
Devices: Local Server - Wifi Connection - Android Device 1, Android Device 2
Android Device 1 updates some data on local server over wifi... Server needs to notify Android Device 2 about the change over Wifi.
Thank You very much for the help
Yes, it is possible.
I used sockets connection to do so.
requestSocket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(ipAddress, portNumber);
requestSocket.connect(socketAddress, timeOutPeriod);
At the server end, you need to open those sockets and ports using whatever languages you prefer. After which you can read or write data using streams.
You should be looking at something like socketIO for that used in a project not that long ago and as long as you know the server address and it is the same on both devices you should be able to push data to and from the server from any device. A group of us used it to make a note taking app. you may need to build your app in html and javascript unless you want to use the java socket communication that would also do the job but more work would be involved.
May be you can implement Message Queue using message server
I have to develop a chatting application and I want to use socket connection. is there a way in android for it. Which will be better way for long time application.
Give me suggestion about it.
Have you looked at the Sockets class? You can use sockets to write and read data between two clients in background threads that update the UI thread with the data received/sent.
Generally with sockets one device (the client) connects to another (the server) and it is expected that the server's hostname will not change. However, you can't guarantee this with devices on mobile networks so a better approach might be to have two devices connect to a well known server that relays chat messages through. If you're just trying to do a basic chat application between devices on the same LAN it would be fine to directly connect them however.
https://developer.android.com/reference/java/net/Socket.html