You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Transmitter.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package model;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.io.PrintWriter;
  14. import java.net.ServerSocket;
  15. import java.net.Socket;
  16. import java.util.Observable;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import logger.OhmLogger;
  20. /**
  21. *
  22. * @author Apollo
  23. */
  24. public class Transmitter extends Observable implements Runnable
  25. {
  26. private static final Logger lg = OhmLogger.getLogger();
  27. private static final int PORT = 35000;
  28. private static final String IP_ADRESSE = "127.0.0.1";
  29. int modus;
  30. private String nachricht;
  31. String message;
  32. OutputStream oStream;
  33. InputStream iStream;
  34. InputStreamReader isr;
  35. OutputStreamWriter osr;
  36. BufferedReader in;
  37. PrintWriter out;
  38. Socket s;
  39. public Transmitter(int modus)
  40. {
  41. this.modus = modus;
  42. nachricht = "";
  43. }
  44. @Override
  45. public void run()
  46. {
  47. if (modus == 0)
  48. {
  49. ServerSocket sSocket;
  50. try
  51. {
  52. sSocket = new ServerSocket(PORT);
  53. lg.info("Server: Warte auf Verbindung ...");
  54. s = sSocket.accept(); // Achtung: blockiert!
  55. lg.info("Server: Verbindung akzeptiert");
  56. }
  57. catch (IOException ex)
  58. {
  59. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  60. }
  61. }
  62. else if (modus == 1)
  63. {
  64. lg.info("Client: verbinde ...");
  65. try
  66. {
  67. s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
  68. lg.info("Client: Verbindung hergestellt");
  69. }
  70. catch (IOException ex)
  71. {
  72. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  73. }
  74. }
  75. try
  76. {
  77. iStream = s.getInputStream();
  78. oStream = s.getOutputStream();
  79. isr = new InputStreamReader(iStream, "UTF-8");
  80. osr = new OutputStreamWriter(oStream, "UTF-8");
  81. in = new BufferedReader(isr);
  82. //BufferedWriter out = new BufferedWriter(osr);
  83. out = new PrintWriter(osr);
  84. }
  85. catch (IOException ex)
  86. {
  87. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  88. }
  89. if (modus == 0)
  90. {
  91. lg.info("Server: Stream initialisiert");
  92. }
  93. else if (modus == 1)
  94. {
  95. lg.info("Client: Stream initialisiert");
  96. }
  97. while (true)
  98. {
  99. try
  100. {
  101. nachricht = in.readLine(); // Achtung blockiert
  102. }
  103. catch (IOException ex)
  104. {
  105. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  106. }
  107. setChanged();
  108. notifyObservers();
  109. lg.info("Server: Nachricht empfangen");
  110. out.flush(); // wirklich absenden!!
  111. if (nachricht.equals("beenden"))
  112. {
  113. break;
  114. }
  115. }
  116. try
  117. {
  118. in.close();
  119. out.close();
  120. }
  121. catch (IOException ex)
  122. {
  123. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  124. }
  125. }
  126. public void send(String input)
  127. {
  128. out.println(input);
  129. out.flush(); // wirklich absenden!!
  130. }
  131. /**
  132. * @return the nachricht
  133. */
  134. public String getNachricht()
  135. {
  136. System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht);
  137. return nachricht;
  138. }
  139. }