Last active
October 16, 2016 02:07
-
-
Save am4dr/8f10fd2daf198f2b6c150c99365e11ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| synchronized def print(String str) { | |
| System.out.print str | |
| } | |
| def bootServer() { | |
| def server = new ServerSocket(8080) | |
| Thread.start { | |
| while (true) { | |
| def soc | |
| try { soc = server.accept() } | |
| catch (SocketException e) { println "[server] SocketError: $e.message"; break } | |
| Thread.start { | |
| def s = soc | |
| s.withStreams { is, os -> | |
| os << '[server echo] ' | |
| for (int a = is.read(); a != '\n'; a = is.read()) { | |
| os.write(a) | |
| } | |
| os << '\n' | |
| } | |
| } | |
| } | |
| } | |
| server | |
| } | |
| def newEchoClient(String id, String message = null) { | |
| new Socket('localhost', 8080).withStreams { is, os -> | |
| def m = message ?: "This is client <$id>\n" | |
| print "client <$id>: $m" | |
| os << m | |
| print("client <$id>: $is.text") | |
| } | |
| } | |
| import java.util.concurrent.CountDownLatch | |
| bootServer()?.withCloseable { | |
| println "server: $it" | |
| def n = 10 | |
| def prepared = new CountDownLatch(n) | |
| def finished = new CountDownLatch(n) | |
| def startSignal = new CountDownLatch(1) | |
| (0..<n).each { id -> | |
| Thread.start { | |
| prepared.countDown() | |
| startSignal.await() | |
| newEchoClient(id.toString()) | |
| finished.countDown() | |
| } | |
| } | |
| prepared.await() | |
| startSignal.countDown() | |
| finished.await() | |
| } | |
| null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=8080] | |
| client <2>: This is client <2> | |
| client <3>: This is client <3> | |
| client <0>: This is client <0> | |
| client <1>: This is client <1> | |
| client <6>: This is client <6> | |
| client <8>: This is client <8> | |
| client <7>: This is client <7> | |
| client <6>: [server echo] This is client <6> | |
| client <9>: This is client <9> | |
| client <5>: This is client <5> | |
| client <4>: This is client <4> | |
| client <2>: [server echo] This is client <2> | |
| client <4>: [server echo] This is client <4> | |
| client <0>: [server echo] This is client <0> | |
| client <5>: [server echo] This is client <5> | |
| client <3>: [server echo] This is client <3> | |
| client <1>: [server echo] This is client <1> | |
| client <8>: [server echo] This is client <8> | |
| client <7>: [server echo] This is client <7> | |
| client <9>: [server echo] This is client <9> | |
| [server] SocketError: socket closed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment