From ae82027a9bd4e75582f9499d4006b18c29a4129c Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Mon, 4 Nov 2024 21:38:50 +0200 Subject: Added basic TCP networking for the server. The client's networking is still very experimental and the actual protocol is not yet specified for either side. --- src/main/scala/Client/Client.scala | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/scala/Client/Client.scala (limited to 'src/main/scala/Client/Client.scala') diff --git a/src/main/scala/Client/Client.scala b/src/main/scala/Client/Client.scala new file mode 100644 index 0000000..1b843ab --- /dev/null +++ b/src/main/scala/Client/Client.scala @@ -0,0 +1,28 @@ +package o1game.Client + +import java.lang.Thread.sleep +import java.net.Socket +import scala.io.Source +import scala.sys.process.stdout +import o1game.constants.* + +class Client(ip: String, port: Int): + private val socket = Socket(ip, port) + private val input = socket.getInputStream + private val output = socket.getOutputStream + private val buffer: Array[Byte] = Array.ofDim(MAX_MSG_SIZE) + private var bufferIndex = 0 + + def startClient(): Unit = + while true do + sleep(POLL_INTERVAL) + + while input.available() != 0 do + val bytesRead = input.read(buffer) + if bytesRead != -1 then + print(buffer.take(bytesRead).toVector.map(_.toChar).mkString) + stdout.flush() + + bufferIndex = s"Houston, I think this shouldn't be so hard.\n".toVector.map(_.toByte).copyToArray(buffer) + output.write(buffer, 0, bufferIndex) + output.flush() \ No newline at end of file -- cgit v1.2.3