A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 汪洋大海 中级黑马   /  2013-11-29 02:44  /  1237 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

各位同学们,你们好,我想问一下,一个程序可以设置多个接收端口吗?如果可以的设置多个端口,如何和多线程配合使用呢?
如果不可以,是为什么呢?
最好可以配上简单的代码,谢谢了哟。

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1 淡定

查看全部评分

2 个回复

倒序浏览
服务器代码
  1. public class UdpServer {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) throws Exception{
  6.                 // TODO Auto-generated method stub
  7.                
  8.                 //开启一个线程,监听端口10000的数据包
  9.                 Executors.newSingleThreadExecutor().execute(
  10.                                 new Runnable(){
  11.                                         public void run(){
  12.                                                 try {
  13.                                                         DatagramSocket ds = new DatagramSocket(10000);
  14.                                                         byte[] buf = new byte[1024];
  15.                                                         int length = 0;
  16.                                                         DatagramPacket dp = new DatagramPacket(buf, 1024);
  17.                                                         while (true) {
  18.                                                                 ds.receive(dp);
  19.                                                                 System.out.println(dp.getAddress()+"::"+dp.getLength()+":"+new String(dp.getData(), 0, dp.getLength()));
  20.                                                         }
  21.                                                 } catch (Exception e) {
  22.                                                         // TODO: handle exception
  23.                                                         e.printStackTrace();
  24.                                                 }
  25.                                         }
  26.                                 });
  27.                 //开启一个线程,监听端口10001的数据包
  28.                 Executors.newSingleThreadExecutor().execute(
  29.                                 new Runnable(){
  30.                                         public void run(){
  31.                                                 try {
  32.                                                         DatagramSocket ds = new DatagramSocket(10001);
  33.                                                         byte[] buf = new byte[1024];
  34.                                                         int length = 0;
  35.                                                         DatagramPacket dp = new DatagramPacket(buf, 1024);
  36.                                                         while (true) {
  37.                                                                 ds.receive(dp);
  38.                                                                 System.out.println(dp.getAddress()+"::"+dp.getLength()+":"+new String(dp.getData(), 0, dp.getLength()));
  39.                                                         }
  40.                                                 } catch (Exception e) {
  41.                                                         // TODO: handle exception
  42.                                                         e.printStackTrace();
  43.                                                 }
  44.                                         }
  45.                                 });
  46.         }

  47. }
复制代码

客户端代码
  1. public class UdpClient {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                
  8.                 //开启一个线程,向本机的10000端口发送数据包
  9.                 Executors.newSingleThreadExecutor().execute(
  10.                                 new Runnable(){
  11.                                         public void run(){
  12.                                                 try {
  13.                                                         DatagramSocket ds = new DatagramSocket();
  14.                                                         byte[] buf = "THIS IS NO.1".getBytes();
  15.                                                         InetAddress ia = InetAddress.getLocalHost();
  16.                                                         DatagramPacket dp = new DatagramPacket(buf, buf.length, ia, 10000);
  17.                                                         while (true) {
  18.                                                                 ds.send(dp);
  19.                                                                 try {
  20.                                                                         Thread.sleep(1000);
  21.                                                                 } catch (Exception e) {
  22.                                                                         // TODO: handle exception
  23.                                                                 }
  24.                                                         }
  25.                                                 } catch (Exception e) {
  26.                                                         // TODO: handle exception
  27.                                                         e.printStackTrace();
  28.                                                 }
  29.                                         }
  30.                                 });
  31.                 //开启一个线程,向本机的10001端口发送数据包
  32.                 Executors.newSingleThreadExecutor().execute(
  33.                                 new Runnable(){
  34.                                         public void run(){
  35.                                                 try {
  36.                                                         DatagramSocket ds = new DatagramSocket();
  37.                                                         byte[] buf = "this is No.2".getBytes();
  38.                                                         InetAddress ia = InetAddress.getLocalHost();
  39.                                                         DatagramPacket dp = new DatagramPacket(buf, buf.length, ia, 10001);
  40.                                                         while (true) {
  41.                                                                 ds.send(dp);
  42.                                                                 try {
  43.                                                                         Thread.sleep(1000);
  44.                                                                 } catch (Exception e) {
  45.                                                                         // TODO: handle exception
  46.                                                                 }
  47.                                                         }
  48.                                                 } catch (Exception e) {
  49.                                                         // TODO: handle exception
  50.                                                         e.printStackTrace();
  51.                                                 }
  52.                                         }
  53.                                 });
  54.         }

  55. }
复制代码
功能:客户端想服务端的不同端口发送请求


评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1 很给力!

查看全部评分

回复 使用道具 举报
谢谢你了。原来是这样呀。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马