Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Engine;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Multiplayer
- {
- public static boolean IsConnected = false;
- public static void InvertIfConnected()
- {
- if (IsConnected = false)
- {
- IsConnected = true;
- }
- else if(IsConnected = true)
- {
- IsConnected = false;
- }
- }
- public static class Client
- {
- public Multiplayer.DataTypes DataType;
- public byte OutputData;
- public byte InputData;
- public Thread Ping;
- public Object PingData;
- public static Socket Client;
- public static DataOutputStream Output;
- public static DataInputStream Input;
- public void Connect(String IP, int Port)
- {
- try{
- this.Client = new Socket( IP , Port);
- this.Output = new DataOutputStream(Client.getOutputStream());
- this.Input = new DataInputStream(Client.getInputStream());
- } catch (Exception e)
- {
- System.out.println(e);
- }
- }
- public static void Disconnect()
- {
- Game.SavePlayer();
- try {
- Output.close();
- Input.close();
- Client.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void UpdateServer(Multiplayer.DataTypes Type)
- {
- switch (Type)
- {
- case Player:
- Object Player = Game.GetPlayer();
- // this.Output.writeExternal(Player);
- /* try {
- // this.Output.writeByte(OutputData);
- } catch (IOException e) {
- e.printStackTrace();
- }
- */
- break;
- case ModifyTile:
- break;
- case PingServer:
- break;
- }
- }
- public byte GetInfo()
- {
- byte Data = 0;
- try {
- Data = this.Input.readByte();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return Data;
- }
- public void FlushData()
- {
- try {
- this.Output.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void run()
- {
- while (true)
- {
- UpdateServer(Multiplayer.DataTypes.PingServer);
- try {
- Ping.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public Client()
- {
- this.PingData = new ClientInfo();
- }
- }
- public class Server
- {
- public byte OutputData;
- public byte InputData;
- Object[] Players;
- public ServerSocket Server;
- public DataOutputStream Output;
- public DataInputStream Input;
- public Socket Client;
- public int MaxPlayers;
- public void CreateServer(int Port)
- {
- try{
- this.Server = new ServerSocket(Port);
- this.Output = new DataOutputStream(Client.getOutputStream());
- this.Input = new DataInputStream(Client.getInputStream());
- } catch (Exception e)
- {
- System.out.println(e);
- }
- }
- public void Disconnect()
- {
- try {
- this.Output.close();
- this.Input.close();
- this.Server.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void PingBack()
- {
- }
- public byte GetInfo()
- {
- byte Data = 0;
- try {
- Data = this.Input.readByte();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return Data;
- }
- public void SendInfo(byte OutputData)
- {
- try {
- this.Output.writeByte(OutputData);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void FlushData()
- {
- try {
- this.Output.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void AcceptClient()
- {
- try {
- this.Client = Server.accept();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public Server(int MaxNumberOfPlayers)
- {
- Players = new Object[MaxNumberOfPlayers];
- }
- }
- public enum DataTypes
- {
- Player, ModifyTile, PingServer, PingClient
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement