⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 Java Socket 实例:C/S结构的文本文件传输
💻 JAVA
字号:
package socket;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class server extends JFrame implements ActionListener {
    JTextArea ta;
    JButton b1;
    JButton file;
    JTextField tf;
    JTabbedPane tp;
    ServerSocket ss;
    Socket cs;
    BufferedReader in;
    PrintWriter out;
    static String ip;
    String line = null;
    String filebody = null;
    JLabel ln;
    public server() {
        super("Server");
        this.setSize(400, 300);
        this.setLocation(200, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pc = new JPanel();
        JPanel ps = new JPanel();
        JPanel pn = new JPanel();
        ln = new JLabel(ip);
        pn.add(ln);
        b1 = new JButton("发送");
        file = new JButton("传送文件");
        b1.addActionListener(this);
        file.addActionListener(this);
        tf = new JTextField(18);
        ps.add(tf);
        ps.add(b1);
        ps.add(file);
        ta = new JTextArea(30, 30);
        ta.setEnabled(false);
        tp = new JTabbedPane();
        tp.add("内容", new JScrollPane(ta));
        this.getContentPane().add(tp, "Center");
        this.getContentPane().add(ps, "South");
        this.getContentPane().add(pn, "North");
        this.setVisible(true);
        try {
            ss = new ServerSocket(5550);
        } catch (Exception ex) {
        } while (true) {
            try {
                cs = ss.accept();
                in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
                out = new PrintWriter(cs.getOutputStream(), true);

                String str = null;
                while ((str = in.readLine()) != null) {
                    if (str.substring(0, 9).equals("clientok:")) {
                        JOptionPane.showMessageDialog(null, "客户端已确认接收文件");
                        out.println("filebody:" + filebody);
                        out.flush();
                    }
                    if (str.substring(0, 10).equals("clientshop")) {
                        JOptionPane.showMessageDialog(null, "客户端拒绝接收文件");
                    } else {
                        ta.setText(ta.getText() + str + "\n");
                    }
                }

            } catch (Exception ex1) {
            }
        }
    }

    public static void main(String[] args) {
        try {
            ip = InetAddress.getLocalHost().toString();
        } catch (UnknownHostException ex) {
        }

        server csmode = new server();

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            String s = tf.getText();
            out.println("server说:" + s);
            out.flush();
            tf.setText("");
        }
        if (e.getSource() == file) {
            JFileChooser filesend = new JFileChooser(".");
            int go = filesend.showOpenDialog(null);
            String name = filesend.getSelectedFile() + "";
            if (go == 0) {
                try {
                    BufferedReader inp = new BufferedReader(new FileReader(name));

                    while ((line = inp.readLine()) != null) {
                        filebody = filebody + line;

                    }
                    
                    out.println("sendfile:");
                    out.flush();
                } catch (Exception ex) {
                }

            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -