gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:03:26 25 4
gpt4 key购买 nike

我有一个不久前为类制作的聊天程序。我正在尝试更改其中的某些部分,其中包括发送字节而不是字符串。如果有人可以指导如何更改此程序以发送字节而不是字符串的正确方向,请告诉我。

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:

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 )

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

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

(冲洗并重复输出 socket )

关于java - 发送字节而不是字符串的聊天程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3225336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com