发送字节而不是字符串的聊天程序

时间:2010-07-12 01:17:56

标签: java

我有一个聊天程序,我刚才上课了。我试图改变它的一些部分,包括发送字节而不是字符串。如果有人可以指导如何更改此程序以发送字节而不是字符串的正确方向,请告诉我。

import java.awt. * ;
import java.awt.event. * ;
import java.net. * ;
import java.io. * ;
import javax.swing. * ;
import java.applet. * ; //for sounds
import java.net. * ; //for sounds
public class ServerChat extends JPanel implements Runnable, ActionListener {

    protected ServerSocket serverSocket;
    protected Socket socket;
    protected BufferedReader reader;
    protected BufferedWriter writer;
    protected JTextField text;
    protected JButton send, exit;
    protected List list;
    protected java.applet.AudioClip clip;
    protected Image bic = new ImageIcon("smoothy_blue.jpg").getImage();

    public ServerChat() {

        setLayout(null);

        send = new JButton("Send");
        send.addActionListener(this);
        exit = new JButton("Exit");
        exit.addActionListener(this);
        text = new JTextField();
        list = new List();
        list.setBounds(10, 10, 300, 350);
        add(list);
        send.setBounds(320, 340, 100, 20);
        add(send);
        exit.setBounds(320, 365, 100, 20);
        add(exit);
        text.setBounds(10, 365, 300, 20);
        add(text);
        connect();

    }

    public void playSound() {

        try {
            //create the clip that will played later
            clip = java.applet.Applet.newAudioClip(
              new java.net.URL("file:blip.wav"));
            clip.play();
        }
        catch (Exception xx) {
            xx.printStackTrace();
        }
    }

    public void connect() {
        try {
            // create the sever socket
            serverSocket = new ServerSocket(100);

            //accept the socket connection
            socket = serverSocket.accept();

            reader = new BufferedReader(
              new InputStreamReader(socket.getInputStream()));

            writer = new BufferedWriter(
              new OutputStreamWriter(socket.getOutputStream()));

            writer.write("Hello");
            writer.newLine();
            writer.flush();
            // start the thread
            Thread thread = new Thread(this);
            thread.start();


        } catch (Exception e) {
            e.getMessage();
        }

    }

    public void run() {
        try {
            socket.setSoTimeout(1);
        } catch (Exception e) {}
        while (true) {
            try {
                list.addItem(reader.readLine());
            } catch (Exception h) {
                h.getMessage();
            }
        }

    }

    public void sendMessage() {
        try {
            writer.write(text.getText());
            writer.newLine();
            writer.flush();
            text.setText("");
        } catch (Exception m) {
            m.getMessage();
        }
    }


    public void actionPerformed(ActionEvent event) {
        Object obj = event.getSource();

        if (obj == exit) {
            System.exit(0);
        }
        if (obj == send) {
            sendMessage();
            playSound();


        }
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.drawImage(bic, 0, 0, null);

    }
}

1 个答案:

答案 0 :(得分:1)

您已经在发送和接收字节(这是所有套接字通信的基础),您只需选择使用具有以下行为的InputStreamReader

  

InputStreamReader是来自的桥梁   字节流到字符流:它   读取字节并将其解码为   使用指定字符集的字符。   它使用的charset可能是   由名称指定或可能给出   显式,或平台的默认值   charset可能被接受。

API

即。您的字符将自动转换为字节,然后通过套接字接口发送。

如果要发送原始字节,请将InputStream包装成处理原始字节的内容,而不进行字节到字符的转换。例如BufferedInputStream

(冲洗并重复输出插座)