trouble creating a socket service for a multi activity app - java

basically i need a constant socket connected to a server that can be used in multiple activities. at this stage im trying to create and connect the socket on a login screen, where then a new activity starts, sets the input/output streams and then handles the data
im having alot of trouble trying to get the socket to persist in multiple activities. i have read into it and there is a lot of conflicting posts saying that services are better and a singleton is better. nonetheless i choose to try and implement a service. except i cant get it started.
How to keep the android client connected to the server even on activity changes and send data to server?
i first tried to implement the code in the above link but it seems to depend on custom written code called TCPClient. not knowing the internals i moved onto the link below
Android service for TCP Sockets
needless to say ive gotten nowhere in understanding and following the Android resource hasnt really helped. according to the guy who posted the first link his code works, i just dont know whats inside the TCPClient class.
im trying to use the code from the second link, and adapt it for my app, but hasnt worked so far. right now im poking around in the dark.
ultimately, id like an explanation on how to get a tcp socket service started, or a better way to attempt this.
TIA

Related

Do a Task When Internet Connection is Available

I'm Working in an Android project that generates data and then it registers that connecting to a WebService, the data can be created even if the user doesn't have an active internet connection in that moment. So, I want a way to run a function that calls the webservice and pass the data when a valid internet connection is detected, all in background. I have read about SyncAdapter and JobScheduler, but I'm very confused about these options because I'm very new in Android development, any help or suggestion to decide which option is better to accomplish this would be very appreciated, thanks in after hand.
You should use broadcast receiver. By that way you can check internet connection in a broadcast receiver and then do some stuff in background.
this is a good tut to how use it.

Proper way to create and connect to a Socket in Android

I'm getting to a point in the development of an Android app where I've reached a stumbling block: how to create, manage, and connect to a Socket in Android.
My app needs to keep a persistent TCP connection to the server in order to exchange JSON formatted strings back and forth. After reading up on the subject, I've determined the best way forward is to create a Service when the app starts up (by extending the Application class and starting the Service in onCreate()), then read from/write to the Socket as needed. But how do I do that?
I obviously know how to create a Service and how to create and work with a Socket. But I don't know the best way to interact with one in an Android environment. Should I create an AsyncTask whenever I want to write data? Should I use Intents? Any help on the subject would be wonderful. And if my question isn't clear, I'll be more than happy to clarify anything.
Within the service, you can simply listen as you would in a regular Java application. This means you can safely wait for IO as you normally would.
You will have to use Intent when you wish to notify your activities about new data arrival using sendBroadcast and receiving it to your activities by either registering a BroadcastReceiver using registerReceiver or modifying your manifest file.
This is a good tutorial that may help you with broadcasting for Service <-> Activity communication.
Apparently I was thinking about this problem incorrectly. What I need is to implement a "Bound Service", which will give my Activity an interface to interact with in order to send and receive messages.
Here's a link to the tutorial I found which gives an excellent overview of how to do this.

making several request to a server with java without getting banned?

