Android Open Source - TreeFrogEngine Network Game Controller






From Project

Back to project page TreeFrogEngine.

License

The source code is released under:

MIT License

If you think the Android project TreeFrogEngine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.discretesoftworks.networking;
/*from  w  w w.  ja  v  a2 s  .  co m*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.discretesoftworks.framework.GameController;

// Class for adding online communications.
public abstract class NetworkGameController extends GameController{

  public static Writer writer;
  
  public static final String hostname = "computer1forus.no-ip.org";
  
  public boolean initializeNetwork(){
    writer = new Writer();
    return true;
  }
  
  public boolean connect(){
    if (writer == null){
      if (!initializeNetwork())
        return false;
    }
        int port = 7497;
        try {
          System.out.println("Connecting...");
            Socket socket = new Socket(hostname, port);
            System.out.println("\nConnection successful thru "+hostname+":"+port);
          socket.setTcpNoDelay(true);
            System.out.println("TCP No Delay: "+socket.getTcpNoDelay());
            writer.setStreams(new BufferedOutputStream(socket.getOutputStream()),
                      new BufferedInputStream(socket.getInputStream()));
            return true;
        } catch (UnknownHostException e) {
            System.err.println("Unknown host: "+hostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Could not connect to '"+hostname+"'");
            System.exit(2);
        }
        return false;
  }
  
  public void listen(){
    try{
      while (writer.getInputStream().available() > 0){
//        System.out.print("In! Buffer size: "+in.available());
        userDefined(writer.getInputStream());
      }
    } catch (IOException ioe){
      System.err.println("Connection lost.");
      System.exit(1);
    }
  }
  
  @Override
  public void update(float deltaTime){
    if (writer != null)
      listen();
    super.update(deltaTime);
  }
  
  // Implement this method if you want to do networking.
  public void userDefined(InputStream in){
    try{
      in.skip(in.available());
    } catch (Exception e){
      e.printStackTrace();
    }
  }
  
}




Java Source Code List

com.discretesoftworks.OUYAframework.OuyaGameController.java
com.discretesoftworks.OUYAframework.OuyaGame.java
com.discretesoftworks.TestGame.Floor.java
com.discretesoftworks.TestGame.Player.java
com.discretesoftworks.TestGame.Pointer.java
com.discretesoftworks.TestGame.TestController.java
com.discretesoftworks.TestGame.TestGame.java
com.discretesoftworks.framework.AndroidGame.java
com.discretesoftworks.framework.Assets.java
com.discretesoftworks.framework.Directional.java
com.discretesoftworks.framework.GameController.java
com.discretesoftworks.framework.GameFont.java
com.discretesoftworks.framework.GameObject.java
com.discretesoftworks.framework.GameRenderer.java
com.discretesoftworks.framework.GriddedObject.java
com.discretesoftworks.framework.ModelLoader.java
com.discretesoftworks.framework.MovingObject.java
com.discretesoftworks.framework.MyGLRenderer.java
com.discretesoftworks.framework.MyGLSurfaceView.java
com.discretesoftworks.framework.NumericalMatrix.java
com.discretesoftworks.framework.Pair.java
com.discretesoftworks.framework.Path.java
com.discretesoftworks.framework.Pool.java
com.discretesoftworks.framework.RaggedDictionary.java
com.discretesoftworks.framework.RenderModel.java
com.discretesoftworks.framework.Sprite.java
com.discretesoftworks.framework.Terrain.java
com.discretesoftworks.framework.TextDisplay.java
com.discretesoftworks.framework.TextHolder.java
com.discretesoftworks.framework.View.java
com.discretesoftworks.networking.NetworkGameController.java
com.discretesoftworks.networking.Writer.java