聊天小程序

以前用linux c实现过简单的聊天程序,但毕竟用linux的人不多,只能自己ping自己。最近用java实现了,其中主要用到了java的网络编程,GUI编程,IO编程。整个程序总共有三个线程,线程1为界面程序+客户端,线程2为服务器,线程3为接收发来消息的方法。程序启动后,线程1启动,随之线程2启动,当建立连接后,线程3启动。当然这只是最初的版本,有待改进。以后最重要的改进还是对于多人聊天的并发处理。此程序用法很简单,内网和外网都适用,(端口号我设定的是1080,一些用路由器的朋友要把自己的内网ip映射到外网ip,具体做法参考下方有图示), 不多废话啦,看程序吧。。。

 

先看两张截图:

 

路由器分配的为内网IP(192.168.1.XXX),如果想被外网IP访问,必须要把你的内网IP映射到外网IP。(如果想访问别人,则无需设置)首先登陆你的路由器设置页面(浏览器中输入192.168.1.1),然后选择转发规则进行设置:端口号设置为1080,IP设置为本机的内网IP。

一下为程序源代码,注释有点少,有疑问或者更好建议的朋友请给我留言。

Code:
  1. import javax.swing.*;  
  2.   
  3. import java.awt.event.*;  
  4. import javax.swing.border.Border;  
  5. import javax.swing.border.LineBorder;  
  6. import javax.swing.text.BadLocationException;  
  7.   
  8. import java.io.*;  
  9. import java.net.*;  
  10. import java.awt.event.*;  
  11. import java.awt.*;  
  12. import java.util.*;  
  13. import java.text.*;  
  14.   
  15.   
  16. public class SeekYou {  
  17.     public static void main(String[] args){  
  18.         Recv r = new Recv();  
  19.         new Thread(r).start();  
  20.     }  
  21. }  
  22.   
  23. class Recv implements Runnable{  
  24.     InterFace inf = new InterFace();  
  25.     public void run(){  
  26.         try {  
  27.             ServerSocket ss = new ServerSocket(1080);  
  28.             Socket s = ss.accept();  
  29.             inf.connectInformation(s);    
  30.               
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.   
  35.     }  
  36. }  
  37.   
  38. class InterFace implements Runnable{  
  39. //  网络连接的套接字  
  40.     Socket sock = null;  
  41.     InputStream is = null;  
  42.     OutputStream os = null;  
  43.       
  44. //  聊天界面的顶层容器  
  45.     JFrame jf = new JFrame("Seek You");  
  46. //  聊天界面的顶部  
  47.     JPanel p = new JPanel();  
  48.     JLabel l = new JLabel("Input His/Her IP: ");  
  49.     final JTextField tf = new JTextField(20);  
  50.     JButton bt = new JButton("Connect");      
  51. //  聊天界面的中部  
  52.     JScrollPane js = new JScrollPane();  
  53.     TextArea ta = new TextArea();   
  54.       
  55. //  聊天界面的底部  
  56.     JPanel p2 = new JPanel();  
  57.     JTextField tff = new JTextField(40);  
  58.     JButton btt = new JButton("Send");    
  59.   
  60.   
  61.     InterFace()  
  62.     {  
  63.           
  64. //      聊天界面的顶层容器  
  65.         jf.setSize(600,400);  
  66.         jf.setLocation(340,160);  
  67.         jf.setVisible(true);  
  68.           
  69. //      聊天界面的顶部  
  70.         tf.addKeyListener(new KeyAdapter(){  
  71.              public void keyPressed(KeyEvent e) {  
  72.                 if( e.getKeyChar() == KeyEvent.VK_ENTER){  
  73.                     bt.doClick();  
  74.                       
  75.                 }  
  76.              }     
  77.         });  
  78.         p.add(l);  
  79.         p.add(tf);  
  80.         p.add(bt);  
  81.           
  82.         jf.getContentPane().add(p,BorderLayout.NORTH);  
  83. //      添加indowListener  
  84.         jf.addWindowListener(new WindowAdapter(){  
  85.             public void windowClosing(WindowEvent e){  
  86.                 try {  
  87.                     if(is != null)  
  88.                         is.close();  
  89.                     if(os != null)  
  90.                         os.close();  
  91.                     if(sock != null)  
  92.                         sock.close();  
  93.                 } catch (IOException e1) {  
  94.                     e1.printStackTrace();  
  95.                 }  
  96.                 System.exit(0);   
  97.             }  
  98.         });  
  99. //      聊天界面的中部  
  100.         ta.setBackground(Color.gray);  
  101.         ta.setForeground(Color.white);  
  102.     //  js.add(ta);  
  103.         jf.getContentPane().add(ta,BorderLayout.CENTER);  
  104.   
  105. //      聊天界面的底部  
  106.           
  107.         tff.addKeyListener(new KeyAdapter(){  
  108.              public void keyPressed(KeyEvent e) {  
  109.                 if( e.getKeyChar() == KeyEvent.VK_ENTER){  
  110.                     btt.doClick();  
  111.                       
  112.                 }  
  113.              }     
  114.         });  
  115.         p2.add(tff);  
  116.         p2.add(btt);  
  117.         jf.getContentPane().add(p2,BorderLayout.SOUTH);  
  118.       
  119.           
  120.           
  121.           
  122. //      实现Send的侦听  
  123.         btt.addActionListener(new ActionListener(){  
  124.             public void actionPerformed(ActionEvent e){  
  125.                 if(sock == null){  
  126.                     String line = System.getProperty("line.separator");  
  127.                     tff.setText("");  
  128.                     ta.append("您还没有建立连接");  
  129.                     ta.append(line);  
  130.                 }  
  131.                 else{  
  132.                     String s = tff.getText();  
  133.                     tff.setText("");  
  134.                     try {  
  135.                         String line = System.getProperty("line.separator");  
  136.                         os.write(s.getBytes());  
  137.                         ta.append("Me: "+s);  
  138.                         ta.append(line);  
  139.                     } catch (Exception e1) {  
  140.                         e1.printStackTrace();  
  141.                     }  
  142.                 }  
  143.             }  
  144.         });  
  145.           
  146. //      实现connect的监听  
  147.         bt.addActionListener(new ActionListener(){  
  148.             public void actionPerformed(ActionEvent e){  
  149.                 if(sock ==null){  
  150.                     try {  
  151.                         sock = new Socket(tf.getText(),1080);  
  152.                             is = sock.getInputStream();  
  153.                             os = sock.getOutputStream();  
  154.                             String line = System.getProperty("line.separator");  
  155.                             ta.append("您已经和"+sock.getInetAddress().getHostName()+"建立连接");  
  156.                             ta.append(line);  
  157.                             recvst();//启动接收信息的进程      
  158.                         }  
  159.                       catch (Exception e1) {  
  160.                         String line = System.getProperty("line.separator");  
  161.                         ta.append("连接失败,请重新输入IP进行连接");  
  162.                         ta.append(line);  
  163.                         e1.printStackTrace();  
  164.                     }     
  165.                       
  166.                 }else{  
  167.                     String line = System.getProperty("line.separator");  
  168.                     ta.append("您已经和"+sock.getInetAddress().getHostName()+"建立连接,");  
  169.                     ta.append("重新启动可以建立其他连接");  
  170.                     ta.append(line);  
  171.                 }  
  172.             }  
  173.               
  174.         });  
  175.     }  
  176.       
  177.     final void recvst(){  
  178.         new Thread(this).start();  
  179.     }  
  180.     void connectInformation(Socket s){  
  181.         if(sock==null){  
  182.             sock = s;  
  183.             String line = System.getProperty("line.separator");  
  184.             ta.append(s.getInetAddress().getHostName()+"已经连接到你的电脑");  
  185.               
  186.             ta.append(line);      
  187.             ta.append(Calendar.getInstance().getTime()+"");  
  188.             ta.append(line);      
  189.             try {  
  190.                 is = s.getInputStream();  
  191.                 os = s.getOutputStream();  
  192.                 recvst();//启动接收信息的进程  
  193.             } catch (Exception e) {  
  194.                 e.printStackTrace();  
  195.             }  
  196.         }  
  197.         else{  
  198.             String line = System.getProperty("line.separator");  
  199.             ta.append(s.getInetAddress().getHostName()+"正在连接到你的电脑...");  
  200.             ta.append("但您已经和"+sock.getInetAddress().getHostName()+"建立连接");  
  201.             ta.append(line);  
  202.             ta.append(Calendar.getInstance().getTime()+"");       
  203.             ta.append(line);      
  204.         }  
  205.       
  206.     }  
  207.     /* 
  208.     void wordclear(){ 
  209.         String   input = ta.getText(); 
  210.         String[]   regStr   = input.split( "line.separator") ;  
  211.  
  212.         int   i   =  0; 
  213.         for(String s : regStr){i++;} 
  214.         if(i==14){ 
  215.             ta.setText(""); 
  216.         } 
  217.     } 
  218.     */  
  219.   
  220.     public void run(){  
  221.     //  new Thread(new WordClear(ta)).start();//开启文本域字数控制线程  
  222.         while(true){  
  223.             if(is!=null){  
  224.                 String line = System.getProperty("line.separator");  
  225.                 byte[] buf = new byte[100];  
  226.                 try {  
  227.                     int len = is.read(buf);  
  228.                     if(len != 0){  
  229.                         ta.append("You: "+new String(buf, 0, len));  
  230.                         ta.append(line);  
  231.                     }     
  232.                 } catch (IOException e) {  
  233.                     // TODO Auto-generated catch block  
  234.                     e.printStackTrace();  
  235.                 }  
  236.             }  
  237.         }  
  238.     }  
  239.       
  240. }  
  241.   
  242. /* 
  243. class WordClear implements Runnable{ 
  244.     TextArea ta; 
  245.     WordClear(TextArea ta){ 
  246.         this.ta = ta; 
  247.     } 
  248.  
  249.     String ss; 
  250.     public void run(){ 
  251.             while(true){ 
  252.                 System.out.println("!!!!!!!!!"); 
  253.                 String   input = ta.getText(); 
  254.                 String[]   regStr   = input.split( "/n") ;  
  255.                  
  256.                 int   i   =  0; 
  257.                 for(String s : regStr){ 
  258.                     i++; 
  259.                     if(i>16){ 
  260.                         ss = s+"/n"; 
  261.                     } 
  262.                 } 
  263.                 if(i==17){ 
  264.                     try { 
  265.                         ta.setText(ss); 
  266.                     } catch (Exception e) { 
  267.                         e.printStackTrace(); 
  268.                     } 
  269.                  
  270.                      
  271.                 } 
  272.             } 
  273.     } 
  274. } 
  275. */  

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值