Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Spiel;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Scanner;
- @SuppressWarnings("static-access")
- public class Connection {
- public DrawArea DA;
- public MyPanelRater DAR;
- private Connection con;
- private static Console console = new Console();
- private static boolean aktive;
- /*
- * Client
- */
- public boolean clientConnection(String ip,int port) {
- aktive = true;
- try {
- ConnectToServer(ip, port);
- setupStreams();
- whileChatting();
- } catch (Exception e) {
- }
- return true;
- }
- private static Thread thread;
- private static Thread thread1;
- private static Thread thread2;
- private static Socket connenction;
- private static ObjectOutputStream output;
- private static ObjectInputStream input;
- private static Object message;
- private void ConnectToServer(String serverip,int Port) throws IOException {
- console.addTextConsole("Versuche verbindung zu "+serverip+":"+""+" verbinden!");
- connenction = new Socket(InetAddress.getByName(serverip), Port);
- console.addTextConsole("Verbunden zu: "+connenction.getInetAddress().getHostName());
- }
- private void setupStreams() throws IOException{
- console.addTextConsole("Baue Verbindungsbrücken auf");
- output = new ObjectOutputStream(connenction.getOutputStream());
- output.flush();
- input = new ObjectInputStream(connenction.getInputStream());
- }
- private void whileChatting() throws IOException{
- console.addTextConsole("Verbunden");
- try {thread.interrupt();} catch (Exception e) {}
- thread=null;
- thread = new Thread(){
- @SuppressWarnings("unchecked")
- public void run(){
- do{
- try {
- if(aktive==false){
- this.interrupt();
- }
- message = input.readObject();
- reciveListener(message);
- try {
- String s = (String) message;
- if(s.equals("killconnection")){
- killConnection();
- this.interrupt();
- }else if(s.equals("clear")){
- DAR.clear();
- }else{
- ArrayList<Integer> ar2 = new ArrayList<>();
- Scanner sc = new Scanner(s);
- while(sc.hasNextLine()){
- String str = sc.nextLine();
- Scanner sca = new Scanner(str);
- while(sca.hasNext()){
- ar2.add(Integer.parseInt(sca.next()));
- }
- sca.close();
- try {DAR.drawLine(ar2.get(0), ar2.get(1), ar2.get(2), ar2.get(3), ar2.get(4), ar2.get(5), ar2.get(6), ar2.get(7));} catch (Exception e2) {e2.printStackTrace();}
- ar2.clear();
- }
- sc.close();
- }
- } catch (Exception e) {
- }
- } catch (Exception e) {}
- }while(true);
- }
- };
- thread.start();
- }
- public boolean Hostcon(String Ip, int Port) {
- try {
- aktive = true;
- serverstartRunning(Port);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //on sucsess
- return true;
- }
- /*
- * Server
- */
- private ServerSocket server;
- private static ObjectOutputStream serveroutput;
- private static ObjectInputStream serverinput;
- private static Socket serverconnection;
- private boolean connected;
- private boolean serverstartRunning(int port) throws IOException{
- connected=false;
- try {thread1.interrupt();} catch (Exception e) {}
- thread1=null;
- thread1 = new Thread(){
- public void run(){
- try {
- try {
- server = new ServerSocket(port,100);
- } catch (IOException e1) {}
- while(connected==false){
- try {
- if(aktive==false){
- this.interrupt();
- }
- serverwaitForConnection();
- serversetupStreams();
- console.addTextConsole("Verbindung hergestellt");
- serverwhileChatting();
- this.interrupt();
- } catch (Exception e) {}
- }
- } catch (Exception e) {
- console.addTextConsole("Der Server hat verbindung getrennt");
- this.interrupt();
- }finally{
- this.interrupt();
- }
- }
- };
- thread1.start();
- console.addTextConsole("Server gestartet!");
- return true;
- }
- private void serverwaitForConnection() throws IOException{
- serverconnection = server.accept();
- connected = true;
- }
- private void serversetupStreams() throws IOException{
- console.addTextConsole("Baue Verbindungsbrücken auf!");
- serveroutput = new ObjectOutputStream(serverconnection.getOutputStream());
- serveroutput.flush();
- serverinput = new ObjectInputStream(serverconnection.getInputStream());
- }
- private void serverwhileChatting() throws IOException{
- try {thread2.interrupt();} catch (Exception e) {}
- thread2=null;
- thread2 = new Thread(){
- @SuppressWarnings("unchecked")
- public void run(){
- do{
- try {
- if(aktive==false){
- this.interrupt();
- }
- message = serverinput.readObject();
- reciveListener(message);
- try {
- String s = (String) message;
- if(s.equals("killconnection")){
- killConnection();
- this.interrupt();
- }else if(s.equals("clear")){
- DAR.clear();
- }else{
- ArrayList<Integer> ar2 = new ArrayList<>();
- Scanner sc = new Scanner(s);
- while(sc.hasNextLine()){
- String str = sc.nextLine();
- Scanner sca = new Scanner(str);
- while(sca.hasNext()){
- ar2.add(Integer.parseInt(sca.next()));
- }
- sca.close();
- try {DAR.drawLine(ar2.get(0), ar2.get(1), ar2.get(2), ar2.get(3), ar2.get(4), ar2.get(5), ar2.get(6), ar2.get(7));} catch (Exception e2) {e2.printStackTrace();}
- ar2.clear();
- }
- sc.close();
- }
- } catch (Exception e) {
- }
- } catch (Exception e) {}
- }while(true);
- }
- };
- thread2.start();
- }
- public static void serversendMessage(Object messagem){
- if(messagem==null){
- return;
- }
- if(serveroutput==null&&output==null){
- console.addTextConsole("Keine verbindung");
- }
- try {
- if(serveroutput!=null){
- try {} catch (Exception e) {}
- serveroutput.writeObject(messagem);
- serveroutput.flush();
- }
- } catch (Exception e) {
- e.printStackTrace();
- console.addTextConsole("Verbindungs Error!");
- }
- try {
- if(output!=null){
- try {} catch (Exception e) {}
- if(output!=null){
- output.writeObject(messagem);
- output.flush();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- console.addTextConsole("Verbindungs Error!");
- }
- }
- public Connection getConnection() {
- return con;
- }
- private void reciveListener(Object message){
- //console.addTextConsole("Ein Datensatz wurde empfangen: "+message);
- }
- public boolean killConnection(){
- String kill = "killconnection";
- message = (Object) kill;
- try {serversendMessage(message);} catch (Exception e) {}
- aktive = false;
- console.addTextConsole("Alte Verbindung Geschlossen");
- try {thread.interrupt();} catch (Exception e) {}
- try {thread1.interrupt();} catch (Exception e) {}
- try {thread2.interrupt();} catch (Exception e) {}
- try {thread=null;} catch (Exception e) {}
- try {thread1=null;} catch (Exception e) {}
- try {thread2=null;} catch (Exception e) {}
- try {server.close();server = null;} catch (Exception e) {}
- try {serverconnection.close();serverconnection = null;} catch (Exception e) {}
- try {serverinput.close();serverinput=null;} catch (Exception e) {}
- try {serveroutput.close();serveroutput=null;} catch (Exception e) {}
- try {output.close();output=null;} catch (Exception e) {}
- try {input.close();input = null;} catch (Exception e) {}
- try {connenction.close();connenction=null;} catch (Exception e) {}
- try {message=null;} catch (Exception e) {}
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement