java.nio的DatagramChannel接收数据包问题

rijain 2005-02-28 03:44:28
在使用datagramsocket的时候,可以结合datagrampacket一起,一个一个的接收数据包。

使用DatagramChannel时候,receive方法是以bytebuffer为参数的。我想问,如果一下来了好几个数据包,怎样把它们一一读出来?

谢谢大侠!!
...全文
576 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rijain 2005-02-28
  • 打赏
  • 举报
回复
DatagramChannel的receive函数,调用一次,接收一个Datagram数据包。如果一下来多个包的话,收完第一个包后,我们remove了那个key,下一个包就会再次激发一个key的了。这样,就收到下一个包。依次完成以后的包。

代码中,应该有一句 theDatagramChannel.socket().bind(theInetSocketAddress);才对。

谢谢各位的支持。
BasaraTracy 2005-02-28
  • 打赏
  • 举报
回复
up
rijain 2005-02-28
  • 打赏
  • 举报
回复
多谢支持,,急啊。。
xu_xinyu 2005-02-28
  • 打赏
  • 举报
回复
没有研究过,支持一下。
rijain 2005-02-28
  • 打赏
  • 举报
回复
付上一段代码来说明我的问题:

public class NIOTest {

String host = "localhost";
int port = 10000;
int datagramSize = 512;
int datagramInterval = 3000;
int numMsgs = 24;
int sendValue = 10;


byte[] sendData = new byte[ datagramSize ];
ByteBuffer theSendDataByteBuffer = ByteBuffer.wrap( sendData );


// array of bytes for receiving datagrams
byte[] receiveData = new byte[ datagramSize ];
ByteBuffer theReceiveDataByteBuffer = ByteBuffer.wrap( receiveData );

NIOTest()
{
try
{

Random theRandom = new Random();
InetSocketAddress theInetSocketAddress = new InetSocketAddress( host, port);

// make a DatagramChannel
DatagramChannel theDatagramChannel = DatagramChannel.open();

theDatagramChannel.configureBlocking( false );

// instantiate a selector
Selector theSelector = Selector.open();

// register the selector on the channel to monitor reading
// datagrams on the DatagramChannel
theDatagramChannel.register( theSelector, SelectionKey.OP_READ );

long millisecsUntilSendNextDatagram = 0;
int i = 1; int j = 1;

// send and read concurrently, but do not block on read:
while (true)
{
long start = System.currentTimeMillis();

// which comes first, next send or a read?
// in case millisecsUntilSendNextDatagram <= 0 go right to send
if ( millisecsUntilSendNextDatagram <= 0 ||
theSelector.selectNow() == 0 )
{
// just for fun, send between 0 and 4 datagrams
for( int k = 0; k < theRandom.nextInt( 5 ); k++ )
{

theDatagramChannel.send( theSendDataByteBuffer, theInetSocketAddress );
System.out.println("sent Datagram " + j++ );
}
millisecsUntilSendNextDatagram = datagramInterval;
}
else
{

// Get an iterator over the set of selected keys
Iterator it = theSelector.selectedKeys().iterator( );

// will be exactly one key in the set, but iterator is
// only way to get at it
while( it.hasNext() ){
SelectionKey sk = (SelectionKey) it.next();

DatagramChannel dc = (DatagramChannel) sk.channel();
dc.receive(theReceiveDataByteBuffer);

System.out.println("theDatagramChannel.receive "+ i++);

it.remove( );
}

// how much time used up
millisecsUntilSendNextDatagram -= System.currentTimeMillis() - start;
}
if( j > numMsgs ) break;
}
theSelector.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Exception " + e);
return;
}
}

public static void main( String args[] )
{
new NIOTest();
}

}

如上所谓,,当发送的多个数据包的时候,怎么在接收的时候一一分开并处理?

请大侠指点。多谢。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