I don't if the question title fits, but here is my problem:
I have a regular webhosting service in hostmonster, with a website built in php.
So I have php script running in a cron job that monitors a xml file for changes, and everytime a new entry comes into that xml file the script stores it in a database.
In the other hand there is java built desktop client, which needs to be noticed ASAP that a new entry is created, for this the client connects to a second php file every second, and this second files tells if there has been changes or not.
The thing is, every 260 connections my I.P gets banned from the server :( and the client crashes, the client will be used by several users.
I contacted support on how to handle this, but they tell me to use a single connection, I tried reusing the UrlConnection but after the first request it just gives null. then I tried with Sockets but no luck. I know there are libraries that manage this but I dont know how are they called. Can someone give me advice?
thank you guys.
Use a long polling method. Hold the connection opened until response arrives. This way you only need to ask for the update once.
PHP may not be the best tool for this job though.

android multi player game over network

I'm programming an Android multi-player game, which basically consist of a server where the clients connect and exchange messages. When the player connects to a server, a player list is return to him/her. A player can then select a user to challenge - of course he must select a player from the player list, which only contains connected users.
When a player1 challenges player2, a message needs to be transmitted from player1 to the server, which in turn must send a message to the player2, notifying him about the challenge. The player2 can then accept/decline the challenge.
I can use the following techniques to make this happen:
Use custom server/client with Java socket programming. The server basically accepts a connection from the client, spawning a new thread for each connected client. The problem with this are:
There needs to be a persistent connection open from client to server wasting battery life of the android phone. This is not really big limitation since the battery isn't consumed that much.
When I'll want to develop another game I'll have to rewrite the client/server code from the scratch - also choosing another port to listen for incoming connections - the whole concept gets rather difficult to maintain.
I'm also worried if this is the way to do it. Spawning another thread for each clients sound quite a lot if thousands clients are connecting at the same time. But I'm guessing the PC games do it like this. Not sure about android.
Use Java REST jersey to build the client-server on top of HTTP. This would be a perfect solution if the server could easily send notifications to clients. There are actually multiple design decisions here:
the client pulls the server for any new data/notifications every few seconds - this is really bad, since we're stuck with non responsiveness, delay, etc.
the client can send a waiting request to server, so the client receives the response only after some data becomes available. This is better, but can still produce a delay when two notifications one after another need to be sent to the user. The first notification is sent instantly, since the client already has a connection open, waiting for data to receive. But we would have to wait for the client to initiate another long http request to receive the second notification. The problem gets bigger as there are multiple notifications that need to be send in a row to a specific client.
the client can initiate a http streaming, where the communication is left open when the request is handled, so the server can also send multiple messages to client whenever it wishes. The problem here is that I don't know how well this works on Android. I've looked at several implementations:
Java jersey + atmosphere: didn't succeed in actually making it work. This seems the most promising, but I don't want to spend too much time on it, since I'm not even sure if it does what I want.
Deacon: seems pretty neat, but after seen the video tutorial on their official web page, I'm not sure that it can do what I need. When a player1 challenges player2, can it send a notification to player2 letting it know about the match request?
I would be glad to know how other multi-player games handle the network communications, if the two players are playing the game over the network.
I'm also open to a totally new suggestion how to achieve what I want. I can pretty much code anything, so don't hesitate to let me know of some more difficult way to achieve the network communication.
Let me also mention that I'll be glad to implement a totally specific method to work in my case, so it can be anything that will do the job done, but I'm also looking at more general way for communication between clients and server. So that I can program an interface/whatever and reuse the code in other android games, android applications.
I hope I presented the problem allright and that I'll receive some valuable answers.
Thank you
You should take a look at XMPP. It's a protocol (originally created for chat programs) that allows sending of xml data between users.
It has a separated client-server relationship, so that you can focus on developing a client application fit for phones, and a different server depending on your needs.
There are loads of information available on the protocol (I should know, I wrote a thesis about using the protocol in game applications), but you can start by looking it up on wikipedia to see if it is what you want.
aSmack is a library for creating android xmpp-clients. It takes some tweaking to set it up and get everything to work, but once you do, it's neat.
EDIT: relating to the answer suggesting using the C2DM:
from the c2dm docs "Sending large numbers of C2DM messages":
Are you sending C2DM messages too frequently? If you need to communicate with your application frequently over a short period of
time, C2DM is probably not the best solution. Instead, consider
implemeting XMPP or your own protocol to exchange messages, and use
C2DM only to send the initial notification.
It sounds like Android Cloud-to-Device-Messaging might be what you need
Push notifications without the app having to keep a connection open
I would vote in favor of some message passing technique - like activeMQ, rabbitMQ, zeroMQ eor something like it. On the server side you may stick with java , or javascript ( like
node.js ) - such solution would provide most performance and minimal latencies.
If latency is not that critical, you may as well use REST calls with JSON

Less response Time from webserver

I had developed an app in android that take some data from a database stored on a web server and draw a GUI according to data captured.The problem is that when i run app on localhost using (WAMP) it creates GUI in 2 seconds but when i connect it to the webserver it takes almost 7 to 10 minutes.I am astonished what could be the possible reason behind this.
I had not used any ASync class in my code.I had used httpclient.execute stuff to connect to web server and JSON pasring.I hope you understand.Please tell me what could be the reason behind less response time.
first thing is whats the speed of your internet ? if your connection is slow that could be the reason. and other thing is you might be doing all gui processing on client via request only. my suggtion is have screens ready and fill data via things like json/REST stuff. for others i can only tell you once you show some code.

Categories