Advertisement
TrodelHD

Untitled

Mar 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. package Spiel;
  2.  
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.net.InetAddress;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12. @SuppressWarnings("static-access")
  13. public class Connection {
  14.  
  15. public DrawArea DA;
  16. public MyPanelRater DAR;
  17.  
  18. private Connection con;
  19. private static Console console = new Console();
  20. private static boolean aktive;
  21.  
  22.  
  23. /*
  24. * Client
  25. */
  26. public boolean clientConnection(String ip,int port) {
  27. aktive = true;
  28. try {
  29. ConnectToServer(ip, port);
  30. setupStreams();
  31. whileChatting();
  32. } catch (Exception e) {
  33.  
  34. }
  35.  
  36. return true;
  37. }
  38.  
  39. private static Thread thread;
  40. private static Thread thread1;
  41. private static Thread thread2;
  42. private static Socket connenction;
  43. private static ObjectOutputStream output;
  44. private static ObjectInputStream input;
  45. private static Object message;
  46.  
  47.  
  48. private void ConnectToServer(String serverip,int Port) throws IOException {
  49. console.addTextConsole("Versuche verbindung zu "+serverip+":"+""+" verbinden!");
  50. connenction = new Socket(InetAddress.getByName(serverip), Port);
  51. console.addTextConsole("Verbunden zu: "+connenction.getInetAddress().getHostName());
  52. }
  53. private void setupStreams() throws IOException{
  54. console.addTextConsole("Baue Verbindungsbrücken auf");
  55. output = new ObjectOutputStream(connenction.getOutputStream());
  56. output.flush();
  57. input = new ObjectInputStream(connenction.getInputStream());
  58. }
  59. private void whileChatting() throws IOException{
  60. console.addTextConsole("Verbunden");
  61. try {thread.interrupt();} catch (Exception e) {}
  62. thread=null;
  63. thread = new Thread(){
  64. @SuppressWarnings("unchecked")
  65. public void run(){
  66. do{
  67. try {
  68. if(aktive==false){
  69. this.interrupt();
  70. }
  71.  
  72.  
  73. message = input.readObject();
  74.  
  75.  
  76. reciveListener(message);
  77. try {
  78. String s = (String) message;
  79. if(s.equals("killconnection")){
  80. killConnection();
  81. this.interrupt();
  82. }else if(s.equals("clear")){
  83. DAR.clear();
  84. }else{
  85. ArrayList<Integer> ar2 = new ArrayList<>();
  86. Scanner sc = new Scanner(s);
  87. while(sc.hasNextLine()){
  88. String str = sc.nextLine();
  89. Scanner sca = new Scanner(str);
  90. while(sca.hasNext()){
  91. ar2.add(Integer.parseInt(sca.next()));
  92. }
  93. sca.close();
  94. 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();}
  95. ar2.clear();
  96. }
  97.  
  98. sc.close();
  99. }
  100. } catch (Exception e) {
  101. }
  102. } catch (Exception e) {}
  103. }while(true);
  104. }
  105. };
  106. thread.start();
  107. }
  108.  
  109.  
  110. public boolean Hostcon(String Ip, int Port) {
  111. try {
  112. aktive = true;
  113. serverstartRunning(Port);
  114. } catch (IOException e) {
  115. // TODO Auto-generated catch block
  116. e.printStackTrace();
  117. }
  118. //on sucsess
  119. return true;
  120. }
  121. /*
  122. * Server
  123. */
  124. private ServerSocket server;
  125. private static ObjectOutputStream serveroutput;
  126. private static ObjectInputStream serverinput;
  127. private static Socket serverconnection;
  128. private boolean connected;
  129.  
  130. private boolean serverstartRunning(int port) throws IOException{
  131. connected=false;
  132. try {thread1.interrupt();} catch (Exception e) {}
  133. thread1=null;
  134. thread1 = new Thread(){
  135. public void run(){
  136. try {
  137. try {
  138. server = new ServerSocket(port,100);
  139. } catch (IOException e1) {}
  140. while(connected==false){
  141. try {
  142. if(aktive==false){
  143. this.interrupt();
  144. }
  145. serverwaitForConnection();
  146. serversetupStreams();
  147. console.addTextConsole("Verbindung hergestellt");
  148. serverwhileChatting();
  149. this.interrupt();
  150. } catch (Exception e) {}
  151. }
  152. } catch (Exception e) {
  153.  
  154. console.addTextConsole("Der Server hat verbindung getrennt");
  155. this.interrupt();
  156. }finally{
  157. this.interrupt();
  158. }
  159. }
  160. };
  161. thread1.start();
  162. console.addTextConsole("Server gestartet!");
  163. return true;
  164.  
  165.  
  166. }
  167.  
  168. private void serverwaitForConnection() throws IOException{
  169.  
  170. serverconnection = server.accept();
  171. connected = true;
  172.  
  173. }
  174. private void serversetupStreams() throws IOException{
  175. console.addTextConsole("Baue Verbindungsbrücken auf!");
  176. serveroutput = new ObjectOutputStream(serverconnection.getOutputStream());
  177. serveroutput.flush();
  178. serverinput = new ObjectInputStream(serverconnection.getInputStream());
  179.  
  180. }
  181.  
  182. private void serverwhileChatting() throws IOException{
  183. try {thread2.interrupt();} catch (Exception e) {}
  184. thread2=null;
  185. thread2 = new Thread(){
  186. @SuppressWarnings("unchecked")
  187. public void run(){
  188. do{
  189. try {
  190. if(aktive==false){
  191. this.interrupt();
  192. }
  193.  
  194.  
  195. message = serverinput.readObject();
  196.  
  197.  
  198. reciveListener(message);
  199. try {
  200. String s = (String) message;
  201. if(s.equals("killconnection")){
  202. killConnection();
  203. this.interrupt();
  204. }else if(s.equals("clear")){
  205. DAR.clear();
  206. }else{
  207. ArrayList<Integer> ar2 = new ArrayList<>();
  208. Scanner sc = new Scanner(s);
  209. while(sc.hasNextLine()){
  210. String str = sc.nextLine();
  211. Scanner sca = new Scanner(str);
  212. while(sca.hasNext()){
  213. ar2.add(Integer.parseInt(sca.next()));
  214. }
  215. sca.close();
  216. 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();}
  217. ar2.clear();
  218. }
  219.  
  220. sc.close();
  221. }
  222. } catch (Exception e) {
  223. }
  224. } catch (Exception e) {}
  225. }while(true);
  226. }
  227. };
  228. thread2.start();
  229.  
  230. }
  231. public static void serversendMessage(Object messagem){
  232. if(messagem==null){
  233. return;
  234. }
  235. if(serveroutput==null&&output==null){
  236. console.addTextConsole("Keine verbindung");
  237. }
  238. try {
  239. if(serveroutput!=null){
  240. try {} catch (Exception e) {}
  241. serveroutput.writeObject(messagem);
  242. serveroutput.flush();
  243. }
  244. } catch (Exception e) {
  245. e.printStackTrace();
  246. console.addTextConsole("Verbindungs Error!");
  247. }
  248. try {
  249. if(output!=null){
  250. try {} catch (Exception e) {}
  251. if(output!=null){
  252. output.writeObject(messagem);
  253. output.flush();
  254. }
  255. }
  256. } catch (Exception e) {
  257. e.printStackTrace();
  258. console.addTextConsole("Verbindungs Error!");
  259. }
  260. }
  261.  
  262.  
  263. public Connection getConnection() {
  264.  
  265. return con;
  266.  
  267. }
  268.  
  269. private void reciveListener(Object message){
  270. //console.addTextConsole("Ein Datensatz wurde empfangen: "+message);
  271. }
  272. public boolean killConnection(){
  273. String kill = "killconnection";
  274. message = (Object) kill;
  275. try {serversendMessage(message);} catch (Exception e) {}
  276. aktive = false;
  277. console.addTextConsole("Alte Verbindung Geschlossen");
  278. try {thread.interrupt();} catch (Exception e) {}
  279. try {thread1.interrupt();} catch (Exception e) {}
  280. try {thread2.interrupt();} catch (Exception e) {}
  281. try {thread=null;} catch (Exception e) {}
  282. try {thread1=null;} catch (Exception e) {}
  283. try {thread2=null;} catch (Exception e) {}
  284. try {server.close();server = null;} catch (Exception e) {}
  285. try {serverconnection.close();serverconnection = null;} catch (Exception e) {}
  286. try {serverinput.close();serverinput=null;} catch (Exception e) {}
  287. try {serveroutput.close();serveroutput=null;} catch (Exception e) {}
  288. try {output.close();output=null;} catch (Exception e) {}
  289. try {input.close();input = null;} catch (Exception e) {}
  290. try {connenction.close();connenction=null;} catch (Exception e) {}
  291. try {message=null;} catch (Exception e) {}
  292.  
  293. return true;
  294. }
  295.  
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement