How to create a device network?

ondesic

Active Member
Licensed User
Longtime User
I have a text based game that shows the stats of a player. I want one device to be the host and the other devices to be clients through bluetooth or WiFi.

When a change is made to a player's stats on any device, I want it to update to all connected devices.

Do we have a library that would allow this kind of device to device network?
 

Jim

Member
Licensed User
Longtime User
Take a look at the network library. Socket and ServerSocket will be able todo everything you need for WiFi connectivity.
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
I looked at that example, but it only showed connecting to a desktop. Maybe I missed how to connect two phones.
 
Upvote 0

Jim

Member
Licensed User
Longtime User
I'm just talking about the library in general which can be found at: Basic4android - Network

... I just looked at this: http://www.b4x.com/forum/basic4android-getting-started-tutorials/7001-android-network-tutorial.html

.... which I think is what you're talking about. The examples provide both a client and server applications. While one example is a device connecting to a desktop and the other example is a desktop connecting to a device, it doesn't matter what you're actually connecting to. Your application doesn't care whats on the other end. If its an IP to a desktop so be it, if the ip takes you to another phone, ok cool. The point is, sockets can connect to anything. Its up to your to send data and interpret the incoming data. You have to implement both sides yourself, in your application. If you choose the client/server model then one phone needs to open a ServerSocket.Listen just like the example, and the other phone needs to:
B4X:
Socket.Connect("ip_of_server_phone", port, timeout)

Once the connection is successful and established, you can create a stream to pass data. Then each phone will send its stats and the other phone will receive, interpret, and do whatever it is you need todo with said data.

Take a look at this tutorial on AsynchStreams, an object used to handle data streams. This should definitely get you headed in the right direction:

http://www.b4x.com/forum/basic4android-getting-started-tutorials/7669-asyncstreams-tutorial.html
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
It seems that the server could poll each connected device for a change in data status and is so, read the new data from the polled phone and stow that away in a database or file. While polling, the server could tell each connected phone that new data is available and then send the updated data to each polled device. each phone would either have new data or want an update of community data. Since all of this would happen so quickly, all phones would simultaneously be updated. I used to work in POS where we would poll 32 POS terminals in a store at 1000x a second. they either had a keystroke or were sent a character to display on their screens. Similiar concept here. It might even be possible to use ONE phone as the "server" for the local network. This would save on a PC. Think about that.
thx,
Ray
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
forgive my stupidity, but what would you use in the IP parameter for a phone? It's a string. Is there an IP to your phone or could you put in a phrase like "MyServer", then have the other phones look for this? This is the one part I don't understand.
 
Upvote 0

Jim

Member
Licensed User
Longtime User
The following code will set a label (lbl_myip) to display your phone's IP address. I recommend first connecting to your WLAN, otherwise you're going to get your 3G IP and service providers may or may not allow connections.

B4X:
ServerSocket1.Initialize(4000,"ServerSocket1")
myip = ServerSocket1.GetMyIP
ServerSocket1.Close
lbl_myip.text = "My IP is " & myip


But, yes, any network connected device will have an ip address. In a client/server model, you really only have to worry about clients connecting to the server ip. ex: phone1 decides to host a game, you click a "host" button and it starts the server(serversocket.listen etc etc), and tells the user his IP address. the user then tells all his friends his IP address. phone2 and phone3 type in the ip of phone1, and click a "join" button (socket1.connect etc etc) the _connected event is raised and successful, you initialize a stream and begin passing data. It isn't that bad actually.

If your game is slow paced, and dependent on reliably transferring data, I defintely recommend you use a prefixed AsynchStreams to pass your input and output stream data.
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
Thank you Jim. I've got a connection now. I am sooo close.

I followed the Async tutorial, but don't seem to be able to send data. I have a button with this code:

B4X:
If AStreams.IsInitialized = False Then 
   Return
Else
        Dim buffer() As Byte
        buffer = "Go Team".GetBytes("UTF8")
        AStreams.Write(buffer)    
End If

Does this raise the NewData sub, or do I have to do something else?
 
Upvote 0

Jim

Member
Licensed User
Longtime User
That code is correct, and when received it will raise a _NewData(Buffer() As Byte) event on the client side of the stream. Do you have both sides implemented? ex: when serversocket is connected it initlizes a asynchstream and when the client socket is connected it also opens a stream? Basically, you're doing both sides of the client server model in the same app, just which part is run depends on the user's input.
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
Jim, or anyone,

Do you know if you can use ASyncStream to send an images from one device to another?
 
Upvote 0
Top