Back to project page TreeFrogEngine.
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.
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(); } } }