1 packagecn.MyNET;2
3 importjava.awt.EventQueue;4
5 importjavax.swing.JFrame;6 importjavax.swing.JPanel;7 importjavax.swing.border.EmptyBorder;8
9 importjavax.swing.JLabel;10 importjavax.swing.JTextField;11 importjavax.swing.JButton;12 importjavax.swing.JTextArea;13 importjava.awt.Color;14 importjava.awt.event.ActionListener;15 importjava.io.DataInputStream;16 importjava.io.DataOutputStream;17 importjava.io.IOException;18 importjava.net.InetAddress;19 importjava.net.ServerSocket;20 importjava.net.Socket;21 importjava.util.Date;22 importjava.awt.event.ActionEvent;23
24 public class Server3_GUI extendsJFrame {25
26 privateJPanel contentPane;27 privateJTextField textField;28 privateJTextField txtXiaoXi;29 privateJTextArea textMessage;30
31 //IO流
32 privateDataInputStream inputFromClient;33 privateDataOutputStream outputToClient;34
35 ServerSocket serverSocket;36 Socket socket;37
38 /**
39 * Launch the application.40 */
41 public static voidmain(String[] args) {42 EventQueue.invokeLater(newRunnable() {43 public voidrun() {44 try{45 Server3_GUI frame = newServer3_GUI();46 frame.setVisible(true);47 } catch(Exception e) {48 e.printStackTrace();49 }50 }51 });52 }53
54 /**
55 * Create the frame.56 */
57 publicServer3_GUI() {58 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);59 setBounds(100, 100, 450, 300);60 contentPane = newJPanel();61 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));62 setContentPane(contentPane);63 contentPane.setLayout(null);64
65 JPanel panel = newJPanel();66 panel.setBounds(0, 0, 426, 57);67 contentPane.add(panel);68 panel.setLayout(null);69
70 JLabel label = new JLabel("端口:");71 label.setBounds(12, 12, 39, 15);72 panel.add(label);73
74 textField = newJTextField();75 textField.setText("8000");76 textField.setBounds(55, 10, 50, 19);77 panel.add(textField);78 textField.setColumns(10);79
80 JButton button = new JButton("启动");81 button.setBounds(200, 7, 70, 25);82 panel.add(button);83 button.addActionListener(newbuttonListener());84
85 JButton button_1 = new JButton("停止");86 button_1.addActionListener(newActionListener() {87 public voidactionPerformed(ActionEvent e) {88 try{89 inputFromClient.close();90 outputToClient.close();91 socket.close();92 textMessage.append("连接已断开~~~");93 } catch(Exception e2) {94 //TODO: handle exception
95 }96 }97 });98 button_1.setBounds(300, 7, 70, 25);99 panel.add(button_1);100
101 JPanel panel_1 = newJPanel();102 panel_1.setBounds(0, 69, 416, 170);103 contentPane.add(panel_1);104
105 textMessage = newJTextArea();106 textMessage.setTabSize(4);107 textMessage.setRows(11);108 textMessage.setColumns(35);109 textMessage.setBackground(Color.LIGHT_GRAY);110 textMessage.setText("123");111 panel_1.add(textMessage);112
113 JPanel panel_2 = newJPanel();114 panel_2.setBounds(10, 240, 428, 89);115 contentPane.add(panel_2);116 panel_2.setLayout(null);117
118 txtXiaoXi = newJTextField();119 txtXiaoXi.addActionListener(newsendListener());120 txtXiaoXi.setBounds(12, 5, 300, 25);121 panel_2.add(txtXiaoXi);122 txtXiaoXi.setColumns(10);123
124 JButton button_2 = new JButton("发送");125 button_2.setBounds(324, 5, 70, 25);126 panel_2.add(button_2);127
128 button_2.addActionListener(newsendListener());129 }130
131 private class sendListener implementsActionListener{132 @Override133 public voidactionPerformed(ActionEvent e) {134 try{135 outputToClient.writeUTF(txtXiaoXi.getText().trim() + '\n'); //向服务器发送消息
136 textMessage.append("发送的消息:" + txtXiaoXi.getText().trim() +'\n');137 txtXiaoXi.setText(""); //输出后清空输入框
138
139 } catch(IOException e1) {140 System.err.println(e1);141 }142 }143 }144
145
146 private class buttonListener implementsActionListener{147 @Override148 public voidactionPerformed(ActionEvent e) {149 int port =Integer.parseInt(textField.getText());150 try{151 serverSocket = newServerSocket(port);152 textMessage.append("服务器已启动~~ 启动时间:" + new Date() +'\n');153
154 //监听连接请求
155 socket =serverSocket.accept();156 textMessage.append("连接成功~~~" + '\n');157 Server_thread server3_thread = newServer_thread();158 Thread thread = newThread(server3_thread);159 thread.start();160 } catch(IOException e1) {161 System.out.println(e1);162 }163 }164 }165
166 //输入输出线程
167 private class Server_thread implementsRunnable{168 publicServer_thread(){169
170 }171 public voidrun(){172 try{173 //IO流
174 inputFromClient = newDataInputStream(socket.getInputStream());175 outputToClient = newDataOutputStream(socket.getOutputStream());176 //获取客户端的名称 和 IP
177 InetAddress inetAddress =socket.getInetAddress();178 String clientName =inetAddress.getHostName();179 String clientIP =inetAddress.getHostAddress();180 while(true){181 String fromClient =inputFromClient.readUTF();182 textMessage.append("客户端" + clientName + "; " + clientIP + "发来消息: "+fromClient);183 }184 } catch(IOException e) {185 e.printStackTrace();186 }187 }188 }189 }