用JAVA写一个客户端和一个服务端可以分别在相同的具有俩个按钮的,可以发送图片和文字的页面组件中
时间: 2024-02-01 09:14:59 浏览: 85
用Java实现客户端和服务器传输
以下是一个简单的Java客户端和服务端代码,可以在相同的GUI页面上发送文本和图片。
服务端代码:
```java
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Server {
private static final int PORT = 8080;
private ServerSocket serverSocket;
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
}
public void start() throws IOException {
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getRemoteSocketAddress());
// Read message type
InputStream in = socket.getInputStream();
int messageType = in.read();
// Handle text message
if (messageType == 1) {
byte[] buffer = new byte[1024];
int len = in.read(buffer);
String message = new String(buffer, 0, len);
System.out.println("Received text message: " + message);
}
// Handle image message
if (messageType == 2) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
byte[] imageBytes = out.toByteArray();
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
System.out.println("Received image: " + image);
}
socket.close();
}
}
public static void main(String[] args) {
try {
Server server = new Server();
System.out.println("Server started on port " + PORT);
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Client {
private static final String HOST = "localhost";
private static final int PORT = 8080;
private Socket socket;
private JTextField textField;
public Client() throws IOException {
socket = new Socket(HOST, PORT);
}
public void start() {
JFrame frame = new JFrame("Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField(20);
JPanel buttonPanel = new JPanel();
JButton textButton = new JButton("Send Text");
textButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
OutputStream out = socket.getOutputStream();
out.write(1); // Message type: text
out.write(textField.getText().getBytes());
out.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
buttonPanel.add(textButton);
JButton imageButton = new JButton("Send Image");
imageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedImage image = ImageIO.read(getClass().getResourceAsStream("image.png"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "png", out);
byte[] imageBytes = out.toByteArray();
OutputStream outStream = socket.getOutputStream();
outStream.write(2); // Message type: image
outStream.write(imageBytes);
outStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
buttonPanel.add(imageButton);
frame.add(textField, BorderLayout.NORTH);
frame.add(buttonPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
Client client = new Client();
System.out.println("Client connected to server");
client.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
此代码演示了如何在Java中创建一个客户端和服务端,使它们能够在同一个GUI界面上发送文本和图片。对于发送文本信息,客户端将文本框中的文本发送到服务器。对于发送图像信息,客户端从磁盘中读取一张图片,并将其编码为PNG格式,然后将其字节发送到服务器。服务器将消息类型读入套接字,然后根据类型读取文本或图像数据。当服务器收到图像数据时,它将其解码为一个BufferedImage对象。
阅读全文