Java使用UDP聊天程序

主要想测试Java UDP通信。Java UDP使用DatagramSocket和DatagramPacket完成UDP通信

主要思路:

1.本机通信,ip地址为:127.0.0.1

2.开一个线程监听一个端口

3.向端口发送消息

4.运行程序java ChatWindow desport listenport

ChatWindow.java

  1 import java.io.IOException;
  2 import java.net.DatagramPacket;
  3 import java.net.DatagramSocket;
  4 import java.net.InetAddress;
  5 import java.net.InetSocketAddress;
  6 import java.util.Scanner;
  7 
  8 /**
  9  * 聊天小程序
 10  * 使用UDP进行通信
 11  * 开一个线程,监听一个端口,接收消息
 12  * 这里主要用于本机通信,IP为127.0.0.1
 13  * 
 14  * 发送消息正常,接收消息没反应
 15  * @author GXF
 16  *
 17  */
 18 public class ChatWindow {
 19     private int listenPort;                    //监听端口
 20     private int desPort;                    //目标端口
 21     
 22     /**
 23      * 初始化目的端口和监听端口
 24      * @param desPort
 25      * @param listenPort
 26      */
 27     public ChatWindow(int desPort, int listenPort){
 28         this.desPort = desPort;
 29         this.listenPort = listenPort;
 30         
 31         //启动监听线程
 32         Thread listenMessageThread = new Thread(new ReceiveMessage());
 33         listenMessageThread.start();
 34         
 35         System.out.println("程序已启动,愉快的开始聊天吧!");
 36         //接收用户输入的消息
 37         Scanner scanner = new Scanner(System.in);
 38         while(true){
 39             String message = scanner.nextLine();
 40             sendMessag(message);
 41         }
 42     }
 43     
 44     /**
 45      * 向目的端口发送一个消息
 46      * @param message
 47      */
 48     public void sendMessag(String message){
 49         //初始化一个socket
 50         //设置好数据包
 51         //发送消息
 52         try {
 53             DatagramSocket socketToSendMessage = new DatagramSocket();
 54             byte buff[] = message.getBytes();
 55             DatagramPacket packetToSend = new DatagramPacket(buff, buff.length, InetAddress.getByName("127.0.0.1"), desPort);
 56             socketToSendMessage.send(packetToSend);                                         //发送消息
 57             socketToSendMessage.close();                                                    //关闭socket
 58         } catch (Exception e) {
 59             e.printStackTrace();
 60         }
 61     }
 62     
 63     
 64     /**
 65      * 监听端口线程
 66      * 实例化一个DatagramSocket,并绑定一个端口进行监听
 67      * @author GXF
 68      *
 69      */
 70     class ReceiveMessage implements Runnable{
 71         private DatagramSocket listenSocket;
 72         private DatagramPacket receivePacket;
 73         private byte buff[] = new byte[1024];
 74         @Override
 75         public void run() {
 76             //init
 77             init();
 78             //start 监听
 79             while(true){
 80                 try {
 81                     listenSocket.receive(receivePacket);                    //接收数据包
 82                     String receiveMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
 83                     System.out.println(receiveMessage);                        //输出接收到的消息
 84                 } catch (IOException e) {
 85                     e.printStackTrace();
 86                 }
 87             }
 88             
 89         }
 90         
 91         /**
 92          * 对socket,端口,进行初始化
 93          */
 94         public void init(){
 95             try {                
 96                 InetSocketAddress serverSocketAddress = new InetSocketAddress(listenPort);
 97                 listenSocket = new DatagramSocket(serverSocketAddress);    
 98                 receivePacket = new DatagramPacket(buff, buff.length);                
 99             } catch (Exception e) {
100                 e.printStackTrace();
101             }
102         }
103     }
104 
105     public static void main(String[] args) {
106         if(args.length != 2)
107         {
108             System.out.println("usage: java ChatWindow desport listenport");
109             return;
110         }
111         int desport = Integer.valueOf(args[0]);
112         int listenPort = Integer.valueOf(args[1]);
113         //first is desport, seconde is listenport
114         ChatWindow chatWindow = new ChatWindow(desport, listenPort);
115         
116 
117     }
118 
119 }

 

posted on 2015-11-02 09:42  luckygxf  阅读(674)  评论(0编辑  收藏  举报

导航