Udp 多线程通信 练习

最近在看Udp通信的知识,仿照前几次TCP多线程的练习编写一个基于UDP的多线程通信

练习中分别指定两个端口的端口号

其中一个端口的程序如下:

 1 import java.net.*;
 2 import java.io.*;
 3 
 4 public class Port_one{
 5     public static void main(String[] args) throws Exception{
 6         //use the multi-thread . 
 7         //one thread response for sneding message ,in this field you should use the System.in as the inputStream
 8         //the other response for receiveing the message
 9         DatagramSocket socket = new DatagramSocket(7000);//指定端口号,用于别的端口向这里发送信息用
10         new Thread(new receiveMess(socket)).start();//起线程接收信息
11         new Thread(new sendMess(socket)).start();//起线程发送信息
12         
13         
14         
15     }
16 }
17 
18 class receiveMess implements Runnable{  //收信息线程
19     DatagramSocket socket;
20     public receiveMess(DatagramSocket socket){   //在构造函数中得到socket
21         this.socket = socket;
22     }
23     public void run(){
24         try{
25             byte[] buffer = new byte[1024];    
26             DatagramPacket packet =  new DatagramPacket(buffer,buffer.length);   //生成一个packet接收信息
27             while(true){
28                 socket.receive(packet);
29                 String str = new String(packet.getData(),0,packet.getLength());
30                 System.out.println("receiveMessage : " + str);
31             }
32         }catch(Exception e){
33             e.printStackTrace();
34         }
35         
36     }
37 }
38 
39 class sendMess implements Runnable{   //起线程发送信息
40     DatagramSocket socket;
41     public sendMess(DatagramSocket socket){
42         this.socket = socket;
43     }
44     public void run(){
45         try{
46             //从键盘输入取得需要发送的信息,通过socket发送出去
47             InputStream is = System.in;
48             byte[] buffer = new byte[1024];
49             DatagramPacket packet = null;
50             while(true){
51                 int length = is.read(buffer);
52                 packet =  new DatagramPacket(buffer,length,InetAddress.getByName("localhost"),7050);
53                 socket.send(packet);
54                 String str = new String(packet.getData(),0,packet.getLength()).trim();
55                 System.out.println("sendMessage :" + str);
56             }    
57         }catch(Exception e){
58             e.printStackTrace();
59         }
60         
61     }
62 }

另一端的代码基本和上面的一样只不过端口号需要更改一下

View Code
 1 import java.net.*;
 2 import java.io.*;
 3 
 4 public class Port_two{
 5     public static void main(String[] args) throws Exception{
 6         //use the multi-thread . 
 7         //one thread response for sneding message ,in this field you should use the System.in as the inputStream
 8         //the other response for receiveing the message
 9         DatagramSocket socket = new DatagramSocket(7050);
10         new Thread(new receiveMess2(socket)).start();
11         new Thread(new sendMess2(socket)).start();
12         
13         
14         
15     }
16 }
17 
18 class receiveMess2 implements Runnable{
19     DatagramSocket socket;
20     public receiveMess2(DatagramSocket socket){
21         this.socket = socket;
22     }
23     public void run(){
24         try{
25             byte[] buffer = new byte[1024];
26             DatagramPacket packet =  new DatagramPacket(buffer,buffer.length);
27             while(true){
28                 socket.receive(packet);
29                 String str = new String(packet.getData(),0,packet.getLength()).trim();
30                 System.out.println("receiveMessage : " + str);
31             }
32         }catch(Exception e){
33             e.printStackTrace();
34         }
35         
36     }
37 }
38 
39 class sendMess2 implements Runnable{
40     DatagramSocket socket;
41     public sendMess2(DatagramSocket socket){
42         this.socket = socket;
43     }
44     public void run(){
45         try{
46             //从键盘输入取得需要发送的信息,通过socket发送出去
47             InputStream is = System.in;
48             byte[] buffer = new byte[1024];
49             DatagramPacket packet = null;
50             while(true){
51                 int length = is.read(buffer);
52                 packet =  new DatagramPacket(buffer,length,InetAddress.getByName("localhost"),7000);
53                 socket.send(packet);
54                 String str = new String(packet.getData(),0,packet.getLength());
55                 System.out.println("sendMessage :" + str);
56             }    
57         }catch(Exception e){
58             e.printStackTrace();
59         }
60         
61     }
62 }

实验完成!

posted @ 2013-05-06 01:19  拙急鸟  阅读(1160)  评论(0编辑  收藏  举报