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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李明伟 中级黑马   /  2012-3-10 14:37  /  1590 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李明伟 于 2012-3-10 14:39 编辑

DatagramSocket  ds=new DatagramSocket(10000);
                byte[] buf=new byte[1024];
                DatagramPacket dg=new DatagramPacket(buf, buf.length);
                ds.receive(dg);
                System.out.println(new String(dg.getData(),0,dg.getData().length));
                ds.close();

上述代码中我创建了一个字节数组,并制定了大小,但是当我接收数据时,接收的数据的字节数远远小于指定字节数组的大小,
输出的时候,输出的字节长度并不是接收到的数据的字节长度,而是自己定义的字节数组的长度,
怎么样才可以使得输出的字节长度与接受的字节数一样呢???

3 个回复

倒序浏览
在new打印出来的String的时候并不需要dg.getData().length,因为dg.getData()获得的是你接收或者是发送的byte数组,再获得长度就是buf的长度,所以你打印出的是“输出的字节长度并不是接收到的数据的字节长度,而是自己定义的字节数组的长度”,而不是你接收数据的长度。你需要调用dg.getLength()方法即可。

getData()

Returns the data buffer. The data received or the data to be sent starts from the offset in the buffer, and runs for length long.

Returns:
the buffer used to receive or send data


getLength()

Returns the length of the data to be sent or the length of the data received.

Returns:
the length of the data to be sent or the length of the data received.

评分

参与人数 1技术分 +1 收起 理由
房宝彬 + 1

查看全部评分

回复 使用道具 举报
冯旭君 发表于 2012-3-10 15:06
在new打印出来的String的时候并不需要dg.getData().length,因为dg.getData()获得的是你接收或者是发送的byt ...

恩  是的
回复 使用道具 举报
这个例子你看下

实例:利用数据报通信的C/S程序
示例12-10给出了一个简单的利用数据报通信的客户端程序,它能够完成与服务器简单的通信。
【程序源代码】

1 // ==================== Program Description ===================
2 // 程序名称:示例12-10: UDPServer.java
3 // 程序目的:创建UDP服务器
4 //=============================================================
5 import java.net.*;
6 import java.io.*;
7
8 public class UDPServer
9 {
10 static public void main(String args[])
11 {
12 try {
13 DatagramSocket receiveSocket = new DatagramSocket(5000);
14 byte buf[]=new byte[1000];
15 DatagramPacket receivePacket=new DatagramPacket(buf,buf.length);
16 System.out.println("startinig to receive packet");
17 while (true)
18 {
19 receiveSocket.receive(receivePacket);
20 String name=receivePacket.getAddress().toString();
21 System.out.println("\n来自主机:"+name+"\n端口:"
22 +receivePacket.getPort());
23 String s=new
String(receivePacket.getData(),0,receivePacket.getLength());
24 System.out.println("the received data: "+s);
25 }
26 }
27 catch (SocketException e) {
28 e.printStackTrace();
29 System.exit(1);
30 }
31 catch(IOException e) {
32 System.out.println("网络通信出现错误,问题在"+e.toString());
33 }
34 }
35 }



【程序输出结果】

startinig to receive packet
来自主机:/166.111.172.20
端口:3456
the received data: hello! this is the client



【程序注解】
第13行和第15行分别实例化了一个DatagramSocket对象receiveSocket和一个DatagramPacket对象receivePacket,都是通过调用各自的构造函数实现的,为建立服务器做好准备。在while这个永久循环中,receiveSocket这个套接字始终尝试receive()方法接收DatagramPacket数据包,当接收到数据包后,就调用DatagramPacket的一些成员方法显示一些数据包的信息。在程序中调用了getAddress()获得地址,getPort()方法获得客户端套接字的端口,getData()获得客户端传输的数据。注意getData( )返回的是字节数组,我们把它转化为字符串显示。在第27~33行我们对程序中发生的SocketException和IOException异常进行了处理。
示例12-11是UDP客户端的程序。
【程序源代码】

1 // ==================== Program Description ===================
2 // 程序名称:示例12-11: UDPClient.java
3 // 程序目的:创建UDP客户端
4 //=============================================================
5 import java.net.*;
6 import java.io.*;
7
8 public class UDPClient
9 {
10 public static void main(String args[])
11 {
12 try {
13 DatagramSocket sendSocket=new DatagramSocket(3456);
14 String string="asfdfdfggf";
15 byte[] databyte=new byte[100];
16 databyte=string.getBytes();
17 DatagramPacketsendPacket=new
DatagramPacket(databyte,string.length(),
18 InetAddress.getByName("163.121.139.20"),
5000);
19 sendSocket.send(sendPacket);
20 System.out.println("send the data: hello ! this is the client");
21 }
22 catch (SocketException e) {
23 System.out.println("不能打开数据报Socket,或数据报Socket无法与指定
24 端口连接!");
25 }
26 catch(IOException ioe) {
27 System.out.println("网络通信出现错误,问题在"+ioe.toString());
28 }
29 }
30 }



【程序输出结果】
send the data: hello !this is the clientsend the data: hello !this is the client

【程序注解】
第13行用DatagramSocket的构造函数实例化一个发送数据的套接字sendSocket。第17~18行实例化了一个DatagramPacket,其中数据包要发往的目的地是163.121.139.20,端口是5000。当构造完数据包后,就调用send( )方法将数据包发送出去。

评分

参与人数 1技术分 +2 收起 理由
职业规划-刘倩老师 + 2 赞一个!

查看全部评分

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