1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
| package mysocket2;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener;
public class Client { private String SERVER_IP="121.36.14.65"; private static int SERVER_PORT=6655; private static int SERVER_FILE_SEND_PORT=8822; private static int SERVER_FILE_RECEIVE_PORT=9911;
private JPanel sendPanel; private JButton btn_send_file; private JButton btn_download_file; private JFrame frame; private JTextArea textArea; private JTextField textField; private JTextField txt_port; private JTextField txt_hostIp; private JTextField txt_username; private JTextField txt_userpwd; private JButton btn_start; private JButton btn_stop; private JButton btn_send; private JPanel northPanel; private JPanel southPanel; private JScrollPane rightScroll; private JScrollPane leftScroll; private JSplitPane centerSplit;
private boolean isConnected = false; private Socket socket; private PrintWriter writer; private BufferedReader reader; private MessageThread messageThread;
public static void main(String[] args){ new Client(); }
public Client(){ textArea = new JTextArea(); textArea.setEditable(false); textArea.setForeground(Color.blue); textField = new JTextField(); txt_port = new JTextField("6655"); txt_hostIp = new JTextField("121.36.14.65"); txt_username = new JTextField("asd"); txt_userpwd = new JTextField("123"); btn_start = new JButton("连接"); btn_stop = new JButton("断开"); btn_send = new JButton("发送"); btn_send_file = new JButton("上传文件"); btn_download_file = new JButton("下载文件"); northPanel = new JPanel(); northPanel.setLayout(new GridLayout(1, 10)); northPanel.add(new JLabel("服务器IP")); northPanel.add(txt_hostIp); northPanel.add(new JLabel("端口")); northPanel.add(txt_port); northPanel.add(new JLabel("用户名")); northPanel.add(txt_username); northPanel.add(new JLabel("密码")); northPanel.add(txt_userpwd); northPanel.add(btn_start); northPanel.add(btn_stop); northPanel.setBorder(new TitledBorder("连接信息")); rightScroll = new JScrollPane(textArea); rightScroll.setBorder(new TitledBorder("消息显示区")); southPanel = new JPanel(new BorderLayout()); sendPanel = new JPanel(new BorderLayout()); southPanel.setBorder(new TitledBorder("写消息")); southPanel.add(textField, "Center"); sendPanel.add(btn_send, BorderLayout.NORTH); sendPanel.add(btn_send_file, BorderLayout.CENTER); sendPanel.add(btn_download_file, BorderLayout.SOUTH); southPanel.add(sendPanel, "East"); centerSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftScroll, rightScroll); centerSplit.setDividerLocation(150); frame = new JFrame("客户机"); frame.setLayout(new BorderLayout()); frame.add(northPanel, "North"); frame.add(centerSplit, "Center"); frame.add(southPanel, "South"); frame.setSize(800, 600); int screen_width = Toolkit.getDefaultToolkit().getScreenSize().width; int screen_height = Toolkit.getDefaultToolkit().getScreenSize().height; frame.setLocation((screen_width - frame.getWidth()) / 2, (screen_height - frame.getHeight()) / 2); frame.setVisible(true);
textField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { send(); } });
btn_send.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { send(); } });
btn_send_file.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sendFile(); } });
btn_download_file.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String fn = JOptionPane.showInputDialog(null,"请输入要下载的文件名:","下载文件",JOptionPane.PLAIN_MESSAGE); textArea.append("接收文件:" + fn + "\r\n"); sendMessage(frame.getTitle() + "接收文件:" + fn); ReceiveFileClientThread rfct_download = new ReceiveFileClientThread(SERVER_IP, SERVER_FILE_RECEIVE_PORT, fn); rfct_download.start(); } });
btn_start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(isConnected){ JOptionPane.showMessageDialog(frame, "已经连接!", "错误", JOptionPane.ERROR_MESSAGE); return; } int port = Integer.parseInt(txt_port.getText().trim()); String hostIp = txt_hostIp.getText().trim(); String username = txt_username.getText().trim(); String userpwd = txt_userpwd.getText().trim(); boolean flag = connectServer(port, hostIp, username, userpwd); if(flag){ frame.setTitle(username); JOptionPane.showMessageDialog(frame, "连接成功!"); } else { JOptionPane.showMessageDialog(frame, "连接失败!"); } } });
btn_stop.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!isConnected){ JOptionPane.showMessageDialog(frame, "已经断开!", "错误", JOptionPane.ERROR_MESSAGE); return; } boolean flag = closeConnection(); if(flag == false){ JOptionPane.showMessageDialog(frame, "断开失败!", "错误", JOptionPane.ERROR_MESSAGE); } JOptionPane.showMessageDialog(frame, "断开成功!"); textArea.setText(""); } });
frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { if(isConnected){ closeConnection(); } System.exit(0); } }); }
public void send() { if (!isConnected) { JOptionPane.showMessageDialog(frame, "还没有连接服务器,无法发送消息!", "错误", JOptionPane.ERROR_MESSAGE); return; } String message = textField.getText().trim(); if (message == null || message.equals("")) { JOptionPane.showMessageDialog(frame, "消息不能为空!", "错误", JOptionPane.ERROR_MESSAGE); return; } sendMessage(frame.getTitle() + ":" + message); textField.setText(""); }
public void sendFile() { JFileChooser sourceFileChooser = new JFileChooser("."); sourceFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int status = sourceFileChooser.showOpenDialog(frame); String pathname = sourceFileChooser.getSelectedFile().getPath(); File sourceFile = new File(pathname); textArea.append("发送文件:" + sourceFile.getName() + "\r\n"); sendMessage(frame.getTitle() + "发送文件:" + sourceFile.getName()); SendFileClientThread sfct = new SendFileClientThread(SERVER_IP, SERVER_FILE_SEND_PORT, pathname); sfct.start(); }
public boolean connectServer(int port, String hostIp, String username, String userpwd) { try { socket = new Socket(hostIp, port); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream()); ObjectOutputStream oos= new ObjectOutputStream(socket.getOutputStream()); User u=new User(username, userpwd); oos.writeObject(u); String login_message = reader.readLine(); textArea.append(login_message + "\r\n"); if(login_message.contains("成功")){ messageThread = new MessageThread(reader, textArea); messageThread.start(); isConnected = true; return true; } else { isConnected = false; return false; } } catch (Exception e) { textArea.append("与端口号为:" + port + " IP地址为:" + hostIp + " 的服务器连接失败!" + "\r\n"); isConnected = false; e.printStackTrace(); return false; } }
class MessageThread extends Thread{ private BufferedReader reader; private JTextArea textArea; public MessageThread(BufferedReader reader, JTextArea textArea){ this.reader = reader; this.textArea = textArea; } public void run() { String message = ""; while(true){ try { message = reader.readLine(); if(message!=null) { textArea.append(message + "\r\n"); if (message.contains("发送文件:")) { if (!message.contains(frame.getTitle())) { String[] m = message.split("发送文件:"); String fn = m[1]; int option = JOptionPane.showConfirmDialog(null, "是否接收文件" + fn + "?", "文件接收提示", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION) { textArea.append("接收文件:" + fn + "\r\n"); sendMessage(frame.getTitle() + "接收文件:" + fn); ReceiveFileClientThread rfct = new ReceiveFileClientThread(SERVER_IP, SERVER_FILE_RECEIVE_PORT, fn); rfct.start(); } } } } }catch (IOException e) { e.printStackTrace(); } } } }
public void sendMessage(String message) { writer.println(message); writer.flush(); }
public synchronized boolean closeConnection() { try { sendMessage("CLOSE"); messageThread.stop(); if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } if (socket != null) { socket.close(); } isConnected = false; return true; } catch (IOException e1) { e1.printStackTrace(); isConnected = true; return false; } }
}
|