tag:blogger.com,1999:blog-7371895490737388670.post3218373276333837058..comments2023-05-04T00:35:27.914-05:00Comments on Scratching my programming itch: Groovy Sockets exampleMike Millerhttp://www.blogger.com/profile/09697567698545249690noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-7371895490737388670.post-13489579486493777882013-07-23T21:16:32.783-05:002013-07-23T21:16:32.783-05:00Thank you so much. It really helped me, a groovy s...Thank you so much. It really helped me, a groovy starter.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7371895490737388670.post-27329265038404698832011-10-18T22:56:49.578-05:002011-10-18T22:56:49.578-05:00Not sure if this matches what you tried, but I adj...Not sure if this matches what you tried, but I adjusted the code a bit to send mutliple lines and did call readLine until it returned null.<br /><br />Server code<br />import java.net.ServerSocket<br />def server = new ServerSocket(4242)<br /><br />while(true) {<br /> server.accept { socket -><br /> println "processing new connection..." <br /> socket.withStreams { input, output -><br /> def reader = input.newReader()<br /> def buffer = null<br /> while ((buffer = reader.readLine()) != null) {<br /> //def buffer = reader.readLine()<br /> println "server received: $buffer"<br /> if (buffer == "*bye*") {<br /> println "exiting..."<br /> System.exit(0)<br /> } else {<br /> output << "echo-response: " + buffer + "\n"<br /> }<br /> }<br /> }<br /> println "processing complete."<br /> }<br />}<br /><br /><br />Client side<br /><br />s = new Socket("localhost", 4242);<br />s.withStreams { input, output -><br /> output << "echo line 1\n"<br /> output << "echo line 2\n"<br /> output << "echo line 3\n"<br /> output << "echo line 4\n"<br /><br /> //output << "*bye*\n"<br /> reader = input.newReader()<br /> buffer = reader.readLine()<br /> println "response = $buffer"<br />}Mike Millerhttps://www.blogger.com/profile/09697567698545249690noreply@blogger.comtag:blogger.com,1999:blog-7371895490737388670.post-65466211537908756352011-10-18T15:21:13.638-05:002011-10-18T15:21:13.638-05:00Have you tried reading multiple lines on the serve...Have you tried reading multiple lines on the server side until end of stream? I tried and the readLine call never returns null for end of stream. It just hangs.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7371895490737388670.post-91152823526471268112010-04-29T14:19:06.890-05:002010-04-29T14:19:06.890-05:00Kris,
Here's a copy of the accept method from...Kris,<br /><br />Here's a copy of the accept method from the GDK taken out of Groovy 1.7.2. As you can see from the code - the GDK class does get a Socket returned but then it calls the closure within a thread using the Socket it was returned. Sorry, maybe my description wasn't completely clear but you can see more from the code - which is in DefaultGroovyMethods.java in the GDK.<br /><br /><br />/**<br /> * Accepts a connection and passes the resulting Socket to the closure<br /> * which runs in a new Thread.<br /> *<br /> * @param serverSocket a ServerSocket<br /> * @param closure a Closure<br /> * @return a Socket<br /> * @throws IOException if an IOException occurs.<br /> * @see java.net.ServerSocket#accept()<br /> * @since 1.0<br /> */<br /> public static Socket accept(ServerSocket serverSocket, final Closure closure) throws IOException {<br /> final Socket socket = serverSocket.accept();<br /> new Thread(new Runnable() {<br /> public void run() {<br /> try {<br /> closure.call(socket);<br /> } finally {<br /> if (socket != null) {<br /> try {<br /> socket.close();<br /> } catch (IOException e) {<br /> LOG.warning("Caught exception closing socket: " + e);<br /> }<br /> }<br /> }<br /> }<br /> }).start();<br /> return socket;<br /> }Mike Millerhttps://www.blogger.com/profile/09697567698545249690noreply@blogger.comtag:blogger.com,1999:blog-7371895490737388670.post-1668750555983506102010-04-29T12:46:15.466-05:002010-04-29T12:46:15.466-05:00ServerSocket's accept returns a plain JDK base...ServerSocket's accept returns a plain JDK based socket. So how does withStreams work with plain JDK based socket or is it a groovy based socket class enhanced with withStreams method that takes c closure. Thanks for the post.Krisnoreply@blogger.com