用JAVA写一个客户端和服务端可以连接,客户端和服务端都具有页面组件,如文本域,流动条,面板,文本框,俩个按钮b1,b2.b1可以分别在客户端和服务端发信息聊天,b2可以发送图片
时间: 2024-01-17 19:05:15 浏览: 73
好的,我会给出一个简单的示例代码,帮助您实现这个功能。
首先是服务端的代码:
```java
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Server extends JFrame implements ActionListener {
private JTextField textField;
private JTextArea textArea;
private JButton button1, button2;
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public Server() {
super("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
textField = new JTextField(20);
button1 = new JButton("Send");
button2 = new JButton("Send Image");
button1.addActionListener(this);
button2.addActionListener(this);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(textField);
panel.add(button1);
panel.add(button2);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
setVisible(true);
try {
serverSocket = new ServerSocket(12345);
textArea.append("Server started.\n");
clientSocket = serverSocket.accept();
textArea.append("Client connected.\n");
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
textArea.append("Client: " + inputLine + "\n");
}
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port or listening for a connection");
System.out.println(e.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String message = textField.getText();
out.println(message);
textArea.append("Server: " + message + "\n");
textField.setText("");
} else if (e.getSource() == button2) {
// TODO: send image
}
}
public static void main(String[] args) {
new Server();
}
}
```
然后是客户端的代码:
```java
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client extends JFrame implements ActionListener {
private JTextField textField;
private JTextArea textArea;
private JButton button1, button2;
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public Client() {
super("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
textField = new JTextField(20);
button1 = new JButton("Send");
button2 = new JButton("Send Image");
button1.addActionListener(this);
button2.addActionListener(this);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(textField);
panel.add(button1);
panel.add(button2);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
setVisible(true);
try {
socket = new Socket("localhost", 12345);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
textArea.append("Server: " + inputLine + "\n");
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println("Exception caught when trying to connect to the server");
System.out.println(e.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String message = textField.getText();
out.println(message);
textArea.append("Client: " + message + "\n");
textField.setText("");
} else if (e.getSource() == button2) {
// TODO: send image
}
}
public static void main(String[] args) {
new Client();
}
}
```
这里还没有实现发送图片的功能,您可以参考Java的ImageIO类来实现。希望这个示例代码对您有所帮助!
阅读全文
相关推荐








