1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.udp.perf;
21
22 import java.net.InetSocketAddress;
23
24 import org.apache.mina.core.buffer.IoBuffer;
25 import org.apache.mina.core.future.ConnectFuture;
26 import org.apache.mina.core.service.IoConnector;
27 import org.apache.mina.core.service.IoHandlerAdapter;
28 import org.apache.mina.core.session.IdleStatus;
29 import org.apache.mina.core.session.IoSession;
30 import org.apache.mina.transport.socket.nio.NioDatagramConnector;
31
32
33
34
35
36
37
38
39
40 public class UdpClient extends IoHandlerAdapter {
41
42 private IoConnector connector;
43
44
45 private static IoSession session;
46
47
48
49
50 public UdpClient() {
51 connector = new NioDatagramConnector();
52
53 connector.setHandler(this);
54
55 ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", UdpServer.PORT));
56
57 connFuture.awaitUninterruptibly();
58
59 session = connFuture.getSession();
60 }
61
62
63
64
65 @Override
66 public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
67 cause.printStackTrace();
68 }
69
70
71
72
73 @Override
74 public void messageReceived(IoSession session, Object message) throws Exception {
75 }
76
77
78
79
80 @Override
81 public void messageSent(IoSession session, Object message) throws Exception {
82 }
83
84
85
86
87 @Override
88 public void sessionClosed(IoSession session) throws Exception {
89 }
90
91
92
93
94 @Override
95 public void sessionCreated(IoSession session) throws Exception {
96 }
97
98
99
100
101 @Override
102 public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
103 }
104
105
106
107
108 @Override
109 public void sessionOpened(IoSession session) throws Exception {
110 }
111
112
113
114
115
116
117
118 public static void main(String[] args) throws Exception {
119 UdpClientdp/perf/UdpClient.html#UdpClient">UdpClient client = new UdpClient();
120
121 long t0 = System.currentTimeMillis();
122
123 for (int i = 0; i <= UdpServer.MAX_RECEIVED; i++) {
124 Thread.sleep(1);
125
126 String str = Integer.toString(i);
127 byte[] data = str.getBytes();
128 IoBuffer buffer = IoBuffer.allocate(data.length);
129 buffer.put(data);
130 buffer.flip();
131 session.write(buffer);
132
133 if (i % 10000 == 0) {
134 System.out.println("Sent " + i + " messages");
135 }
136 }
137
138 long t1 = System.currentTimeMillis();
139
140 System.out.println("Sent messages delay : " + (t1 - t0));
141
142 Thread.sleep(100000);
143
144 client.connector.dispose(true);
145 }
146 }