开发者

Chat Program that Sends Bytes Instead of String

开发者 https://www.devze.com 2023-01-06 15:07 出处:网络
I have a chat program I made a while ago for a class. I am trying to changes some parts of it and that includes sending bytes instead of strings. If anyone can guide to the correct direction on how to

I have a chat program I made a while ago for a class. I am trying to changes some parts of it and that includes sending bytes instead of strings. If anyone can guide to the correct direction on how to change this program to send bytes instead of strings, please let me know.

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 voi开发者_开发问答d 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);

    }
}


You are already sending and receiving bytes (this is fundamental to all socket communication), you're just choosing to use an InputStreamReader which has the following behaviour:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

(API)

i.e. Your characters are automatically being converted into bytes, which is then sent through the socket interface.

If you want to send raw bytes, then wrap your InputStream into something that deals with raw bytes and doesn't do byte-to-character conversion. e.g. BufferedInputStream

(Rinse and repeat for your output socket)

0

精彩评论

暂无评论...
验证码 换一张
取 消