Question

I am developing board game in android. I want to make this game playable on two separate devices, for that I need to use WiFi-Direct. I want to know is there any library available which will help me to

  • Find and connect with device
  • Send and receive board coordinates between two devices after touch-listener event

I am interested in built-in library. OR If possible please share implemented example of client/server architecture.

Was it helpful?

Solution

Make the device that starts the game run as a TCP server and make it broadcast on the network and listen on a predetermined port. When another player wants to join, he just selects the server from a menu and join the game. The coordinates can be sent as packets on touch events.

This is for the server:

Thread serverThread = new Thread(new Runnable() {

    @Override
    public void run() {                                 
    
    try { 
         serverSocketTCP = new ServerSocket(); 
         serverSocketTCP.setReuseAddress(true); 
         serverSocketTCP.bind(new InetSocketAddress(YourPort));

   while (status) {
        clientSocketTCP = serverSocketTCP.accept();

        BufferedReader bufferedReader = new BufferedReader(new 
        InputStreamReader(client.getInputStream()));

        OutputStream outputStream = client.getOutputStream();



     }
  } catch (Exception e) {
     e.printStackTrace();
                                    
   }
    });
serverThread.start();

This is for the client:

Socket  clientSocket = new Socket(ServerIP,ServerPort);
outputStream = clientSocket.getOutputStream();
bufferedReader=newBufferedReader(new 
InputStreamReader(clientSocket.getInputStream()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top