I am trying to write a Connector class that will provide me with a text IO and a server that itself transmits only text. So basically a telnet clone. I have written this so far:
import java.net._ import scala.actors.Actor import Actor._ import java.io._ class Connector(socket: Socket, handler: String => Unit) { private val out = new PrintStream(socket.getOutputStream) private val in = new BufferedReader(new InputStreamReader(socket.getInputStream)) private val receiver = actor { var msg = in.readLine while (msg != null) { handler(msg) msg = in.readLine } } def print(msg: String) = out.println(msg) def close = { in.close out.close } }
Now I'm trying to connect to the server with this (for testing)
import java.net._ import java.io._ import InetAddress.getByName object Main extends App { val addr = getByName("wolfwings.us") val socket = new Socket(addr, 4000) val connector = new Connector(socket, ((msg) => println(msg))) val stdIn = new BufferedReader(new InputStreamReader(System.in)) var input = stdIn.readLine while(input != null) { connector.print(input) input = stdIn.readLine } stdIn.close connector.close }
What happens: it connects correctly and sends me a server welcome screen. And then he stops in the middle of the text. When I enter now, it displays the last line of the welcome text, and then the rest, minus the last line. And so on. In addition, an extra \n is placed per line. When I replace println with print , then suddenly there is no \n .
From what I see, everything should work fine and right. I mean, this doesnβt look like plain text, since it is more witchcraft? What hidden quirks did I not know here?
EDIT : Bugfix: after some attempts and research, I found that in fact the program only swallows the last line of server responses. It seems that the server itself sometimes does not send complete lines, so, of course, the client will read them.
Lanbo source share