Java 客户端-服务器从文件发送文本

Java Client-Server Sending Text From File

我是Java的新手,我的class被分配了任务:

设计一个 client/server 应用程序,其中客户端直接与服务器通信。服务器将产品和客户信息存储在两个单独的文件中。客户端应用程序将允许用户从服务器请求产品和客户信息。服务器将检索请求的信息并将其发送给客户端。信息将由客户端以用户友好的方式显示。建议您的应用程序使用 Swing 组件以支持用户友好的界面。

我以前没有构建过客户端-服务器应用程序。我按照 youtube 视频教程 (https://www.youtube.com/watch?v=vCDrGJWqR8w) 来构建客户端-服务器应用程序本身,但我不确定如何实现将文本文件中的信息从服务器发送到客户端。我在网上找不到答案,不知道是我问的方式不对还是怎么的。

我想我可以在服务器文件中添加 if 语句,所以当客户端输入相应的单词时,它会 运行 将特定文件的文本发送到客户端的代码。

如果有人能指导我如何完成此任务,我将不胜感激。我的导师没有回复我,这周末我们的辅导服务停止维护。

编辑:我找到了有关如何创建 GUI 的教程 (https://www.youtube.com/watch?v=h3i459_arng)。我决定编写 GUI 并添加文件读取功能可能更容易。

我还找到了一个关于如何从我的文件中读取文本的网站 (https://www.geeksforgeeks.org/different-ways-reading-text-file-java/)

现在我可以从文件中获取文本以写入服务器聊天中的 JTextArea window,但我需要将其发送到客户端聊天中的 JTextArea window。我该怎么做?

ChatClient.java代码

package clientserver2;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jade
 */
public class ChatClient extends javax.swing.JFrame {

    /**
     * Creates new form ChatClient
     */
    public ChatClient() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaChat = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaMessage = new javax.swing.JTextArea();
        jButtonSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaChat.setColumns(20);
        jTextAreaChat.setRows(5);
        jScrollPane1.setViewportView(jTextAreaChat);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaMessage.setColumns(20);
        jTextAreaMessage.setRows(5);
        jScrollPane2.setViewportView(jTextAreaMessage);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jButtonSend.setText("Send");
        jButtonSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonSend, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        setBounds(0, 0, 407, 342);
    }// </editor-fold>                        

    private void jButtonSendActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        String message = jTextAreaMessage.getText();
        writer.println(message);
        writer.flush();

        jTextAreaChat.append("Client: " + message + "\n");
        jTextAreaMessage.setText("");
    }                                           

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        try {
            // TODO add your handling code here:
            socket = new Socket("localhost", 4789);
            scanner = new Scanner(socket.getInputStream());
            writer = new PrintWriter(socket.getOutputStream());
            
            Thread myThread = new Thread(new Runnable(){ 
                
                @Override
                public void run(){
                    
                        jTextAreaChat.append("Welcome! Please enter a command." + "\n");
                        jTextAreaChat.append("Enter products to view list of products." + "\n");
                        jTextAreaChat.append("Enter customers to view list of customers." + "\n");
                
                    while(true){

                        //jTextAreaChat.append("Welcome! Please enter a command." + "\n");
                        //jTextAreaChat.append("Enter products to view list of products." + "\n");
                        //jTextAreaChat.append("Enter customers to view list of customers." + "\n");
                        String message = scanner.nextLine();
                        jTextAreaChat.append("Server: " + message + "\n");
                
                    }
                }
 
            });
            myThread.start();
            
        } catch (IOException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }                                 

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChatClient().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonSend;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaChat;
    private javax.swing.JTextArea jTextAreaMessage;
    // End of variables declaration                   
    private Socket socket;
    private Scanner scanner;
    private PrintWriter writer;
    

}

ChatServer.java代码

package clientserver2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jade
 */
public class ChatServer extends javax.swing.JFrame {

    /**
     * Creates new form ChatClient
     */
    public ChatServer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaChat = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaMessage = new javax.swing.JTextArea();
        jButtonSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaChat.setColumns(20);
        jTextAreaChat.setRows(5);
        jTextAreaChat.setText("Waiting for Client...\n");
        jScrollPane1.setViewportView(jTextAreaChat);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaMessage.setColumns(20);
        jTextAreaMessage.setRows(5);
        jScrollPane2.setViewportView(jTextAreaMessage);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jButtonSend.setText("Send");
        jButtonSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonSend, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        setBounds(0, 0, 407, 342);
    }// </editor-fold>                        

    private void jButtonSendActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        
        String message = jTextAreaMessage.getText();
        writer.println(message);
        jTextAreaChat.append("Server: " + message + "\n");
        jTextAreaMessage.setText("");
        
        
    }                                           

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        try {
            // TODO add your handling code here:
            serverSocket = new ServerSocket(4789);        
            socket = serverSocket.accept();
            jTextAreaChat.append("Client found." + "\n"); 
            scanner = new Scanner(socket.getInputStream());
            writer = new PrintWriter(socket.getOutputStream(), true);
            
            Thread myThread = new Thread(new Runnable() {
                
                @Override
                public void run(){
                    
                    while(true){
                
                        String message = scanner.nextLine();
                        jTextAreaChat.append("Client: " + message + "\n");
                        
                        if (message.equalsIgnoreCase("products"))
                        {                                                        
                            try {
                                File file = new File("C:\Users\jades\Desktop\Java\ClientServer2\src\clientserver2\Products.txt");
                                Scanner sc = new Scanner(file);
                                while (sc.hasNextLine()){                                    
                                    jTextAreaChat.append(sc.nextLine() + "\n");
                                }
                            } catch (FileNotFoundException ex) {
                                Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        
                        if (message.equalsIgnoreCase("customers"))
                        {                                                        
                            try {
                                File file = new File("C:\Users\jades\Desktop\Java\ClientServer2\src\clientserver2\Customers.txt");
                                Scanner sc = new Scanner(file);
                                while (sc.hasNextLine()){                                    
                                    jTextAreaChat.append(sc.nextLine() + "\n");
                                }
                            } catch (FileNotFoundException ex) {
                                Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                
                    }

                }
            });
            myThread.start();
            
            
            
        } catch (IOException ex) {
            Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }                                 

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChatServer().setVisible(true);
            }
        });
        
        
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonSend;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaChat;
    private javax.swing.JTextArea jTextAreaMessage;
    // End of variables declaration                   
private ServerSocket serverSocket;
private Socket socket;
private Scanner scanner;
private PrintWriter writer;                                                                                                             



}

