aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Client/Client.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/Client/Client.scala')
-rw-r--r--src/main/scala/Client/Client.scala28
1 files changed, 28 insertions, 0 deletions
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