dimanche 3 mai 2015

Socket Wie ein einfacher Multithreads Service mit Telnet als Client mit Observable/Observer gelöst....

z.B. als Client: telnet 192.168.121.35 7777

untere code als ausführbare JAR-File erstellen

danach aufruf mit: java -jar ChattService.jar -p 7777

:oops:
Java Code:

  1.  
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.Observable;
  11. import java.util.Observer;
  12. import java.util.Scanner;
  13.  
  14.  
  15.  
  16.  
  17. /**
  18.  * @author Adriano Zuccarello
  19.  *
  20.  */
  21.  
  22.  
  23. public class ChatThreadService extends Observable {
  24.  
  25. private long nofThreads = 0;
  26. private SimpleDateFormat sdf;
  27. private int mPort = 7575;
  28.  
  29. public ChatThreadService() {
  30. sdf = new SimpleDateFormat("HH:mm:ss");
  31. }
  32. public ChatThreadService(String[] pArgs) {
  33. this();
  34. mPort = setup(pArgs);
  35. System.out.println("Listen on Port: "+ mPort);
  36. }
  37.  
  38. /**
  39.   * setup
  40.   *
  41.   * @param pArgs
  42.   * -p <port> [-n <name>] default: -n user.name
  43.   */
  44. private int setup(String[] pArgs) {
  45. for (int i = 0; i < pArgs.length; i++) {
  46. if ((pArgs[i].endsWith("-p") && ((i + 1) < pArgs.length))) {
  47. try {
  48. mPort = Integer.parseInt(pArgs[++i].trim());
  49. } catch (Exception e) {
  50. System.err.printf("Bad format in argument (%s)\nWill use Port: %i", pArgs[i], mPort);
  51. }
  52. }
  53. }
  54.  
  55.  
  56. if (mPort <= 0) {
  57. System.err.println("Port has to be defined greater 0");
  58. }
  59.  
  60. System.out.println("Setup: Port: "+ mPort);
  61. return mPort;
  62. }
  63.  
  64. public void start() {
  65. try {
  66. Socket incomingSocket = null;
  67. ServerSocket serviceSocket = new ServerSocket(mPort);
  68.  
  69. try {
  70. while( (incomingSocket = serviceSocket.accept()) != null) {
  71. System.out.println("Start Thread"+ ++nofThreads);
  72.  
  73. ClientThread threadClient = new ClientThread(incomingSocket, nofThreads);
  74.  
  75. this.addObserver(threadClient);
  76.  
  77. Thread thread = new Thread(threadClient);
  78. thread.start();
  79.  
  80. broadcast("Client-"+ nofThreads +" logged in");
  81.  
  82. java.awt.Toolkit.getDefaultToolkit().beep();
  83. }
  84. }
  85. finally {
  86. serviceSocket.close();
  87. }
  88.  
  89. } catch (IOException ioex) {
  90. ioex.printStackTrace();
  91. }
  92. }
  93.  
  94. private void removeObserver(ClientThread pCTh) {
  95. this.deleteObserver(pCTh);
  96. --nofThreads;
  97. }
  98.  
  99. private synchronized void broadcast(String message) {
  100. // add HH:mm:ss and \n to the message
  101. this.setChanged();
  102. this.notifyObservers(sdf.format(new Date()) +" "+ message);
  103. }
  104.  
  105.  
  106.  
  107. //Chat-Client Thread
  108. class ClientThread implements Runnable, Observer {
  109. private Socket mSocket;
  110. private long mCounter;
  111. private String mChatterName;
  112.  
  113. ClientThread(final Socket pSocket, final long pCount) {
  114. this.mSocket = pSocket;
  115. this.mCounter = pCount;
  116.  
  117.  
  118. System.out.println("I am ChatClient-Number: "+ mCounter);
  119. }
  120.  
  121.  
  122. @Override
  123. public void run() {
  124. try {
  125. try {
  126. mChatterName = mSocket.getInetAddress().getHostAddress() +" Chatter-"+ mCounter +": ";
  127.  
  128. printMessage(mSocket.getOutputStream(), "Hello "+ mChatterName +"! Enter BYE to exit.\n", true);
  129.  
  130. InputStream inStream = mSocket.getInputStream();
  131. Scanner inScanner = new Scanner(inStream);
  132.  
  133. boolean done = false;
  134. while(!done && inScanner.hasNextLine()) {
  135. String line = inScanner.nextLine();
  136. done = line.trim().equalsIgnoreCase("BYE") ? true : false;
  137. broadcast(mChatterName + line);
  138. System.out.println(mChatterName + line);
  139. }
  140.  
  141. inScanner.close();
  142. }
  143. finally {
  144. removeObserver(this);
  145. mSocket.close();
  146. }
  147.  
  148. } catch (IOException ioex) {
  149. ioex.printStackTrace();
  150. }
  151. }
  152.  
  153. private void printMessage(final OutputStream os, final String msg, final boolean flush) {
  154. PrintWriter pwout = new PrintWriter(os, flush);
  155. pwout.println(msg);
  156. System.out.println(msg);
  157. }
  158.  
  159.  
  160. @Override
  161. public void update(Observable o, Object arg) {
  162. try {
  163. String observerArgs = (String)arg;
  164. if(observerArgs != null && mChatterName != null && !observerArgs.contains(mChatterName)) { /* don't show your self */
  165. printMessage(mSocket.getOutputStream(), observerArgs, true);
  166. }
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171.  
  172. }//End Chat-Client Thread
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. public static void main(String[] args) {
  180. ChatThreadService thService = new ChatThreadService(args);
  181. thService.start();
  182. }
  183.  
  184.  
  185. }
Angehängte Dateien


Socket Wie ein einfacher Multithreads Service mit Telnet als Client mit Observable/Observer gelöst....

0 commentaires:

Enregistrer un commentaire