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

来自 , 2020-07-04, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/ffeed84c
  1. package MyFrame;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStreamWriter;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import javax.swing.JOptionPane;
  15.  
  16. public class Client {
  17.                 //默认服务器IP
  18.                 private final String I ="192.168.3.108";
  19.                 ///////////////////////////////////
  20.                 private Myframe mf;
  21.                 private String msg;
  22.                 private Socket client=null;
  23.                 private BufferedWriter bf =null;
  24.                 private String ip;
  25.                 private String port;
  26. //              private InputStream in =null;
  27. //              private ObjectOutputStream out =null;
  28. //              private byte[] b =null;
  29.                 //默认发送
  30.                 public Client(String msg,String port, String theIp){
  31.                         //我的电脑的IP地址,当需要吧自己的当终端用时使用,系统消息会全部转发到自己这
  32.                         this.ip=I;
  33.                         //测试时可使用这段,系统消息发当前电脑ip
  34.                         if(theIp != null && !"".equals(theIp)){
  35.                                 this.ip=theIp;
  36.                         }
  37.                        
  38.                         this.msg=msg;
  39.                         this.port=port;
  40.                         this.Send();
  41.                        
  42.                 }
  43.                 //正常消息发送
  44.                 public Client(Myframe mf){
  45.                         this.mf=mf;
  46.                         this.port=mf.getPane().getPortjt().getText();
  47.                         this.msg="我&"+mf.getPane().getMsgjt().getText();
  48.                         this.ip=(String) mf.getPane().getIpjt().getSelectedItem();
  49.                        
  50.                         this.Send();
  51.                 }
  52.        
  53.                 //发送
  54.                 private void Send(){
  55.                         //System.out.println("IP:"+this.ip+"端口:"+this.port+"发送:"+this.msg);
  56.                 try {
  57.                         client =new Socket(this.ip,Integer.parseInt(this.port));
  58.        
  59.                         bf =new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
  60.                         bf.write(this.msg);
  61.                        
  62.                         bf.flush();
  63.                        
  64.                 } catch (UnknownHostException e) {
  65.                         // TODO Auto-generated catch block
  66.                         JOptionPane.showMessageDialog(mf, "IP"+this.ip+"不存在,请确认...");
  67.                         mf.getPane().getMsgjt().setText("");
  68.                         return;
  69.                 } catch (IOException e) {
  70.                         // TODO Auto-generated catch block
  71.                         JOptionPane.showMessageDialog(mf, "系统繁忙,请稍后再试..");
  72.                         mf.getPane().getMsgjt().setText("");
  73.                         return;
  74.                 } catch (Exception e) {
  75.                         // TODO Auto-generated catch block
  76.                         JOptionPane.showMessageDialog(mf, "系统繁忙请稍后再试!");
  77.                         return;
  78.                 }finally{
  79.                         if(bf!=null){
  80.                                 try {
  81.                                         bf.close();
  82.                                 } catch (IOException e) {
  83.                                         // TODO Auto-generated catch block
  84.                                         e.printStackTrace();
  85.                                 }
  86.                         }
  87.                         if(client!=null){
  88.                                 try {
  89.                                         client.close();
  90.                                 } catch (IOException e) {
  91.                                         // TODO Auto-generated catch block
  92.                                         e.printStackTrace();
  93.                                 }
  94.                         }
  95.                        
  96.                        
  97.                 }
  98.                
  99.                 }
  100.                 public String getMsg() {
  101.                         return msg;
  102.                 }
  103.                 public void setMsg(String msg) {
  104.                         this.msg = msg;
  105.                 }
  106.                 public String getIp() {
  107.                         return ip;
  108.                 }
  109.                 public void setIp(String ip) {
  110.                         this.ip = ip;
  111.                 }
  112.                 public String getPort() {
  113.                         return port;
  114.                 }
  115.                 public void setPort(String port) {
  116.                         this.port = port;
  117.                 }
  118.        
  119.  
  120. }
  121. //源代码片段来自云代码http://yuncode.net
  122.                        

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

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

captcha