Java Chat-Programm

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Java Chat-Programm

    Hi Leute!
    Ich bräuchte mal eure Hilfe, ich hab hier ein Chat-Programm, dass aber nur mit einem Server und einem Client funktioniert. Es soll aber wenn möglich mit mehreren Client (am besten ohne Server-Instanz) rennen.
    Habt ihr da vl Tips für mich wie ich dieses Beispiel verändern kann?

    mfg, nagl


    Quellcode

    1. public class TCPChat {
    2. public static void main(String args[]) {
    3. TCPChatCom comChat = new TCPChatCom();
    4. TCPChatGUI guiChat = new TCPChatGUI();
    5. guiChat.createAndShowGUI( comChat );
    6. comChat.setGUI( guiChat );
    7. }
    8. }
    9. import java.lang.*;
    10. import java.io.*;
    11. import java.net.*;
    12. public class TCPChatCom implements Runnable {
    13. private ServerSocket defSocket = null;
    14. private Socket srvSocket = null;
    15. private BufferedReader inStream = null;
    16. private PrintWriter outStream = null;
    17. private TCPChatGUI guiChat = null;
    18. private Thread process = null;
    19. TCPChatCom() {
    20. }
    21. public void run() {
    22. System.out.println("Verbindung hergestellt");
    23. try {
    24. while( true ) {
    25. try { // Poll every ~10 ms
    26. Thread.sleep(10);
    27. }
    28. catch (InterruptedException e) {}
    29. if (inStream.ready()) {
    30. System.out.print("erhalten: ");
    31. String s = inStream.readLine();
    32. if ((s != null) && (s.length() != 0)) {
    33. guiChat.addMessage( s );
    34. System.out.println(s);
    35. }
    36. }
    37. }
    38. }
    39. catch(Exception e) {
    40. System.out.println("Fehler aufgetreten");
    41. }
    42. try {
    43. inStream.close();
    44. outStream.close();
    45. srvSocket.close();
    46. if ( defSocket != null )
    47. defSocket.close();
    48. System.out.println("Verbindung beendet");
    49. }
    50. catch(Exception e) {
    51. System.out.println("Fehler aufgetreten");
    52. }
    53. }
    54. public void startServer() {
    55. try {
    56. System.out.println("startServer() -> Server wartet auf Verbindung");
    57. defSocket = new ServerSocket(Integer.parseInt(guiChat.port.getText()) );
    58. srvSocket = defSocket.accept();
    59. inStream = new BufferedReader(new InputStreamReader(srvSocket.getInputStream()));
    60. outStream = new PrintWriter(srvSocket.getOutputStream(), true);
    61. process = new Thread( this );
    62. process.start();
    63. }
    64. catch(Exception e) {
    65. System.out.println("TCPChatCom startServer(): Fehler aufgetreten");
    66. }
    67. }
    68. public void startConnection( ) {
    69. try {
    70. String ip = guiChat.ipAdr.getText();
    71. int port = Integer.parseInt(guiChat.port.getText());
    72. srvSocket = new Socket(ip, port );
    73. System.out.println("start Connection");
    74. inStream = new BufferedReader(new InputStreamReader(srvSocket.getInputStream()));
    75. outStream = new PrintWriter(srvSocket.getOutputStream(), true);
    76. process = new Thread( this );
    77. process.start();
    78. }
    79. catch(Exception e) {
    80. System.out.println("TCPChatCom startConnection(): Fehler aufgetreten");
    81. }
    82. }
    83. public void stopConnection( ) {
    84. try {
    85. process.stop();
    86. inStream.close();
    87. outStream.close();
    88. srvSocket.close();
    89. if ( defSocket != null )
    90. defSocket.close();
    91. }
    92. catch(Exception e) {
    93. System.out.println("TCPChatCom stopConnection(): Fehler aufgetreten");
    94. }
    95. }
    96. public void sendMessage( String inMessage ) {
    97. try {
    98. System.out.println("Sende Nachricht: " + inMessage );
    99. outStream.print( inMessage );
    100. outStream.flush();
    101. }
    102. catch(Exception e) {
    103. System.out.println("TCPChatCom sendMessage(): Fehler aufgetreten");
    104. }
    105. }
    106. public void setGUI( TCPChatGUI inGuiChat ) {
    107. guiChat = inGuiChat;
    108. }
    109. }
    110. import java.io.*;
    111. import java.awt.*;
    112. import java.awt.event.*;
    113. import javax.swing.*;
    114. import javax.swing.event.*;
    115. import java.lang.*;
    116. import java.util.*;
    117. public class TCPChatGUI extends JPanel {
    118. private static TCPChatCom comChat = null;
    119. public static JFrame mainFrame = null;
    120. public static JTextArea chatText = null;
    121. public static JTextField chatLine = null;
    122. public static JPanel statusBar = null;
    123. public static JLabel statusField = null;
    124. public static JTextField myName = null;
    125. public static JTextField statusColor = null;
    126. public static JTextField ipAdr = null;
    127. public static JTextField port = null;
    128. public static JRadioButton hostOption = null;
    129. public static JRadioButton guestOption = null;
    130. public static JButton connectButton = null;
    131. public static JButton disconnectButton = null;
    132. ActionListener buttonListener = null;
    133. ChatListener chatListener = null;
    134. public TCPChatGUI() {
    135. super(new BorderLayout());
    136. chatListener = new ChatListener();
    137. JPanel pane = null;
    138. JPanel optionsPane = new JPanel(new GridLayout(5, 1));
    139. pane = new JPanel(new FlowLayout(FlowLayout.CENTER));
    140. pane.add(new JLabel("My Name:"));
    141. myName = new JTextField(10);
    142. myName.setText("");
    143. myName.setEnabled(true);
    144. pane.add( myName );
    145. optionsPane.add(pane);
    146. JPanel ipPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    147. ipPanel.add(new JLabel("Server IP:"));
    148. ipAdr = new JTextField(10);
    149. ipAdr.setText("127.0.0.1");
    150. ipAdr.setEnabled(true);
    151. ipPanel.add(ipAdr);
    152. ipPanel.add(new JLabel("Port: "));
    153. port = new JTextField(5);
    154. port.setText("1234");
    155. port.setEnabled(true);
    156. ipPanel.add(port);
    157. optionsPane.add(ipPanel);
    158. ButtonGroup bg = new ButtonGroup();
    159. hostOption = new JRadioButton("Server", true);
    160. hostOption.setMnemonic(KeyEvent.VK_H);
    161. hostOption.setActionCommand("server");
    162. guestOption = new JRadioButton("Client", false);
    163. guestOption.setMnemonic(KeyEvent.VK_G);
    164. guestOption.setActionCommand("client");
    165. bg.add(hostOption);
    166. bg.add(guestOption);
    167. pane = new JPanel(new GridLayout(1, 2));
    168. pane.add(hostOption);
    169. pane.add(guestOption);
    170. optionsPane.add(pane);
    171. JPanel buttonPane = new JPanel(new GridLayout(1, 2));
    172. connectButton = new JButton("Connect");
    173. connectButton.setMnemonic(KeyEvent.VK_C);
    174. connectButton.setActionCommand("connect");
    175. connectButton.addActionListener( chatListener );
    176. connectButton.setEnabled(true);
    177. disconnectButton = new JButton("Disconnect");
    178. disconnectButton.setMnemonic(KeyEvent.VK_D);
    179. disconnectButton.setActionCommand("disconnect");
    180. disconnectButton.addActionListener( chatListener );
    181. disconnectButton.setEnabled(true);
    182. buttonPane.add(connectButton);
    183. buttonPane.add(disconnectButton);
    184. optionsPane.add(buttonPane);
    185. // Set up the chat pane
    186. JPanel chatPane = new JPanel(new BorderLayout());
    187. chatText = new JTextArea(10, 20);
    188. chatText.setLineWrap(true);
    189. chatText.setEditable(false);
    190. chatText.setForeground(Color.blue);
    191. JScrollPane chatTextPane = new JScrollPane(chatText,
    192. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    193. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    194. chatLine = new JTextField();
    195. chatLine.setEnabled(true);
    196. chatLine.addActionListener( chatListener );
    197. chatPane.add(chatLine, BorderLayout.SOUTH);
    198. chatPane.add(chatTextPane, BorderLayout.CENTER);
    199. chatPane.setPreferredSize(new Dimension(200, 250));
    200. this.add(optionsPane, BorderLayout.SOUTH);
    201. this.add(chatPane, BorderLayout.NORTH);
    202. }
    203. public static void createAndShowGUI( TCPChatCom inComChat ) {
    204. comChat = inComChat;
    205. JFrame frame = new JFrame("TCPChat");
    206. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    207. JComponent newContentPane = new TCPChatGUI();
    208. newContentPane.setOpaque(true); //content panes must be opaque
    209. frame.setContentPane(newContentPane);
    210. frame.pack();
    211. frame.setVisible(true);
    212. }
    213. public static void addMessage( String inMessage ) {
    214. chatText.append(inMessage + "\n" );
    215. }
    216. class ChatListener implements ActionListener {
    217. public void actionPerformed(ActionEvent e) {
    218. if ( e.getSource() == chatLine ) {
    219. String message = chatLine.getText();
    220. if ( !message.equals("") ) {
    221. comChat.sendMessage( myName.getText() + ": " + message + "\n");
    222. addMessage( myName.getText() + ": " + message );
    223. chatLine.setText("");
    224. }
    225. }
    226. if ( e.getSource() == connectButton ) {
    227. if ( hostOption.isSelected() )
    228. comChat.startServer();
    229. else
    230. comChat.startConnection();
    231. }
    232. if ( e.getSource() == disconnectButton ) {
    233. comChat.stopConnection();
    234. }
    235. }
    236. }
    237. }
    Alles anzeigen