Advertisement
TrodelHD

Untitled

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