what is the best approach of connecting android to server and ip and corresponding port? THis connection doesn’t need to be all the time, but I am assuming I will send and recive files (in byte arrays or streams).
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since the Android Development Tools are native to Java, you can use simple Java Socket APIs to accomplish this goal (see ServerSocket and Socket).
Server Code
You must start by opening a
ServerSocketon your host computer by defining a port to listen on:Then you must begin listening for a client by calling
ss.accept(). This method will block until a client connects:You now have a socket on your server that you can manipulate as you wish (probably through the use of
ObjectInputStreamandObjectOutputStream):Client Code
You must establish a connection with the server that you have just created. You will do this by initializing a socket and passing in the IP address of your server (usually localhost for most testing purposes) and the port number on which your server is currently listening:
Again, establish some streams for communication:
And there you have it! You can now easily communicate with a server from an Android device. It is much simpler than you would think.
Please note, however, that this architecture implements TCP, which is much slower than UDP and will not work for any type of fast-paced data intensive games, but should accomplish your goals given your description above.