Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClientServer.java
More file actions
Latest commit
125 lines (114 loc) · 5.58 KB
/
Copy pathClientServer.java
File metadata and controls
125 lines (114 loc) · 5.58 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* MIT License
*
* Copyright (c) 2017 Distributed clocks
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
importorg.github.com.jvec.JVec;
importjava.net.DatagramPacket;
importjava.net.DatagramSocket;
importjava.net.InetAddress;
importjava.util.concurrent.ForkJoinPool;
importjava.util.concurrent.RecursiveAction;
importjava.nio.ByteBuffer;
publicclassClientServer {
privatestaticfinalintSERVERPORT = 8080;
privatestaticfinalintCLIENTPORT = 8081;
privatestaticfinalStringSERVERIP = "localhost";
privatestaticfinalintMESSAGES = 10;
privatestaticfinalintMAXBUFLEN = 100;
staticclassUDPServerextendsRecursiveAction {
@Override
protectedvoidcompute() {
try {
JVecvcInfo = newJVec("server", "serverlogfile");
DatagramSocketserverSocket = newDatagramSocket(SERVERPORT);
byte[] receiveData = newbyte[MAXBUFLEN];
InetAddressIPAddress = InetAddress.getByName(SERVERIP);
intn = 0, nMinOne = 0;
for (inti = 0; i < MESSAGES; i++) {
DatagramPacketreceivePacket = newDatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
ByteBufferdecodedMsg = ByteBuffer.wrap(vcInfo.unpackReceive("Received message from client.", receivePacket.getData()));
intdecodedInt = decodedMsg.getInt();
System.out.println("Received message from client: " + decodedInt);
if (decodedInt == 0) {
n = 0;
} elseif (decodedInt == 1) {
nMinOne = 0;
n = 1;
} else {
intnMinTwo = nMinOne;
nMinOne = n;
n = nMinOne + nMinTwo;
}
ByteBufferb = ByteBuffer.allocate(Integer.SIZE/Byte.SIZE);
byte[] inBuf = vcInfo.prepareSend("Responding to client.", b.putInt(n).array());
System.out.println("Responding to client with value " + n);
DatagramPacketsendPacket = newDatagramPacket(inBuf, inBuf.length, IPAddress, CLIENTPORT);
serverSocket.send(sendPacket);
}
serverSocket.close();
// We are done. Flush the buffered results to our logfile and close the reader.
vcInfo.closeJVectorLog();
} catch (Exceptione) {
e.printStackTrace();
}
}
}
staticclassUDPClientextendsRecursiveAction {
@Override
protectedvoidcompute() {
try {
JVecvcInfo = newJVec("client", "clientlogfile");
DatagramSocketclientSocket = newDatagramSocket(CLIENTPORT);
byte[] receiveData = newbyte[MAXBUFLEN];
InetAddressIPAddress = InetAddress.getByName(SERVERIP);
for (inti = 0; i < MESSAGES; i++) {
ByteBufferb = ByteBuffer.allocate(Integer.SIZE/Byte.SIZE);
byte[] inBuf = vcInfo.prepareSend("Sending message to server.", b.putInt(i).array());
System.out.println("Sending message to server.");
DatagramPacketsendPacket = newDatagramPacket(inBuf, inBuf.length, IPAddress, SERVERPORT);
clientSocket.send(sendPacket);
DatagramPacketreceivePacket = newDatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
ByteBufferdecodedMsg = ByteBuffer.wrap(vcInfo.unpackReceive("Received message from server.", receivePacket.getData()));
intdecodedInt = decodedMsg.getInt();
System.out.println("Received value " + decodedInt + " from server.");
}
clientSocket.close();
// We are done. Flush the buffered results to our logfile and close the reader.
vcInfo.closeJVectorLog();
} catch (Exceptione) {
System.out.println("Something went wrong.");
e.printStackTrace();
}
}
}
publicstaticvoidmain(Stringargs[]) throwsInterruptedException {
UDPServerserver = newUDPServer();
UDPClientclient = newUDPClient();
ForkJoinPoolforkJoinPool = newForkJoinPool(2);
forkJoinPool.execute(server);
Thread.sleep(1000);
forkJoinPool.invoke(client);
}
}