UDP client and server implemented with Java NIO

Source: Internet
Author: User

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////

Client.java

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////

Package Udp.nio;

Import java.io.IOException;
Import java.net.InetSocketAddress;
Import java.net.SocketAddress;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.DatagramChannel;
Import Java.nio.channels.SelectionKey;
Import Java.nio.channels.Selector;
Import Java.util.Arrays;
Import Java.util.Iterator;

Import Test. Publictool;

public class UdpClient {
Static Datagramchannel channel;
Static Selector Selector;
SocketAddress sa;
Static Bytebuffer ByteBuffer1;
static Bytebuffer ByteBuffer2 = bytebuffer.allocate (100);

public static void Run () {
try {
Channel = Datagramchannel.open ();
Channel.configureblocking (FALSE);
Channel.socket (). bind (New Inetsocketaddress (10000));
} catch (Exception e) {
E.printstacktrace ();
}
try {
selector = Selector.open ();
Channel.register (selector, selectionkey.op_read);
Byte[] ret = {0x7C, 0x13, 0x1F, 0x47, 0x0E, 0x03, 0x06, 0x0F,
0x13, 0x30, 0x00, 0x08, 0x00, 0x00, 0x00, 0x55,
(byte) 0xAA, 0x70, 0x7A};
ByteBuffer1 = Bytebuffer.wrap (ret);
Channel.send (ByteBuffer1, New inetsocketaddress ("192.168.1.200",
10000));
} catch (Exception e) {
E.printstacktrace ();
}

int num = 0;
while (num < 2) {
try {
System.out.println ("1111");
int n = selector.select ();
System.out.println (n);
if (n > 0) {
Iterator Iterator = Selector.selectedkeys (). Iterator ();
System.out.println (iterator);
while (Iterator.hasnext ()) {
Selectionkey key = (Selectionkey) iterator.next ();
Iterator.remove ();
if (key.isreadable ()) {
Channel = (Datagramchannel) key.channel ();
Channel.receive (BYTEBUFFER2);
byte[] all = Bytebuffer2.array ();
byte[] receivebuf = new Byte[all[1]];
System.arraycopy (all, 0, receivebuf, 0, all[1]);
Publictool.printhexstring (RECEIVEBUF);
Bytebuffer2.clear ();
if (receivebuf = = NULL | | receivebuf = = new Byte[0]);
}
}
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
Channel.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

public static void Main (string[] args) {
while (true) {
Run ();
try {
Thread.Sleep (1000l);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////

Server

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////

Package Udp.nio;

Import java.net.InetSocketAddress;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.DatagramChannel;
Import Java.nio.channels.SelectionKey;
Import Java.nio.channels.Selector;
Import Java.util.Arrays;
Import Java.util.Iterator;

Import Test. Publictool;

public class Udpserver
{
Datagramchannel Channel;

Selector Selector;

public void work ()
{
Try
{
Open a UDP Channel
Channel = Datagramchannel.open ();

Set to non-blocking channel
Channel.configureblocking (FALSE);
Binding port
Channel.socket (). bind (New Inetsocketaddress (10000));

Open a Selector
selector = Selector.open ();
Channel.register (selector, selectionkey.op_read);
} catch (Exception e)
{
E.printstacktrace ();
}

Bytebuffer Bytebuffer = bytebuffer.allocate (19);
while (true)
{
Try
{
Make a selection
int n = selector.select ();
if (n > 0)
{
Gets the collection of keys to select
Iterator Iterator = Selector.selectedkeys (). Iterator ();

while (Iterator.hasnext ())
{
Selectionkey key = (Selectionkey) iterator.next ();

Must be manually removed
Iterator.remove ();

if (key.isreadable ())
{
Datagramchannel Datagramchannel = (datagramchannel) key
. Channel ();

Bytebuffer.clear ();
Read
Inetsocketaddress address = (inetsocketaddress) datagramchannel
. receive (Bytebuffer);

Publictool.printhexstring (Bytebuffer.array ());

Delete data in a buffer
Bytebuffer.clear ();
Byte[] ret = {0x55, (byte) 0xAA, 0x70, 0x7A};

Bytebuffer.put (ret);

Bytebuffer.flip ();

Send data
Datagramchannel.send (Bytebuffer, address);
}
}
}
} catch (Exception e)
{
E.printstacktrace ();
}
}

}

public static void Main (string[] args)
{
New Udpserver (). work ();

}

}

UDP client and server implemented with Java NIO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.