Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 发送字节而不是字符串的聊天程序_Java - Fatal编程技术网

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

Java 发送字节而不是字符串的聊天程序,java,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. *

我有一个聊天程序,我刚才为一节课做的。我试图改变它的某些部分,包括发送字节而不是字符串。如果有人能指导正确的方向如何改变这个程序发送字节而不是字符串,请让我知道

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);

    }
}

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

InputStreamReader是从 字节流到字符流:It 读取字节并将其解码为 使用指定字符集的字符。 它使用的字符集可能是 由名称指定或可以指定 显式,或平台的默认值 可以接受字符集

()

i、 e.您的字符将自动转换为字节,然后通过套接字接口发送

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


(冲洗并重复你的输出插座)

我想在学校,他们的评论并不多