java简易的局域网聊天工具
Posted 薄灬荷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java简易的局域网聊天工具相关的知识,希望对你有一定的参考价值。
main方法
1 package com.lovo.work; 2 3 public class Test { 4 5 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 new FeiqiuFrame(); 10 11 12 } 13 14 }
主程序:文件中是姓名和IP,用键值对的方式写入
1 package com.lovo.work; 2 3 import java.awt.Color; 4 import java.awt.Container; 5 import java.awt.Font; 6 import java.awt.Toolkit; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.io.BufferedReader; 10 import java.io.BufferedWriter; 11 import java.io.FileInputStream; 12 import java.io.FileNotFoundException; 13 import java.io.IOException; 14 import java.io.InputStreamReader; 15 import java.io.ObjectInputStream; 16 import java.io.OutputStreamWriter; 17 import java.net.ServerSocket; 18 import java.net.Socket; 19 import java.net.UnknownHostException; 20 import java.text.SimpleDateFormat; 21 import java.util.Calendar; 22 import java.util.HashMap; 23 import java.util.Properties; 24 import java.util.Set; 25 26 import javax.swing.JButton; 27 import javax.swing.JCheckBox; 28 import javax.swing.JComboBox; 29 import javax.swing.JFrame; 30 import javax.swing.JOptionPane; 31 import javax.swing.JScrollPane; 32 import javax.swing.JTextArea; 33 import javax.swing.JTextField; 34 35 public class FeiqiuFrame extends JFrame{ 36 37 38 39 private Container contentP; 40 private JTextArea msgArea; 41 private JTextField IPTxt; 42 private JButton qingkongBtn; 43 private JTextField wenbenTxt; 44 private JButton fasongBtn; 45 private String msg; 46 private Socket fromClient; 47 private String str3; 48 private JComboBox classComb; 49 private Properties pro; 50 private JCheckBox xuanze; 51 52 53 54 55 56 public FeiqiuFrame() { 57 pro=new Properties(); 58 try { 59 pro.load(new FileInputStream("j124.properties"));// 60 } catch (FileNotFoundException e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } catch (IOException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 68 this.setSize(400, 350); 69 Toolkit tk = Toolkit.getDefaultToolkit(); 70 int screenW = (int) tk.getScreenSize().getWidth();// 得到屏幕宽 71 int screenH = (int) tk.getScreenSize().getHeight();// 得到屏幕高 72 this.setLocation((screenW - 400) / 2, (screenH - 510) / 2); 73 this.setTitle("我的飞秋"); 74 // 设置窗体关闭即为关闭程序 75 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 76 // 设置窗体内容面板上所有的东西 77 this.addContent(); 78 // 设置窗体可见 79 this.setVisible(true); 80 81 82 83 ServerSocket server = null; 84 try { 85 server = new ServerSocket(9527); 86 while (true) { 87 // accept是阻塞方法,直到监听到客户端发送过来的消息 88 Socket fromClient = server.accept(); 89 new MSGThread(fromClient,this).start(); 90 91 } 92 } catch (IOException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 } finally { 96 if (server != null) { 97 try { 98 server.close(); 99 } catch (IOException e) { 100 // TODO Auto-generated catch block 101 e.printStackTrace(); 102 } 103 } 104 } 105 106 107 } 108 109 private void addContent() { 110 // TODO Auto-generated method stub 111 this.contentP = this.getContentPane(); 112 // 设置窗体面板的背景色 113 this.contentP.setBackground(Color.WHITE); 114 // 设置容器的布局管理器为空---空布局管理 115 this.contentP.setLayout(null); 116 117 this.msgArea = new JTextArea(); 118 JScrollPane sp = new JScrollPane(this.msgArea); 119 sp.setBounds(15, 20, 365, 200); 120 msgArea.setEditable(false); 121 122 // this.msgArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 123 this.contentP.add(sp); 124 125 126 127 this.wenbenTxt = new JTextField(); 128 this.wenbenTxt.setFont(new Font("微软雅黑", Font.ITALIC,15)); 129 this.wenbenTxt.setForeground(Color.BLUE); 130 this.wenbenTxt.setBounds(15, 230, 110, 25); 131 this.contentP.add(this.wenbenTxt); 132 133 this.fasongBtn = new JButton(); 134 this.fasongBtn .setText("发送"); 135 this.fasongBtn .setFont(new Font("微软雅黑", Font.PLAIN, 15)); 136 this.fasongBtn .setForeground(Color.BLUE); 137 this.fasongBtn .setBounds(130, 230, 65, 25); 138 139 140 this.classComb = new JComboBox(this.pro.keySet().toArray()); 141 this.classComb.setEditable(true); 142 this.classComb.setBounds(210, 230,90, 25); 143 this.contentP.add(this.classComb); 144 145 146 this.xuanze = new JCheckBox("自动回复"); 147 this.xuanze.setBackground(new Color(225, 249, 255)); 148 this.xuanze.setBounds(15, 270, 100, 25); 149 this.contentP.add(this.xuanze); 150 151 152 this.qingkongBtn = new JButton(); 153 this.qingkongBtn.setText("清空"); 154 this.qingkongBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15)); 155 this.qingkongBtn.setForeground(Color.BLUE); 156 this.qingkongBtn.setBounds(310, 230, 65, 25); 157 this.contentP.add(this.qingkongBtn); 158 this.qingkongBtn.addActionListener(new ActionListener(){ 159 160 @Override 161 public void actionPerformed(ActionEvent e) { 162 // TODO Auto-generated method stub 163 msgArea.setText(""); 164 } 165 166 }); 167 this.fasongBtn.addActionListener(new ActionListener(){ 168 169 @Override 170 public void actionPerformed(ActionEvent e) { 171 // TODO Auto-generated method stub 172 String str=wenbenTxt.getText(); 173 String ipstr=(String)FeiqiuFrame.this.getClassComb().getSelectedItem(); 174 String IP=FeiqiuFrame.this.pro.getProperty(ipstr); 175 176 String str2 = str.replaceAll(" ", ""); 177 if(str2!=null&&!str2.equals("")){ 178 if(IP.matches("(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3}")){ 179 msg="XX&"+str2; 180 str3=msgArea.getText(); 181 msgArea.setText(str3+"\n"+getFormatDate()+"\n"+"我说:"+str2); 182 183 Socket client = null; 184 try { 185 client = new Socket(IP, 9527); 186 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); 187 bw.write(msg); 188 bw.flush(); 189 } catch (UnknownHostException e1) { 190 // TODO Auto-generated catch block 191 e1.printStackTrace(); 192 } catch (IOException e1) { 193 // TODO Auto-generated catch block 194 e1.printStackTrace(); 195 } finally{ 196 if(client != null){ 197 try { 198 client.close(); 199 } catch (IOException e1) { 200 // TODO Auto-generated catch block 201 e1.printStackTrace(); 202 } 203 } 204 } 205 }else{ 206 JOptionPane.showMessageDialog(fasongBtn, "IP输入不正确!"); 207 } 208 209 }else{ 210 JOptionPane.showMessageDialog(fasongBtn, "消息输入不能为空!"); 211 } 212 } 213 }); 214 215 this.contentP.add(this.fasongBtn ); 216 217 } 218 public static String getFormatDate() 219 { 220 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 221 return sdf.format(Calendar.getInstance().getTime()); 222 } 223 224 public Container getContentP() { 225 return contentP; 226 } 227 228 public void setContentP(Container contentP) { 229 this.contentP = contentP; 230 } 231 232 public JTextArea getMsgArea() { 233 return msgArea; 234 } 235 236 public void setMsgArea(JTextArea msgArea) { 237 this.msgArea = msgArea; 238 } 239 240 public JTextField getIPTxt() { 241 return IPTxt; 242 } 243 244 public void setIPTxt(JTextField iPTxt) { 245 IPTxt = iPTxt; 246 } 247 248 public JButton getQingkongBtn() { 249 return qingkongBtn; 250 } 251 252 public void setQingkongBtn(JButton qingkongBtn) { 253 this.qingkongBtn = qingkongBtn; 254 } 255 256 257 public JComboBox getClassComb() { 258 return classComb; 259 } 260 261 public void setClassComb(JComboBox classComb) { 262 this.classComb = classComb; 263 } 264 265 public JTextField getWenbenTxt() { 266 return wenbenTxt; 267 } 268 269 public void setWenbenTxt(JTextField wenbenTxt) { 270 this.wenbenTxt = wenbenTxt; 271 } 272 273 public JButton getFasongBtn() { 274 return fasongBtn; 275 } 276 277 public void setFasongBtn(JButton fasongBtn) { 278 this.fasongBtn = fasongBtn; 279 } 280 281 public String getMsg() { 282 return msg; 283 } 284 285 public void setMsg(String msg) { 286 this.msg = msg; 287 } 288 289 public Socket getFromClient() { 290 return fromClient; 291 } 292 293 public void setFromClient(Socket fromClient) { 294 this.fromClient = fromClient; 295 } 296 297 public String getStr3() { 298 return str3; 299 } 300 301 public void setStr3(String str3) { 302 this.str3 = str3; 303 } 304 305 306 }
数据的传输
1 package com.lovo.work; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.Socket; 7 import java.text.SimpleDateFormat; 8 import java.util.Calendar; 9 import java.util.Date; 10 11 public class MSGThread extends Thread{ 12 13 private Socket fromClient; 14 private FeiqiuFrame feiqiuframe; 15 16 public MSGThread(Socket fromClient,FeiqiuFrame feiqiuframe){ 17 this.fromClient = fromClient; 18 this.feiqiuframe = feiqiuframe; 19 } 20 21 public void run(){ 22 BufferedReader br = null; 23 try { 24 br = new BufferedReader(new InputStreamReader( 25 fromClient.getInputStream())); 26 String str = br.readLine(); 27 String[] allMsg = str.split("&"); 28 String str2=MSGThread.this.feiqiuframe.getMsgArea().getText(); 29 String str1=allMsg[0] + "说:" + allMsg[1]; 30 String str3=str2+"\n"+getFormatDate()+"\n"+str1; 31 MSGThread.this.feiqiuframe.setStr3(str3); 32 MSGThread.this.feiqiuframe.getMsgArea().setText(MSGThread.this.feiqiuframe.getStr3()); 33 // System.out.println(str2); 34 // System.out.println(str1); 35 // System.out.println(str3); 36 37 } catch (IOException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } finally{ 41 if(br != null){ 42 try { 43 br.close(); 44 } catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 } 49 if(this.fromClient != null){ 50 try { 51 this.fromClient.close(); 52 } catch (IOException e) { 53 // TODO Auto-generated catch block 54 e.printStackTrace(); 55 } 56 } 57 } 58 59 60 } 61 62 public static String getFormatDate() 63 { 64 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 65 return sdf.format(Calendar.getInstance().getTime()); 66 } 67 68 }
以上是关于java简易的局域网聊天工具的主要内容,如果未能解决你的问题,请参考以下文章