[Java] NIO DatagramChannel Tutorial

[Java] NIO DatagramChannel Tutorial

Goal

  • Java NIO DatagramChannel을 이용하여 UDP Send/Receive 구현


Practice

DatgramChannel Receiver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 *****************************************************************
 * 
 * NIOUDPRecvTest.java
 * 
 * NIO DatagramChannel Receiver
 *
 *****************************************************************
 *
 * @version 1.0.0 2017-10-11 dorbae 최초생성
 * @since 1.0.0
 * @author dorbae
 *
 */
package test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class NIOUDPRecvTest {

 /**
  *
  * @version 1.0.0 2017-10-11 dorbae 최초생성
  * @since 1.0.0
  * @author dorbae
  *
  * @param args
  */
 public static void main(String[] args) {
  DatagramChannel channel = null;
  try {
   channel = DatagramChannel.open();
   channel.socket().bind(new InetSocketAddress("localhost", 9000));

  } catch (IOException e) {
   e.printStackTrace();
   System.exit(1);
  }
  
  ByteBuffer buf = ByteBuffer.allocate(256);
  byte[] buffer = new byte[256];
  int limit = 0;
  try {
   while (true) {
    buf.clear();
    try {
     System.out.println("\nWaiting...");
     channel.receive(buf);
     limit = buf.limit();
     System.out.println("buf.limit()=" + limit);
     System.out.println("buf.capacity()=" + buf.capacity());
     System.out.println("buf.position()=" + buf.position());
     System.out.println("buf.arrayOffset()=" + buf.arrayOffset());

     buf.flip();
     limit = buf.limit();
     System.out.println("buf.limit()=" + limit);

     buf.get(buffer, 0, limit);

     System.out.println("data=" + new String(buffer, 0, limit, "utf8"));

    } catch (IOException e) {
     break;
    }

   }

  } catch (Exception e) {
   e.printStackTrace();
  
  } finally {
   if (channel != null)
    try {
     channel.close();
    } catch (IOException e) {}
  }
  
 } 

}


DatagramChannel Sender

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 *****************************************************************
 * 
 * NIOUDPSendTest.java
 * 
 * NIO DatagramChannel Sender
 *
 *****************************************************************
 *
 * @version 1.0.0 2017-10-11 dorbae 최초생성
 * @since 1.0.0
 * @author dorbae
 *
 */
package test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class NIOUDPSendTest {

 /**
  *
  * @version 1.0.0 2017-10-11 dorbae 최초생성
  * @since 1.0.0
  * @author dorbae
  *
  * @param args
  * @throws IOException 
  */
 public static void main(String[] args) throws IOException {
  String message = "한글 String to write to file..." + System.currentTimeMillis();

  
  DatagramChannel channel = DatagramChannel.open();
  
  
  ByteBuffer buf = ByteBuffer.allocate(256);
  buf.clear();
  buf.put(message.getBytes("utf8"));
  buf.flip();

  int bytesSent = channel.send(buf, new InetSocketAddress("localhost", 9000));

 }

}

댓글남기기

-->