0

ちょっとしたチャットプログラムを作ろうとしましたが、本当にうまくいきません。サーバーとクライアントはほとんど同じです。新しいソケットと新しいチャット (GUI) を作成します。誰かが私が犯した間違いを見つけるのを手伝ってもらえますか?

サーバーコード:

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import java.net.Socket;
import java.net.ServerSocket;

public class Server {
    private     Chat            ch      = null;
    private     ServerSocket    server  = null;
    private     Socket          s       = null;
    private     BufferedReader  in      = null;
    private     BufferedWriter  out     = null;

    public Server() {
        try {
            server = new ServerSocket(1792);
            s = server.accept();
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            ch = new Chat("Server", out);
        } catch (IOException ioe) {

        }

        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        String line = in.readLine();
                        ch.showString(line);
                    } catch (IOException e) {
                        System.exit(0);     // exit program when connection is lost
                        return;
                    }
                }
            }
        }).start();
    }
}

クライアントコード:

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import java.net.Socket;

public class Client {
    private     Chat            ch      = null;
    private     Socket          s       = null;
    private     BufferedReader  in      = null;
    private     BufferedWriter  out     = null;

    public Client() {
        try {
            s = new Socket("localHost", 1792);
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            ch = new Chat("Client", out);
        } catch (IOException ioe) {

       }

        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        String line = in.readLine();
                        ch.showString(line);
                    } catch (IOException e) {
                        System.exit(0);     // exit program when connection is lost
                        return;
                    }
                }
            }
        }).start();
    }
}

チャット (GUI) コード:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.io.BufferedWriter;
import java.io.IOException;

public class Chat implements ActionListener {
    protected   JFrame          fr;
    private     JPanel          p;
    private     JTextField      tf;
    private     JButton         b;
    private     int             lines   = 20;
    private     JLabel[]        l       = new JLabel[lines];
    private     String          title   = "";
    private     BufferedWriter  out;

    public Chat(String name, BufferedWriter bw) {
        title = name;
        out = bw;

        fr = new JFrame(title);
        fr.setLayout(new BorderLayout());
        fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
        fr.setSize(400, 475);
        fr.setLocationRelativeTo(null);
        fr.setResizable(false);
        fr.setVisible(true);

        Insets in = fr.getInsets();
        int width = fr.getWidth() - in.left - in.right;
        int height = fr.getHeight() - in.top - in.bottom;

        p = new JPanel();
        p.setLayout(null);
        p.setBackground(Color.WHITE);
        fr.add(p, BorderLayout.CENTER);

        tf = new JTextField();
        tf.setHorizontalAlignment(tf.LEFT);
        tf.addActionListener(this);
        tf.setBounds(0, 400, 300, height-400);
        p.add(tf);

        b = new JButton("Send");
        b.setBounds(300, 400, width-300, height-400);
        b.addActionListener(this);
        p.add(b);

        p.validate();
        fr.validate();

        p.repaint();
        fr.repaint();

        for(int i=0;i<lines;i++) {
            l[i] = new JLabel("");
            l[i].setHorizontalAlignment(l[i].LEFT);
            l[i].setBounds(0, 400*i/lines, 500, 400/lines);
            p.add(l[i]);
        }
    }

    public void showString(String text) {
        if (text.equals("")) return;
        for(int i=0;i<lines-1;i++) {
            l[i].setText(l[i+1].getText());
        }
        l[lines-1].setText(text);
    }

    public void actionPerformed(ActionEvent ae) {
        String text = tf.getText();
        try {
            showString(text);
            out.write(text);
            out.flush();
            tf.setText("");
        } catch (IOException ioe) {

        }
    }
}
4

1 に答える 1

1

readLine() 関数は、行の終わりを待ちます。チャット クラスでは、出力ストリームへの書き込み中に行末を指定することはありません。そのため、サーバーとクライアントの両方が in.readLine() で行末文字を待機し、1 行の読み取りを完了します。出力ストリームに書き込む各行の後に、行末文字を付ける必要があります。洗い流すだけでは不十分です。

したがって、Chatクラスでこの部分を変更するだけです。

古いコード

        out.write(text);
        out.flush();

新しいコード

        out.write(text);
        out.write('\n');
        out.flush();

残りのコードはすべて問題なく動作しています。幸運を!

于 2014-10-26T16:29:44.540 に答える