-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaytimeServer.java
More file actions
87 lines (74 loc) · 3.09 KB
/
DaytimeServer.java
File metadata and controls
87 lines (74 loc) · 3.09 KB
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
85
86
87
import com.sun.nio.sctp.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.charset.*;
import java.text.*;
import java.util.*;
public class DaytimeServer {
static int SERVER_PORT = 3456;
static int US_STREAM = 0;
static int FR_STREAM = 1;
static SimpleDateFormat USformatter = new SimpleDateFormat(
"h:mm:ss a EEE d MMM yy, zzzz", Locale.US);
static SimpleDateFormat FRformatter = new SimpleDateFormat(
"h:mm:ss a EEE d MMM yy, zzzz", Locale.FRENCH);
public static void main(String[] args) throws IOException {
SctpServerChannel ssc = SctpServerChannel.open();
InetSocketAddress serverAddr = new InetSocketAddress(SERVER_PORT);
ssc.bind(serverAddr);
ByteBuffer buf = ByteBuffer.allocateDirect(60);
CharBuffer cbuf = CharBuffer.allocate(60);
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer recvbuf = ByteBuffer.allocateDirect(255);
while (true) {
SctpChannel sc = ssc.accept();
/* get the current date */
Date today = new Date();
cbuf.put(USformatter.format(today)).flip();
encoder.encode(cbuf, buf, true);
buf.flip();
/* send the message on the US stream */
MessageInfo outMessageInfo = MessageInfo.createOutgoing(null,
US_STREAM);
sc.send(buf, outMessageInfo);
/* update the buffer with French format */
cbuf.clear();
cbuf.put(FRformatter.format(today)).flip();
buf.clear();
encoder.encode(cbuf, buf, true);
buf.flip();
/* send the message on the French stream */
outMessageInfo.streamNumber(FR_STREAM);
sc.send(buf, outMessageInfo);
cbuf.clear();
buf.clear();
// shutdown and receive all pending messages/notifications
sc.shutdown();
AssociationHandler assocHandler = new AssociationHandler();
MessageInfo inMessageInfo = null;
while (true) {
inMessageInfo = sc.receive(recvbuf, System.out, assocHandler);
if (inMessageInfo == null || inMessageInfo.bytes() == -1) {
break;
}
}
sc.close();
}
}
static class AssociationHandler
extends AbstractNotificationHandler<PrintStream>
{
public HandlerResult handleNotification(AssociationChangeNotification not,
PrintStream stream) {
stream.println("AssociationChangeNotification received: " + not);
return HandlerResult.CONTINUE;
}
public HandlerResult handleNotification(ShutdownNotification not,
PrintStream stream) {
stream.println("ShutdownNotification received: " + not);
return HandlerResult.RETURN;
}
}
}