📄 client.java
字号:
package socket;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class client extends JFrame implements ActionListener, Runnable {
JTextArea ta;
JButton b1;
JTextField tf;
JTabbedPane tp;
static String serverip;
Socket cs;
BufferedReader in;
PrintWriter out;
JLabel ln;
OutputStreamWriter outp;
public client() {
super("Client");
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(serverip);
pn.add(ln);
b1 = new JButton("发送");
b1.addActionListener(this);
tf = new JTextField(18);
ps.add(tf);
ps.add(b1);
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);
Thread thread = new Thread(this);
thread.start();
}
public static void main(String[] args) {
serverip = JOptionPane.showInputDialog(null, "请输入服务器IP");
if (serverip == null) {} else {
client csmode = new client();
}
}
public void actionPerformed(ActionEvent e) {
String s = tf.getText();
out.println("client说:" + s);
out.flush();
tf.setText("");
}
public void run() {
while (true) {
try {
cs = new Socket(serverip, 5550);
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("sendfile:")) {
int send = JOptionPane.showConfirmDialog(null,
"服务器传来文件");
if (send == 0) {
out.println("clientok:");
JFileChooser file = new JFileChooser(".");
int go = file.showSaveDialog(null);
String name = file.getSelectedFile() + "";
if (go == 0) {
outp = new OutputStreamWriter(new
FileOutputStream(name));
}
out.flush();
} else {
out.println("clientshop");
out.flush();
}
}
if (str.substring(0, 9).equals("filebody:")) {
String filebody = str.substring(9, str.length());
outp.write(filebody);
outp.close();
} else {
ta.setText(ta.getText() + str + "\n");
}
}
} catch (Exception ex1) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -