Getting IP address of host - java

I created a little game and now I am trying to make a multiplayer game out of it. One android phone should host the game and the other phone should be able to connect to this server. My question is how can I tell the Client to which IP he should connect. Is there a way to scan for hosts?

For Android games there is a perfect (and simple) library called kryonet.
It has a method - discoverHost - for scanning local network for devices that are listening for incoming connections.
Also, it contains classes such as:
Client
Server
that makes implementing simple communication protocol (on both UDP and TCP protocols) really easy.
As many people uses this library (including me) - you can find a lot of examples:
The Simplest and Easiest Game Network layer with KRYONET
KryoNet Client and Server Example
Kryonet's itself examples
... and many others. :)
Update 1:
There is also a great library from Google called Nearby Connections API that allows to autodiscover devices in local network and allows to create multiplayer (or multiscreen) games.
Furthermore, Google has created nice (multiplatform) APIs for multiplayer games over the WAN:
Real-time Multiplayer API
Turn-based Multiplayer API.
Sample for Nearby Connections and Real-time Multiplayer APIs you can find here.

You can use Network Service Discovery:
http://developer.android.com/intl/es/training/connect-devices-wirelessly/nsd.html

If I understand you correctly, you want the game host to advertise itself in the reachable network to clients to join.
There are many possibilities around this on different levels of the network protocol stack.
Somethings to look at, that come to my mind:
Rendezvous protocols: https://en.wikipedia.org/wiki/Rendezvous_protocol
Bonjour: https://de.wikipedia.org/wiki/Bonjour_%28Apple%29
A well known central directory server, that keeps track of who is offering and who wants to play (LDAP, DNS, ...)
Word-of-Mouth: Tell your IP to the user and she will broadcast it via voice to other players.
IRC channels or some other means of central communication established outside of your control, like a Twitter account.
You see, there are quite some possibilities. Maybe you want to narrow down you question to your specific needs:
Who is the audience, is it a "LAN-party", other phones somewhere in the mobile space?
How much effort do you want to put into it?
What platforms do you need to support?

Related

How to implement client server in android for making BINGO game, but not for chat application

I am making a game of Bingo. For that I will be connecting two mobiles via wifi- hotspot and the grid should be shared to both clients, that is both mobiles should see the grid. How can i pass the grid two both clients? Also, how to pass numbers in between client and server? Via object or other thing and how? Also how to exactly implement client server application making 2 java files n android? PS: we are not implementing a chat application.
Bingo XML file: https://github.com/TanishaShrotriya/SDL/blob/master/activity_main_bingo.xml
Java File:https://github.com/TanishaShrotriya/SDL/blob/master/MainActivity_bingo.java
You have a few options. First you need to determine which architecture works for you.
Client/Server (AWS Instance with APIs, and Firebase with PUSH)
---This will enable the devices to get current came status on open, and push changes to their board on confirming their space was called/filled.
---This will enable other devices in that "room" a name given to a virtual shared space for a game. To receive PUSH notification of changed data to display.
Next you need to decide if the server will maintain the full state of each user or just the current called values for comparisons.
If you decide that you do not want to write APIs and setup a Server for PUSH, you can do an ad-hoc wi-fi or even a BLE based communication among everyone in the area. However, this will require significantly more understanding of how to pass bytes, handle network issues, latencies, and communicating from device to device.
If you choose to do the ad-hoc wifi as you mentioned you would have to maintain a ton of connections like a mesh network. A better option would be to have all devices join the same wifi network so that they have access to each other, but aren't necessarily keeping track of mesh networking.
However, even if you are on the same wifi knowing who to send the information to can be difficult without a server to retain routes and IPs of current players. So you may have to create a host so that it can listen for enrolled players. The host device would need to retain routes to all other devices so that it can send the info to each IP address when a change occurs.
I still highly recommend going the client/server route of APIs and PUSH it will be far more reliable.
Now as far as how to pass the data, you could of course screenshot via code and send images, but that is wasteful. I would recommend having a matrix that keeps track of the values covered and simply send a JSON snippet of the card_id, the player_id, and the json of covered blocks to the server that can then push changes back to all players in the "virtual room". This allows them to see how their fellow competitors are doing.
So I would have to ask for more details about what your goal is to help any further, but that is my recommendation, because ad-hoc wifi or bluetooth is doable, but on a mesh scale size you will have a giant headache on your hands. The host scenario device with all on same wifi would be my second direction if you are really anti-server and APIs, which the only reason I could see you avoiding that is if you wanted to play off wifi that doesn't have internet or if you don't want to pay the $30/month for traffic while it is small.
Let me know if that helps or if you want to provide additional clarity.

GCM vs. Socket in android

In my knowledge, most of android tutorials and examples out there rely on use of GCM for sending data from server to android device; and use php scripts along with post/get methods to send data from device to server.
A friend of mine(someone who doesn't have any knowledge of android programming) simply asked me, why can't we use Socket class in java? In traditional java programming, you use sockets (IP adress + port no.) to achieve functionality similar to GCM (single server multiple clients - using proper techniques you can ensure messages are not lost, just like gcm).
Can this traditional socket approach be implemented in android as well? If so, what are the pros and cons of GCM and Sockets? In which situations would the use of sockets be appropriate?
Whatsapp and lot of other apps, to my knowledge, use GCM. Are there apps which use sockets?
P.S: I'm just a C.S. student; apologies if I've got my concepts wrong somewhere
GCM uses sockets too. However, they are managed by the system and thus can do things a regular app cannot do. Generally, for any type of 'push' notifications, the system needs to have a long-lived socket, and a 'notification' is just some data received on that socket. All of this is not too hard to implement on an always connected machine, such as a desktop or a server, but there are additional challenges on mobile. For example, switching networks (3G<->WiFi), going out of range, maintaining a connection without keeping the device awake all the time (and killing the battery in the process). So yes, you could implement something similar using a service , sockets and your own server, but it's not trivial on mobile.
Check out http://mqtt.org/ for an open-source implementation.

Are there any TCP/IP stack implementations on top of Bluetooth for Java, more specificaly for Android?

What I need to do is pair with a Bluetooth enabled device which hosts some webservices and browse for DNS-SD services.
Then I have to pick one of those discovered services and make some http requests over https or other secure method.
I already did this trough WiFi, but for some reasons there's a need to do the same trough Bluetooth.
I did some research yesterday but I haven't found what I need. I'm not sure how to approach this.
Any input on this is welcomed.
Thanks!
Most implementations of bluetooth implement sockets on top of RFCOMM or L2CAP, but these are not standard sockets.
I've never used it, but I believe that the Bluetooth network encapsulation protocol (BNEP) allows for the use of standard TCP/IP networking over Bluetooth. Android does not implement this and I don't think it is widely supported elsewhere.

wifi chat between two or more android devices

I want to develop an chat application to chat between two or more android devices using wifi network.The application should be able to send or receive strings from each other. I have an experience of using Bluetooth network between pc and android. Can any give me any suggestion or right direction. Thanks in advance.
You can be able to chat between two android devices in Android Version 4.0.There is an API called 'Wifi P2P'.More information can be had from here:
http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html
If you are thinking about connecting devices that are under the same WiFi AP/router and without setting up any server, then I would suggest you to consider using UDP multicast which has been available since API level 1:
http://developer.android.com/reference/java/net/MulticastSocket.html
UDP does not guarantee data delivery (could be lost), so I would use UDP multicast for device discovery, and open up a TCP connection for data that requires guaranteed delivery. TCP listening port can be advertised via UDP multicast so that everyone can connect with each other over TCP. (There may be 3rd party tool that does this low level stuff for you.)
maybe XMPP will help, google talk uses the same protocol.
The Extensible Messaging and Presence Protocol (XMPP) is an open
technology for real-time communication, which powers a wide range of
applications including instant messaging, presence, multi-party chat,
voice and video calls, collaboration, lightweight middleware, content
syndication, and generalized routing of XML data.
things you need to do:
1. you need a server computer to run XMPP server, there are lots of open source implementations.
2. you need a XMPP client running on your android device, there are also lots of open source implementations running on android platform.

Android location aware chat application

I need help with this kind of application.I don't know how will two devices communicate over internet. I think I should create database to store users information, and then create sockets for communication. Is there a better way to do this?
Most mobile networks use some form of NAT, so inbound connections to mobile devices are not possible. You'll need an intermediate server. P2P is possible, but to setup a connection you still need a server.
Don't reinvent the wheel: there is already a lot of servers/libraries/protocols that do chat. Just pick one. Most notable one is XMPP, with open protocol, multiple free OSS server implementations and libraries (including Android).
Additionally you can create your own server app, that does login/authentication and location-aware stuff. Once two users are authenticated and location-paired, you can hand-off the chat part to XMPP server.

Categories