Package server.connection

Source Code of server.connection.ClientConnection

package server.connection;

import general.helperclasses.logger.Logger;
import general.helperclasses.logger.LoggingLevel;
import general.messages.StringMessage;
import general.messages.UpdateUnitMessage;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.lwjgl.util.vector.Vector3f;

public class ClientConnection extends Thread{

  private final Socket client;
  private final ServerSocket server;
  private final ObjectOutputStream outstream;
 
  public ClientConnection(ServerSocket server, Socket client) throws IOException {
    this.server = server;
    this.client = client;
   
    try {
      this.outstream = new ObjectOutputStream(client.getOutputStream());
    } catch (IOException e) {
      Logger.addEntry(LoggingLevel.SERVE, "Something went wrong while creating the ObjectOutputStream\n"+e.toString());
      client.close();
      throw e;
    }
  }
 
  @Override
  public void run() {
    System.out.println("running client-thread");
    StringMessage welcomeMessage = new StringMessage("Willkommen auf dem Server!");
    UpdateUnitMessage updateMessage = new UpdateUnitMessage(4, new Vector3f(3.4f, 1.3f, 8.3f), new Vector3f(3.5f, 1.8f, 3.2f), 4.6f);
   
    try {
      System.out.println("Sending welcome Message");
      outstream.writeObject(welcomeMessage);
      outstream.flush();
     
      outstream.writeObject(updateMessage);
      outstream.flush();
    } catch (IOException e) {
      Logger.addEntry(LoggingLevel.WARN, "Could not send welcomeMessage");
    }
   
    while(!client.isClosed())
    {
      try {
        //Benchmarktest
       
        UpdateUnitMessage update = new UpdateUnitMessage((int)(Math.random()*100), new Vector3f((float)(Math.random()*100), (float)(Math.random()*100), (float)(Math.random()*100)), new Vector3f((float)(Math.random()*100), (float)(Math.random()*100), (float)(Math.random()*100)), (float)Math.random()*100);
        outstream.writeObject(update);
        outstream.flush();
        //Thread.sleep(1);
      //} catch (InterruptedException e) {
        // TODO Auto-generated catch block
      //  e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    //Hier sp�ter loop
    /*
    try {
      outstream.close();
    } catch (IOException e) {
      Logger.addEntry(LoggingLevel.WARN, "Could not close ObjectOutputStream:\n"+e.toString());
    }
    try {
      client.close();
    } catch (IOException e) {
      Logger.addEntry(LoggingLevel.WARN, "Could not close ClientSocket:\n"+e.toString());
    }
    */
  }
 
}
 
TOP

Related Classes of server.connection.ClientConnection

TOP
Copyright © 2018 www.massapi.com. 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.