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

© 何超 中级黑马   /  2013-11-30 20:21  /  1671 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 何超 于 2013-11-30 20:53 编辑

发送端数据
  1. public class UdpSend2 {

  2.         /**
  3.          * @param args
  4.          * @throws IOException
  5.          */
  6.         public static void main(String[] args)
  7.         {
  8.                 // TODO Auto-generated method stub
  9.                
  10.                 BufferedReader br=
  11.                                 new BufferedReader(new InputStreamReader(System.in));
  12.         
  13.                 try {
  14.                         DatagramSocket ds=new DatagramSocket();
  15.                         String str;
  16.                         while ((str = br.readLine()) != null)
  17.                         {
  18.                                 byte[] buf = str.getBytes();
  19.                                 DatagramPacket dp = new DatagramPacket(buf,
  20.                                                         buf.length,
  21.                                                         InetAddress.getByName("169.254.246.95)"),
  22.                                                         10066);        
  23.                                 ds.send(dp);
  24.                                 
  25.                         }
  26.                         ds.close();
  27.                 } catch (BindException e)
  28.                 {
  29.                         System.out.println("端口号已被占用");
  30.                 }        catch(IOException e1)
  31.                 {
  32.                         System.out.println("读取数据失败");
  33.                 }
  34.                 try {
  35.                         br.close();
  36.                 } catch (IOException e2) {
  37.                         // TODO Auto-generated catch block
  38.                         e2.printStackTrace(System.out);
  39.                         System.out.println("关闭读取流失败");
  40.                 }
  41.         }

  42. }
复制代码

4 个回复

倒序浏览
接受端程序
  1. import java.io.IOException;
  2. import java.net.*;


  3. public class UdpRece2 {

  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args)
  8.         {
  9.                 // TODO Auto-generated method stub
  10.                 try {
  11.                         DatagramSocket ds=new DatagramSocket(10066);
  12.                         while(true)
  13.                         {
  14.                         byte[] buf=new byte[1024];
  15.                         DatagramPacket dp=new DatagramPacket(buf,buf.length);
  16.                         ds.receive(dp);
  17.                         String data=new String(dp.getData(),0,dp.getLength());
  18.                         String ip=dp.getAddress().getHostAddress();
  19.                         int port=dp.getPort();
  20.                         System.out.println("IP"+ip);
  21.                         System.out.println("缓冲区数据是"+data);
  22.                         System.out.println("端口号"+port);
  23.                         }
  24.                 } catch (SocketException e) {
  25.                         // TODO Auto-generated catch block
  26.                         e.printStackTrace();
  27.                         System.out.println("端口连接失败");
  28.                 }catch(IOException e)
  29.                 {
  30.                         e.printStackTrace();
  31.                         System.out.println("IO出现异常");
  32.                 }
  33.                
  34.         }

  35. }
复制代码
回复 使用道具 举报
不管是在Eclipse还是cmd上 都是两边全处于阻塞状态  我是找不到错误了···眼睛都看疼了···{:soso_e136:}
回复 使用道具 举报
接收端里的while循环是干嘛用的,我觉得不必要
回复 使用道具 举报
本帖最后由 25343215 于 2013-11-30 20:59 编辑

楼主的代码我修改了三个地方。后面有运行结果
都是在发送端,首先是BufferedReader的定义,还有String str=null,最后一个问题是客户端发送的数据包Ip地址里面,有错误。
具有见代码。


服务器端
  1. import java.io.*;
  2. import java.util.*;
  3. import java.net.*;



  4. public class UdpRece2 {

  5.         /**
  6.          * @param args
  7.          */
  8.         public static void main(String[] args)
  9.         {
  10.                 // TODO Auto-generated method stub
  11.                 try {
  12.                         DatagramSocket ds=new DatagramSocket(10066);
  13.                         while(true)
  14.                         {
  15.                         byte[] buf=new byte[1024];
  16.                         DatagramPacket dp=new DatagramPacket(buf,buf.length);
  17.                         ds.receive(dp);
  18.                         String data=new String(dp.getData(),0,dp.getLength());
  19.                         String ip=dp.getAddress().getHostAddress();
  20.                         int port=dp.getPort();
  21.                         System.out.println("IP"+ip);
  22.                         System.out.println("缓冲区数据是"+data);
  23.                         System.out.println("端口号"+port);
  24.                         }
  25.                 } catch (SocketException e) {
  26.                         // TODO Auto-generated catch block
  27.                         e.printStackTrace();
  28.                         System.out.println("端口连接失败");
  29.                 }catch(IOException e)
  30.                 {
  31.                         e.printStackTrace();
  32.                         System.out.println("IO出现异常");
  33.                 }
  34.                
  35.         }

  36. }
复制代码
客户端:
  1. import java.io.*;
  2. import java.util.*;
  3. import java.net.*;

  4. public class UdpSend2 {

  5.         /**
  6.          * @param args
  7.          * @throws IOException 2013/11/30
  8.          */
  9.         public static void main(String[] args)
  10.         {
  11.                                 BufferedReader br=null;
  12.                 try {

  13.                                          br=new BufferedReader(new InputStreamReader(System.in));
  14.                     DatagramSocket ds=new DatagramSocket();
  15.                     String str=null;
  16.                         while ((str = br.readLine()) != null)
  17.                         {
  18.                                 byte[] buf = str.getBytes();
  19.                                 DatagramPacket dp = new DatagramPacket(buf,
  20.                                                         buf.length,
  21.                                                         InetAddress.getByName("192.168.1.101"),
  22.                                                         10066);        
  23.                                 ds.send(dp);
  24.                                 
  25.                         }
  26.                         ds.close();
  27.                 } catch (BindException e)
  28.                 {
  29.                         System.out.println("端口号已被占用");
  30.                 }        catch(IOException e1)
  31.                 {
  32.                         System.out.println("读取数据失败");
  33.                 }
  34.                 try {
  35.                         br.close();
  36.                 } catch (IOException e2) {
  37.                         // TODO Auto-generated catch block
  38.                         e2.printStackTrace(System.out);
  39.                         System.out.println("关闭读取流失败");
  40.                 }
  41.         }

  42. }
复制代码





360软件小助手截图20131130205824.jpg (32.6 KB, 下载次数: 34)

运行结果

运行结果

360软件小助手截图20131130205212.jpg (36.52 KB, 下载次数: 26)

运行结果

运行结果
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马