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.

CommandConnect.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 netz.controller;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.io.IOException;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12. import java.util.logging.Logger;
  13. import ohmlogger.OhmLogger;
  14. import netz.model.ChatModel;
  15. import netz.view.ChatView;
  16. /**
  17. *
  18. * @author hd, chris
  19. */
  20. public class CommandConnect implements ActionListener
  21. {
  22. private static Logger lg = OhmLogger.getLogger();
  23. ChatView view;
  24. ChatModel model;
  25. private static final int PORT = 35000;
  26. private static final String IP_ADRESSE = "127.0.0.1";
  27. public CommandConnect(ChatView view, ChatModel transmitter)
  28. {
  29. this.view = view;
  30. this.model = transmitter;
  31. }
  32. public void registerEvents(){
  33. view.getBtnSetServer().addActionListener(this);
  34. view.getBtnSetClient().addActionListener(this);
  35. }
  36. @Override
  37. public void actionPerformed(ActionEvent e) {
  38. Object src = e.getSource();
  39. // connect to server
  40. if(src == view.getBtnSetServer()){
  41. synchronized (this){
  42. try {
  43. view.getLblType().setText("Server");
  44. ServerSocket sSocket = new ServerSocket(PORT);
  45. lg.info("Server: Warte auf Verbindung ...");
  46. Socket s = sSocket.accept(); // Achtung: blockiert!
  47. lg.info("Server: Verbindung akzeptiert");
  48. model.setSocket(s);
  49. } catch (IOException ex) {
  50. lg.severe(ex.toString());
  51. }
  52. }
  53. }
  54. // connect to vlient
  55. if(src == view.getBtnSetClient()){
  56. synchronized (this){
  57. try {
  58. view.getLblType().setText("Client");
  59. lg.info("Client: verbinde ...");
  60. Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
  61. lg.info("Client: Verbindung hergestellt");
  62. model.setSocket(s);
  63. } catch (IOException ex) {
  64. lg.severe(ex.toString());
  65. }
  66. }
  67. }
  68. }
  69. }