歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

JAVA 多線程實現聊天(UDP通信 )

JAVA 多線程實現聊天(UDP通信 )
  1. /* 
  2.  * UDP實現相互傳遞消息 
  3.  *  
  4.  */  
  5. import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.net.DatagramPacket;  
  9. import java.net.DatagramSocket;  
  10. import java.net.InetAddress;  
  11.   
  12. /* 
  13.  * 客服端/發送端 
  14.  */  
  15. class Send implements Runnable{  
  16.     private DatagramPacket dPacket;  
  17.     private DatagramSocket dSocket;  
  18.     public Send(DatagramSocket dSocket)  
  19.     {  
  20.         this.dSocket=dSocket;  
  21.     }  
  22.     @Override  
  23.     public void run() {  
  24.         // TODO Auto-generated method stub   
  25.         try {  
  26.             BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));//從鍵盤讀取數據   
  27.             String string;  
  28.             while ((string=bufferedReader.readLine())!=null) {//循環讀取   
  29.                 if (string.equals("Bye"))   
  30.                 {  
  31.                 System.err.println("我下線了");  
  32.                 System.exit(0);  
  33.                   
  34.                 }  
  35.                 byte []buf=string.getBytes();//將輸入的字符串轉換為字節數組   
  36.                 /* 
  37.                  * 獲取本機IP(外網IP,我是發送到本機IP,也可以試試其它IP),然後從本機端口號為18888的端口發送出去 
  38.                  */  
  39.                 String IP=InetAddress.getLocalHost().getHostAddress().toString();  
  40.                 dPacket=new DatagramPacket(buf, buf.length, InetAddress.getByName(IP), 18888);  
  41.                 dSocket.send(dPacket);  
  42.             }  
  43.         } catch (Exception e) {  
  44.             // TODO: handle exception   
  45.             System.err.println("發送數據失敗");  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.       
  50. }  
  51. /* 
  52.  *服務器/ 接受端 
  53.  */  
  54. class Recive implements Runnable{  
  55.     private DatagramPacket dPacket;  
  56.     private DatagramSocket dSocket;  
  57.     public Recive(DatagramSocket dSocket)  
  58.     {  
  59.         this.dSocket=dSocket;  
  60.     }  
  61.     @Override  
  62.     public void run() {  
  63.         // TODO Auto-generated method stub   
  64.         while (true) {  
  65.             byte[] buf=new byte[1024];  
  66.             dPacket=new DatagramPacket(buf, buf.length);  
  67.             try {  
  68.                 dSocket.receive(dPacket);  
  69.                 String IP=dPacket.getAddress().getHostAddress();  
  70.                 String data=new String(dPacket.getData(), 0, dPacket.getLength());  
  71.                 int portString=dPacket.getPort();  
  72.                 System.out.println("IP:"+IP+"端口:"+portString+"\n"+data);  
  73.             } catch (IOException e) {  
  74.                 // TODO Auto-generated catch block   
  75.                 e.printStackTrace();  
  76.             }  
  77.               
  78.               
  79.         }  
  80.     }  
  81.       
  82. }  
  83. public class test {  
  84.   
  85.     /** 
  86.      * @param args 
  87.      */  
  88.     public static void main(String[] args) throws Exception{  
  89.         // TODO Auto-generated method stub   
  90.       
  91.       new Thread(new Send(new DatagramSocket())).start();  
  92.       
  93.       new Thread(new Recive(new DatagramSocket(18888))).start();  
  94.     }  
  95.   
  96. }  
Copyright © Linux教程網 All Rights Reserved