带 GUI 的 java ChatServer

发布于 2024-11-30 22:54:27 字数 3732 浏览 0 评论 0原文

我正在尝试使用 GUI 组件来实现聊天服务器。我实现了 3 个部分(服务器、客户端和 GUI 组件)。

以下是我遇到的一些问题 -

  • 我无法用 GUI 代码包装代码。
  • 当与客户交谈时,只有当每个人都只输入一行并按回车键时,它才有效。

代码:

第一个 GUI 组件:

public class ChatServer extends javax.swing.JFrame {
    String str;

    public ChatServer() {
        initComponents();      
        screen.setEditable(false);
    }

private void sendActionPerformed(java.awt.event.ActionEvent evt) {                                     
    str = enter.getText();
    enter.setText("");
    screen.append(str+"\n");

}

public static void main(String args[]) {
        new ChatServer().setVisible(true);
}

    private javax.swing.JTextPane enter;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea screen;
    private javax.swing.JButton send;

}

,它看起来像:

在此处输入图像描述

这个我的服务器代码:

public class Server {
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(23);
        System.out.println(InetAddress.getLocalHost()+" hazir");
        while(true){
            Socket s = ss.accept();
            System.out.println(s.getInetAddress().getHostName() + " baglandi");
            new ServerPart(s).start();
        }
    }

}

public class ServerPart extends Thread {
    private Socket s;
    public ServerPart(Socket s){
        this.s=s;
    }

    @Override
    public void run() {

        try {

            PrintStream ps = new PrintStream(s.getOutputStream());

            ps.println("Hello" + s.getInetAddress().getHostName());

            String gelen;

            while(true){ 

            Scanner sc = new Scanner(s.getInputStream());   

            gelen = sc.nextLine();

            if(gelen.trim().equalsIgnoreCase("bye"))
                break;


            System.out.println("Client: " + gelen);

            BufferedReader input = new BufferedReader( 
                    new InputStreamReader(System.in) );

            ps.println("Server: " + input.readLine());

            }

            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

客户端代码:

public class Client extends Thread {
    private Socket s;
    public Client(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        try {

            PrintStream ps = new PrintStream(s.getOutputStream());

            Scanner sc = new Scanner(s.getInputStream());

            ps.println("Hello" + s.getInetAddress().getHostName());

            String gelen;

            while(true){

                BufferedReader input = new BufferedReader( 
                        new InputStreamReader(System.in) );

            gelen = sc.nextLine();

            if(gelen.trim().equalsIgnoreCase("bye"))
                break;

            System.out.println("Client: " + gelen);
            ps.println("Server: " + input.readLine());


            }

            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    /**
     * @param args
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public static void main(String[] args) throws UnknownHostException, IOException {

        Socket s = new Socket("192.168.1.173", 23);

        new Client(s);

    }

}

如果您能帮助我,我将不胜感激。

I am trying to implement a chatserver with using GUI components. I implemented 3 parts(Server,Client and GUI components).

Following are a few problems I have -

  • I can't wrap the code with GUI code.
  • When talking with clients, it only works when everybody enter only one line and press enter.

Code:

First GUI component:

public class ChatServer extends javax.swing.JFrame {
    String str;

    public ChatServer() {
        initComponents();      
        screen.setEditable(false);
    }

private void sendActionPerformed(java.awt.event.ActionEvent evt) {                                     
    str = enter.getText();
    enter.setText("");
    screen.append(str+"\n");

}

public static void main(String args[]) {
        new ChatServer().setVisible(true);
}

    private javax.swing.JTextPane enter;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea screen;
    private javax.swing.JButton send;

}

and it looks like:

enter image description here

This my server code:

public class Server {
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(23);
        System.out.println(InetAddress.getLocalHost()+" hazir");
        while(true){
            Socket s = ss.accept();
            System.out.println(s.getInetAddress().getHostName() + " baglandi");
            new ServerPart(s).start();
        }
    }

}

public class ServerPart extends Thread {
    private Socket s;
    public ServerPart(Socket s){
        this.s=s;
    }

    @Override
    public void run() {

        try {

            PrintStream ps = new PrintStream(s.getOutputStream());

            ps.println("Hello" + s.getInetAddress().getHostName());

            String gelen;

            while(true){ 

            Scanner sc = new Scanner(s.getInputStream());   

            gelen = sc.nextLine();

            if(gelen.trim().equalsIgnoreCase("bye"))
                break;


            System.out.println("Client: " + gelen);

            BufferedReader input = new BufferedReader( 
                    new InputStreamReader(System.in) );

            ps.println("Server: " + input.readLine());

            }

            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

Client code:

public class Client extends Thread {
    private Socket s;
    public Client(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        try {

            PrintStream ps = new PrintStream(s.getOutputStream());

            Scanner sc = new Scanner(s.getInputStream());

            ps.println("Hello" + s.getInetAddress().getHostName());

            String gelen;

            while(true){

                BufferedReader input = new BufferedReader( 
                        new InputStreamReader(System.in) );

            gelen = sc.nextLine();

            if(gelen.trim().equalsIgnoreCase("bye"))
                break;

            System.out.println("Client: " + gelen);
            ps.println("Server: " + input.readLine());


            }

            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    /**
     * @param args
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public static void main(String[] args) throws UnknownHostException, IOException {

        Socket s = new Socket("192.168.1.173", 23);

        new Client(s);

    }

}

Would appreciate if you can help me mates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2024-12-07 22:54:27

您正在使用 PrintStream 类,以及方法 ps.readline();。如果您正在开发聊天应用程序,那么这种方法将不起作用,因为 readline 方法在找到换行符或文件结尾时,或者在回车即输入时终止流。所以我更喜欢使用.. datainputstream 和 dataoutputstream --

Socket sc = new Socket("address",port);
DataOutputStream daos;
DataInputStream dis;
dis = new DataInputStream(sc.getInputStream());
daos= new DataOutputStream(sc.getOutputStream());

You are using PrintStream class , with method ps.readline();. If you are developing a chat application, then this approach will not work, as readline method terminate the stream when found newline or End of file, or when carriage return i.e. enter. So I do prefer to use.. datainputstream and dataoutputstream --

Socket sc = new Socket("address",port);
DataOutputStream daos;
DataInputStream dis;
dis = new DataInputStream(sc.getInputStream());
daos= new DataOutputStream(sc.getOutputStream());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文