付上一段代码来说明我的问题: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();
}}如上所谓,,当发送的多个数据包的时候,怎么在接收的时候一一分开并处理?请大侠指点。多谢。
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();
}}如上所谓,,当发送的多个数据包的时候,怎么在接收的时候一一分开并处理?请大侠指点。多谢。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货