一、问题描述
利用socket和TCP编写,允许客户端指定一个文件名,并让服务器发回文件内容,或者指出文件不存在
二、实现代码
一、服务端
package tcp;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
private Socket socket;
private File file;
private BufferedWriter writer;
private BufferedReader reader;
Server() {
getServer();
}
public void getServer() {
try {
serverSocket = new ServerSocket(9999);
System.out.println("服务器套接字已经创建成功");
while (true) {
System.out.println("等待客户机的连接");
socket = serverSocket.accept();
System.out.println("客户机已连接");
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("等待客户机发送消息");
do {
String message;
while ((message = reader.readLine()) == null) {}
System.out.println("客户机:" + message);
handleMessage(message);
}while (true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleMessage(String message) {
String content;
try {
file = new File(message);
if (!file.exists()) {
writer.write("该文件不存在\n");
writer.newLine();
writer.flush();
}else {
BufferedReader fileReader = new BufferedReader(new FileReader(file));
while ((content = fileReader.readLine()) != null) {
writer.write(content);
writer.newLine();
writer.flush();
}
if (fileReader != null) {
fileReader.close();
}
}
writer.write("end");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
二、客户端
package tcp;
import java.io.*;
import java.net.Socket;
public class Client {
private Socket socket;
private BufferedReader reader;
private BufferedWriter writer;
public void connect() throws IOException {
socket = new Socket("127.0.0.1",9999);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void sendMessage(String message) throws IOException {
if (writer != null) {
writer.write(message);
writer.newLine();
writer.flush();
}
}
public String getContent() throws IOException {
String content;
StringBuilder builder = new StringBuilder();
if (reader == null) {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
try {
while (!("end".equals((content = reader.readLine())))) {
builder.append(content);
}
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
三、主界面
package tcp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class MainFrame extends JFrame {
private static final Client client = new Client();
private JTextField input;
private JLabel label;
private JTextArea output;
private JButton button;
MainFrame() {
setTitle("TCP测试");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(300,200,400,500);
setLayout(null);
label = new JLabel("请输入文件名称+后缀名:");
label.setFont(new Font("华文中宋 常规", Font.PLAIN,12));
label.setBounds(0,430,150,30);
getContentPane().add(label);
input = new JTextField();
input.setFont(new Font("华文中宋 常规", Font.PLAIN,14));
input.setBounds(150,430,150,30);
getContentPane().add(input);
output = new JTextArea();
output.setFont(new Font("华文中宋 常规", Font.PLAIN,13));
output.setPreferredSize(new Dimension(380,5000));
output.setLineWrap(true);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(output);
scrollPane.setBounds(0,0,380,430);
getContentPane().add(scrollPane);
button = new JButton("发 送");
button.setFont(new Font("华文中宋 常规", Font.PLAIN,14));
button.setBounds(310,430,70,30);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
new Thread(() -> {
try {
client.sendMessage(input.getText());
output.setText("");
output.append("发送完成\n");
input.setText("");
output.append(client.getContent() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
});
getContentPane().add(button);
init();
setVisible(true);
}
private void init() {
output.append("正在连接...\n");
try {
client.connect();
output.append("完成连接\n");
} catch (IOException e) {
output.append("连接失败\n");
}
}
public static void main(String[] args) {
new MainFrame();
}
}