[Java] java在线聊天及文件传输系统3 →→→→→进入此内容的聊天室

来自 , 2020-11-17, 写在 Java, 查看 101 次.
URL http://www.code666.cn/view/678a1491
  1. package MyFrame;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.ObjectInputStream;
  9. import java.io.OutputStream;
  10. import java.net.Socket;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. import java.util.List;
  14.  
  15. import javax.swing.JOptionPane;
  16.  
  17. public class Msgthread extends Thread {
  18.  
  19.         private BufferedReader bf =null;
  20.         private Socket sock =null;
  21.         private String msg=null;
  22.         private Myframe mf;
  23.  
  24.         public Msgthread(Socket sock,Myframe mf){
  25.                 this.sock =sock;
  26.                 this.mf=mf;
  27.         }
  28.         //接收消息
  29.         public void run(){
  30.                
  31.                 try {
  32.                         bf =new BufferedReader(new InputStreamReader(this.sock.getInputStream()));
  33.                         String ms=bf.readLine();
  34.                         //解析名字
  35.                         String[] mess = ms.split("\\&",-1);
  36.                         //获取文本框原有消息
  37.                         msg=mf.getPane().getContendjt().getText();
  38.                         //接受消息
  39.                         mf.getPane().getContendjt().setText(this.sock.getInetAddress().getHostAddress()+" 说:"+mess[1]+"  ("+new SimpleDateFormat("MM-dd HH:mm:ss").format(new Date())+")\n"+this.msg);
  40.                         //增加该IP
  41.                         if(!mf.getPane().getIpjt().getSelectedItem().equals(this.sock.getInetAddress().getHostAddress())){
  42.                         mf.getPane().getIpjt().addItem(this.sock.getInetAddress().getHostAddress());
  43.                         }
  44.                         //获得接受IP
  45.                         mf.getPane().getIpjt().setSelectedItem(this.sock.getInetAddress().getHostAddress());
  46.                        
  47.                 }
  48.                 catch (Exception e) {
  49.                         // TODO Auto-generated catch block
  50.                         JOptionPane.showMessageDialog(mf, "系统繁忙请稍后再试!");
  51.                         return;
  52.                 }finally{
  53.                         if(bf!=null){
  54.                                 try {
  55.                                         bf.close();
  56.                                 } catch (IOException e) {
  57.                                         // TODO Auto-generated catch block
  58.                                         e.printStackTrace();
  59.                                 }
  60.                         }
  61.                         if(sock!=null){
  62.                                 try {
  63.                                         sock.close();
  64.                                 } catch (IOException e) {
  65.                                         // TODO Auto-generated catch block
  66.                                         e.printStackTrace();
  67.                                 }
  68.                         }
  69.                 }
  70.                
  71.                
  72.         }
  73.        
  74.        
  75.         //接收文件
  76.         public void FileWrite(){
  77.  
  78.                
  79.                
  80.                
  81.        
  82.         }
  83.        
  84.        
  85.         public String getMsg() {
  86.                 return msg;
  87.         }
  88.         public void setMsg(String msg) {
  89.                 this.msg = msg;
  90.         }
  91.        
  92. }
  93. //源代码片段来自云代码http://yuncode.net
  94.                        

回复 "java在线聊天及文件传输系统3"

这儿你可以回复上面这条便签

captcha