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
Alles anzeigen
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
- public class TCPChat {
- public static void main(String args[]) {
- TCPChatCom comChat = new TCPChatCom();
- TCPChatGUI guiChat = new TCPChatGUI();
- guiChat.createAndShowGUI( comChat );
- comChat.setGUI( guiChat );
- }
- }
- import java.lang.*;
- import java.io.*;
- import java.net.*;
- public class TCPChatCom implements Runnable {
- private ServerSocket defSocket = null;
- private Socket srvSocket = null;
- private BufferedReader inStream = null;
- private PrintWriter outStream = null;
- private TCPChatGUI guiChat = null;
- private Thread process = null;
- TCPChatCom() {
- }
- public void run() {
- System.out.println("Verbindung hergestellt");
- try {
- while( true ) {
- try { // Poll every ~10 ms
- Thread.sleep(10);
- }
- catch (InterruptedException e) {}
- if (inStream.ready()) {
- System.out.print("erhalten: ");
- String s = inStream.readLine();
- if ((s != null) && (s.length() != 0)) {
- guiChat.addMessage( s );
- System.out.println(s);
- }
- }
- }
- }
- catch(Exception e) {
- System.out.println("Fehler aufgetreten");
- }
- try {
- inStream.close();
- outStream.close();
- srvSocket.close();
- if ( defSocket != null )
- defSocket.close();
- System.out.println("Verbindung beendet");
- }
- catch(Exception e) {
- System.out.println("Fehler aufgetreten");
- }
- }
- public void startServer() {
- try {
- System.out.println("startServer() -> Server wartet auf Verbindung");
- defSocket = new ServerSocket(Integer.parseInt(guiChat.port.getText()) );
- srvSocket = defSocket.accept();
- inStream = new BufferedReader(new InputStreamReader(srvSocket.getInputStream()));
- outStream = new PrintWriter(srvSocket.getOutputStream(), true);
- process = new Thread( this );
- process.start();
- }
- catch(Exception e) {
- System.out.println("TCPChatCom startServer(): Fehler aufgetreten");
- }
- }
- public void startConnection( ) {
- try {
- String ip = guiChat.ipAdr.getText();
- int port = Integer.parseInt(guiChat.port.getText());
- srvSocket = new Socket(ip, port );
- System.out.println("start Connection");
- inStream = new BufferedReader(new InputStreamReader(srvSocket.getInputStream()));
- outStream = new PrintWriter(srvSocket.getOutputStream(), true);
- process = new Thread( this );
- process.start();
- }
- catch(Exception e) {
- System.out.println("TCPChatCom startConnection(): Fehler aufgetreten");
- }
- }
- public void stopConnection( ) {
- try {
- process.stop();
- inStream.close();
- outStream.close();
- srvSocket.close();
- if ( defSocket != null )
- defSocket.close();
- }
- catch(Exception e) {
- System.out.println("TCPChatCom stopConnection(): Fehler aufgetreten");
- }
- }
- public void sendMessage( String inMessage ) {
- try {
- System.out.println("Sende Nachricht: " + inMessage );
- outStream.print( inMessage );
- outStream.flush();
- }
- catch(Exception e) {
- System.out.println("TCPChatCom sendMessage(): Fehler aufgetreten");
- }
- }
- public void setGUI( TCPChatGUI inGuiChat ) {
- guiChat = inGuiChat;
- }
- }
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.lang.*;
- import java.util.*;
- public class TCPChatGUI extends JPanel {
- private static TCPChatCom comChat = null;
- public static JFrame mainFrame = null;
- public static JTextArea chatText = null;
- public static JTextField chatLine = null;
- public static JPanel statusBar = null;
- public static JLabel statusField = null;
- public static JTextField myName = null;
- public static JTextField statusColor = null;
- public static JTextField ipAdr = null;
- public static JTextField port = null;
- public static JRadioButton hostOption = null;
- public static JRadioButton guestOption = null;
- public static JButton connectButton = null;
- public static JButton disconnectButton = null;
- ActionListener buttonListener = null;
- ChatListener chatListener = null;
- public TCPChatGUI() {
- super(new BorderLayout());
- chatListener = new ChatListener();
- JPanel pane = null;
- JPanel optionsPane = new JPanel(new GridLayout(5, 1));
- pane = new JPanel(new FlowLayout(FlowLayout.CENTER));
- pane.add(new JLabel("My Name:"));
- myName = new JTextField(10);
- myName.setText("");
- myName.setEnabled(true);
- pane.add( myName );
- optionsPane.add(pane);
- JPanel ipPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
- ipPanel.add(new JLabel("Server IP:"));
- ipAdr = new JTextField(10);
- ipAdr.setText("127.0.0.1");
- ipAdr.setEnabled(true);
- ipPanel.add(ipAdr);
- ipPanel.add(new JLabel("Port: "));
- port = new JTextField(5);
- port.setText("1234");
- port.setEnabled(true);
- ipPanel.add(port);
- optionsPane.add(ipPanel);
- ButtonGroup bg = new ButtonGroup();
- hostOption = new JRadioButton("Server", true);
- hostOption.setMnemonic(KeyEvent.VK_H);
- hostOption.setActionCommand("server");
- guestOption = new JRadioButton("Client", false);
- guestOption.setMnemonic(KeyEvent.VK_G);
- guestOption.setActionCommand("client");
- bg.add(hostOption);
- bg.add(guestOption);
- pane = new JPanel(new GridLayout(1, 2));
- pane.add(hostOption);
- pane.add(guestOption);
- optionsPane.add(pane);
- JPanel buttonPane = new JPanel(new GridLayout(1, 2));
- connectButton = new JButton("Connect");
- connectButton.setMnemonic(KeyEvent.VK_C);
- connectButton.setActionCommand("connect");
- connectButton.addActionListener( chatListener );
- connectButton.setEnabled(true);
- disconnectButton = new JButton("Disconnect");
- disconnectButton.setMnemonic(KeyEvent.VK_D);
- disconnectButton.setActionCommand("disconnect");
- disconnectButton.addActionListener( chatListener );
- disconnectButton.setEnabled(true);
- buttonPane.add(connectButton);
- buttonPane.add(disconnectButton);
- optionsPane.add(buttonPane);
- // Set up the chat pane
- JPanel chatPane = new JPanel(new BorderLayout());
- chatText = new JTextArea(10, 20);
- chatText.setLineWrap(true);
- chatText.setEditable(false);
- chatText.setForeground(Color.blue);
- JScrollPane chatTextPane = new JScrollPane(chatText,
- JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
- JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- chatLine = new JTextField();
- chatLine.setEnabled(true);
- chatLine.addActionListener( chatListener );
- chatPane.add(chatLine, BorderLayout.SOUTH);
- chatPane.add(chatTextPane, BorderLayout.CENTER);
- chatPane.setPreferredSize(new Dimension(200, 250));
- this.add(optionsPane, BorderLayout.SOUTH);
- this.add(chatPane, BorderLayout.NORTH);
- }
- public static void createAndShowGUI( TCPChatCom inComChat ) {
- comChat = inComChat;
- JFrame frame = new JFrame("TCPChat");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JComponent newContentPane = new TCPChatGUI();
- newContentPane.setOpaque(true); //content panes must be opaque
- frame.setContentPane(newContentPane);
- frame.pack();
- frame.setVisible(true);
- }
- public static void addMessage( String inMessage ) {
- chatText.append(inMessage + "\n" );
- }
- class ChatListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if ( e.getSource() == chatLine ) {
- String message = chatLine.getText();
- if ( !message.equals("") ) {
- comChat.sendMessage( myName.getText() + ": " + message + "\n");
- addMessage( myName.getText() + ": " + message );
- chatLine.setText("");
- }
- }
- if ( e.getSource() == connectButton ) {
- if ( hostOption.isSelected() )
- comChat.startServer();
- else
- comChat.startConnection();
- }
- if ( e.getSource() == disconnectButton ) {
- comChat.stopConnection();
- }
- }
- }
- }