以防万一有人想知道,我想通了。我已经添加了 GUI,并找到了从 txt 文件中读取的代码,但我被卡住了,因为文件中的文本被添加到服务器的 GUI 中,我需要它添加到客户端的 GUI 中。

我需要使用我的 PrintWriter 对象编写器将文本发送到我的客户端。

这是我更新后的代码:

ChatClient.java 文件

package clientserver2;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChatClient extends javax.swing.JFrame {

    public ChatClient() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaChat = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaMessage = new javax.swing.JTextArea();
        jButtonSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaChat.setColumns(20);
        jTextAreaChat.setRows(5);
        jScrollPane1.setViewportView(jTextAreaChat);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaMessage.setColumns(20);
        jTextAreaMessage.setRows(5);
        jScrollPane2.setViewportView(jTextAreaMessage);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jButtonSend.setText("Send");
        jButtonSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonSend, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        setBounds(0, 0, 407, 342);
    }// </editor-fold>                        

    private void jButtonSendActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String message = jTextAreaMessage.getText();
        writer.println(message);
        writer.flush();

        jTextAreaChat.append("Client: " + message + "\n");
        jTextAreaMessage.setText("");
    }                                           

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        try {
            socket = new Socket("localhost", 4789);
            scanner = new Scanner(socket.getInputStream());
            writer = new PrintWriter(socket.getOutputStream());
            
            Thread myThread = new Thread(new Runnable(){ 
                
                @Override
                public void run(){
                    
                        jTextAreaChat.append("Welcome! Please enter a command." + "\n");
                        jTextAreaChat.append("Enter products to view list of products." + "\n");
                        jTextAreaChat.append("Enter customers to view list of customers." + "\n");
                
                    while(true){
                        String message = scanner.nextLine();
                        jTextAreaChat.append(message + "\n");
                    }
                }
 
            });
            myThread.start();
            
        } catch (IOException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }                                 

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChatClient().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonSend;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaChat;
    private javax.swing.JTextArea jTextAreaMessage;
    // End of variables declaration                   
    private Socket socket;
    private Scanner scanner;
    private PrintWriter writer;
    

}

ChatServer.java 文件

package clientserver2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChatServer extends javax.swing.JFrame {

    public ChatServer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaChat = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaMessage = new javax.swing.JTextArea();
        jButtonSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaChat.setColumns(20);
        jTextAreaChat.setRows(5);
        jTextAreaChat.setText("Waiting for Client...\n");
        jScrollPane1.setViewportView(jTextAreaChat);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout(10, 10));

        jTextAreaMessage.setColumns(20);
        jTextAreaMessage.setRows(5);
        jScrollPane2.setViewportView(jTextAreaMessage);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jButtonSend.setText("Send");
        jButtonSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSendActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonSend, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        setBounds(0, 0, 407, 342);
    }// </editor-fold>                        

    private void jButtonSendActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String message = jTextAreaMessage.getText();
        writer.println(message);
        jTextAreaChat.append("Server: " + message + "\n");
        jTextAreaMessage.setText("");
        
        
    }                                           

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        try {
            // TODO add your handling code here:
            serverSocket = new ServerSocket(4789);        
            socket = serverSocket.accept();
            jTextAreaChat.append("Client found." + "\n"); 
            scanner = new Scanner(socket.getInputStream());
            writer = new PrintWriter(socket.getOutputStream(), true);
            
            Thread myThread = new Thread(new Runnable() {
                
                @Override
                public void run(){
                    
                    while(true){
                
                        String message = scanner.nextLine();
                        jTextAreaChat.append("Client: " + message + "\n");
                        
                        if (message.equalsIgnoreCase("products"))
                        {                                                        
                            try {
                                File file = new File("C:\Users\jades\Desktop\Java\ClientServer2\src\clientserver2\Products.txt"); 
                                Scanner sc = new Scanner(file);
                                while (sc.hasNextLine()){
                                    writer.println(sc.nextLine());
                                    writer.flush();
                                }
                            } catch (FileNotFoundException ex) {
                                Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        
                        if (message.equalsIgnoreCase("customers"))
                        {                                                        
                            try {
                                File file = new File("C:\Users\jades\Desktop\Java\ClientServer2\src\clientserver2\Customers.txt");
                                Scanner sc = new Scanner(file);
                                while (sc.hasNextLine()){                                    
                                    writer.println(sc.nextLine());
                                    writer.flush();
                                }
                            } catch (FileNotFoundException ex) {
                                Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }                
                    }
                }
            });
            myThread.start();
                                  
        } catch (IOException ex) {
            Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }                                 

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChatServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChatServer().setVisible(true);
            }
        });               
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonSend;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaChat;
    private javax.swing.JTextArea jTextAreaMessage;
    // End of variables declaration                   
private ServerSocket serverSocket;
private Socket socket;
private Scanner scanner;
private PrintWriter writer;                                                                                                           
}