Package com.golden.gamedev.engine.network.tcp

Examples of com.golden.gamedev.engine.network.tcp.TCPClient


  /** ***************************** CONSTRUCTOR ******************************* */
  /** ************************************************************************* */
 
  public static void main(String[] args) {
    // connect to server
    TCPClient client = null;
   
    try {
      client = new TCPClient("localhost", 11137); // connect to
                            // localhost:11137
     
    }
    catch (IOException ex) {
      ex.printStackTrace();
      System.err.println("Client initialization failed.\nCaused by:\n"
              + ex.getMessage());
      System.exit(-1);
    }
   
    System.out.println("Client Detail: " + client.getDetail());
   
    // network loop
    int i = 0;
    while (true) {
      // update client
      try {
        client.update(100);
       
      }
      catch (IOException ex) {
        ex.printStackTrace();
        System.err.println("Client disconnected.\nCaused by:\n"
                + ex.getMessage());
        System.exit(-1);
      }
     
      // handle received messages
      NetworkPacket[] packets = client.getReceivedPackets();
      for (int j = 0; j < packets.length; j++) {
        NetworkMessage packet = (NetworkMessage) packets[j];
       
        System.out.println(client.getRemoteDetail() + ": "
                + packet.getMessage());
      }
     
      // send message
      // every 20 tick
      if (i++ % 20 == 0 && i < 100) {
        try {
          System.out.println("sending packet to server");
          client.sendPacket(new NetworkMessage(
                  "Client said Yes or No"));
         
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
       
        // disconnect from server
        // after 200 tick
      }
      else if (i > 200) {
        try {
          client.disconnect();
         
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
View Full Code Here


      ex.printStackTrace();
      System.exit(-1);
    }
   
    // create client and connet to server
    TCPClient client = null;
    try {
      client = new TCPClient(host, portNumber);
     
    }
    catch (IOException ex) {
      ex.printStackTrace();
     
      JOptionPane.showMessageDialog(null, "Connection Failed.\n"
              + "Caused by:\n" + ex.getMessage(), "Connection Failed",
              JOptionPane.ERROR_MESSAGE);
     
      System.exit(-1);
    }
   
    // our first network packet: nick name
    String nick = JOptionPane.showInputDialog("Enter Your Nick Name:");
    if (nick == null) {
      // cancel
      client.silentDisconnect();
      System.exit(0);
    }
   
    // send the client nick name
    try {
      client.sendPacket(new NetworkMessage(nick));
    }
    catch (IOException ex) {
      ex.printStackTrace();
     
      JOptionPane.showMessageDialog(null, "Connection Failed.\n"
View Full Code Here

    // we need to register our custom packet first
    NetworkConfig.registerPacket((short) 1, AllFieldTypePacket.class);
    NetworkConfig.DEBUG = true;
   
    try {
      BaseClient bsClient = new TCPClient(host, port); // create client
                                // and connect
                                // to server
     
      int sendCounter = 0;
     
      System.out.println("Start the Client...");
     
      // network loop
      while (true) {
        bsClient.update(100); // the main work
       
        // send packet to server
        // every 50 tick
        if (++sendCounter % 50 == 0) {
          bsClient.sendPacket(new AllFieldTypePacket());
        }
       
        // disconnect from server
        // after 200 tick
        if (sendCounter > 200) {
          bsClient.silentDisconnect();
          break;
        }
       
        try {
          Thread.sleep(100);
View Full Code Here

TOP

Related Classes of com.golden.gamedev.engine.network.tcp.TCPClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.