用java写一个可以互联客户端和服务端,声明页面组件,组件中含有文本域,流动条,面板,文本框,俩个按钮b1,b2.b1可以发送信息,b2可以发送图片。最后摄制监听
时间: 2023-12-02 18:02:30 浏览: 27
好的,这是一个比较复杂的问题,需要用到Java Socket编程以及Swing组件。我会尽可能详细地回答你。
首先,我们需要在客户端和服务端之间建立Socket连接。客户端会向服务端请求连接,如果连接成功,就可以进行通信。以下是客户端和服务端的代码:
客户端代码:
```java
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client extends JFrame implements ActionListener {
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
private JLabel label1 = new JLabel("Message:");
private JLabel label2 = new JLabel("Image:");
private JTextField textField1 = new JTextField(20);
private JTextField textField2 = new JTextField(20);
private JButton button1 = new JButton("Send Message");
private JButton button2 = new JButton("Send Image");
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JTextArea textArea = new JTextArea(10, 30);
private JScrollPane scrollPane = new JScrollPane(textArea);
public Client() {
super("Client");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(new GridLayout(2, 2));
panel1.add(label1);
panel1.add(textField1);
panel1.add(label2);
panel1.add(textField2);
panel2.setLayout(new GridLayout(2, 1));
panel2.add(button1);
panel2.add(button2);
panel3.setLayout(new BorderLayout());
panel3.add(panel1, BorderLayout.CENTER);
panel3.add(panel2, BorderLayout.EAST);
add(scrollPane, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
setVisible(true);
try {
socket = new Socket("localhost", 5000);
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
textArea.append("Connected to server\n");
} catch (IOException e) {
textArea.append("Failed to connect to server\n");
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String message = textField1.getText();
try {
output.writeUTF(message);
output.flush();
textField1.setText("");
} catch (IOException ex) {
textArea.append("Failed to send message\n");
}
} else if (e.getSource() == button2) {
String filename = textField2.getText();
try {
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
fis.read(buffer);
fis.close();
output.writeInt(buffer.length);
output.write(buffer);
output.flush();
textField2.setText("");
} catch (IOException ex) {
textArea.append("Failed to send image\n");
}
}
}
public static void main(String[] args) {
new Client();
}
}
```
服务端代码:
```java
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Server extends JFrame implements ActionListener {
private ServerSocket serverSocket = null;
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
private JLabel label1 = new JLabel("Message:");
private JLabel label2 = new JLabel("Image:");
private JTextField textField1 = new JTextField(20);
private JTextField textField2 = new JTextField(20);
private JButton button1 = new JButton("Send Message");
private JButton button2 = new JButton("Send Image");
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JTextArea textArea = new JTextArea(10, 30);
private JScrollPane scrollPane = new JScrollPane(textArea);
public Server() {
super("Server");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(new GridLayout(2, 2));
panel1.add(label1);
panel1.add(textField1);
panel1.add(label2);
panel1.add(textField2);
panel2.setLayout(new GridLayout(2, 1));
panel2.add(button1);
panel2.add(button2);
panel3.setLayout(new BorderLayout());
panel3.add(panel1, BorderLayout.CENTER);
panel3.add(panel2, BorderLayout.EAST);
add(scrollPane, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
setVisible(true);
try {
serverSocket = new ServerSocket(5000);
socket = serverSocket.accept();
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
textArea.append("Client connected\n");
} catch (IOException e) {
textArea.append("Failed to start server\n");
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String message = textField1.getText();
try {
output.writeUTF(message);
output.flush();
textField1.setText("");
} catch (IOException ex) {
textArea.append("Failed to send message\n");
}
} else if (e.getSource() == button2) {
String filename = textField2.getText();
try {
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
fis.read(buffer);
fis.close();
output.writeInt(buffer.length);
output.write(buffer);
output.flush();
textField2.setText("");
} catch (IOException ex) {
textArea.append("Failed to send image\n");
}
}
}
public static void main(String[] args) {
new Server();
}
}
```
这两份代码中,我们创建了一个客户端窗口和一个服务端窗口,每个窗口中都有一个文本框和一个按钮,可以发送消息和图片。当客户端和服务端连接成功后,就可以进行通信了。
对于发送消息,我们使用了DataOutputStream的writeUTF方法,它可以将字符串以UTF-8的编码方式发送出去。对于发送图片,我们先读取图片文件的内容,然后将内容的长度和内容本身一起发送出去。
现在我们需要在服务端中加入监听器,接收客户端发送的消息和图片。以下是服务端代码的修改部分:
```java
public class Server extends JFrame implements ActionListener {
// ...
public Server() {
// ...
new Thread(() -> {
while (true) {
try {
int type = input.readInt();
if (type == 1) {
String message = input.readUTF();
textArea.append("Client: " + message + "\n");
} else if (type == 2) {
int length = input.readInt();
byte[] buffer = new byte[length];
input.readFully(buffer);
ImageIcon icon = new ImageIcon(buffer);
JLabel label = new JLabel(icon);
textArea.insertComponent(label);
}
} catch (IOException e) {
textArea.append("Connection lost\n");
break;
}
}
}).start();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String message = textField1.getText();
try {
output.writeInt(1);
output.writeUTF(message);
output.flush();
textField1.setText("");
} catch (IOException ex) {
textArea.append("Failed to send message\n");
}
} else if (e.getSource() == button2) {
String filename = textField2.getText();
try {
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
fis.read(buffer);
fis.close();
output.writeInt(2);
output.writeInt(buffer.length);
output.write(buffer);
output.flush();
textField2.setText("");
} catch (IOException ex) {
textArea.append("Failed to send image\n");
}
}
}
// ...
}
```
我们在服务端中添加了一个线程,用来监听客户端发送的消息和图片。每当接收到消息或图片时,就在文本域中显示出来。对于接收到的图片,我们使用了JLabel和ImageIcon将其显示在文本域中。
现在我们就完成了一个简单的互联客户端和服务端的程序。当程序运行起来后,可以在客户端中输入消息或选择图片文件,然后点击按钮发送给服务端,服务端会将其显示出来。