'in.next()/out.println() blocking the program - scala/java

I am trying out a simple multi-threaded client/server program in scala using the java packages for io.The program waits for something indefinitely both on the client side as well as server side. Not sure what is the problem here please help out

Client Code:

import java.net._
import java.io._
import scala.io._
import java.nio.file.{Files, Paths}
import scala.collection.JavaConverters._


object cool {
  def main(args: Array[String]): Unit = {
    val s = new Socket(InetAddress.getByName("localhost"), 9999)
    val in = new BufferedSource(s.getInputStream()).getLines();
    //val outputStreamWriter = new BufferedSource(s.getOutputStream())
    val out = new PrintStream(s.getOutputStream())

    println(in.next());println(in.next());println(in.next())
    var preference = scala.io.StdIn.readInt()
    if(preference == 1 || preference == 2){
      print("Enter username: ")
      var username = scala.io.StdIn.readLine()
      print("Enter password: ")
      var password = scala.io.StdIn.readLine()
      out.print(preference)
      var user_pass = Array(username,password).mkString("-")
      out.print(user_pass)
    }
    println(in.next())

    //out.println(preference)

    out.flush()
    //println("Received: " + in.next())

    s.close()
  }


}

Client handler :

import hello.{search, writeCsvFile}

import java.io.PrintStream
import java.net.Socket
import scala.io.BufferedSource

class Handler(s: Socket) extends Thread {
  override def run(): Unit = {
    println("From a thread")

      val in = new BufferedSource(s.getInputStream()).getLines();
      val out = new PrintStream(s.getOutputStream())

      //out.println(in.next());

      var i=0
      while(i==0){
        val send_msg = "Press the corresponding number for functionalities\n" +
          "1.Login\n2.Create account"
        out.println(send_msg)
        var preference = in.next()
        var user_pass = in.next()
        var username = user_pass.split("-")(0)
        var password = user_pass.split("-")(1)
        println(username,password)
        if(preference == "2"){
          var data =  Seq(Seq(username,password,0))
          writeCsvFile("credentials.csv",List("username","password"),data)
        }
        else if(preference == "1") {
          val ans = search("credentials.csv",username,password)
          println("inside search")
          out.print("hello")
//          if(ans == 0)out.println("Correct")
//          else if(ans == 1)println("Please register")
//          else println("Wrong")
        }
        //var preference = in.next()
        //println(preference)
        i=i+1
      }

      //println(in.next())
      out.flush();
      s.close()
    }

}

Server :

import java.net._
import java.io._
import java.nio.channels.FileChannel
import java.nio.file.{Files, Paths}
import scala.io._
import scala.jdk.CollectionConverters.IteratorHasAsScala


object hello{
  var connections = 0

  def main(args: Array[String]): Unit = {
    val server  = new ServerSocket(9999);
    while(true){
      val s = server.accept();
      println("Connection requests : "+(connections+1));
      connections=connections+1
      val handler = new Handler(s)
      handler.start()
    }

  }

  def writeCsvFile(filename:String,header:List[String],rows:Seq[Seq[Any]]):Unit=synchronized{
    val file = filename;
    val separator = ",";

    val writer = new FileWriter(filename,true);
    try{
      rows.foreach{
        line => writer.write(s"${line.map(_.toString).mkString(separator)}\n")
      }
    }
    finally {
      writer.flush();
      writer.close();
    }
  }

  def search(filename:String,username:String,password:String): Int ={
    var reader = new FileReader(filename)
    try{
      var data = Files.lines(Paths.get(filename)).iterator().asScala.toSeq.map(_.split(",")(0))
      if(!data.contains(username)){
        return 1
      }
      else{
        val map = scala.collection.mutable.Map[String,String]()
        var names = Files.lines(Paths.get(filename)).iterator().asScala.toSeq.map(_.split(",")(0))
        var pass = Files.lines(Paths.get(filename)).iterator().asScala.toSeq.map(_.split(",")(1))
        //println(names,pass)
        for(x <- names.indices){
          map+=(names(x)->pass(x))
        }
        if(map(username) != password) return 2
      }
    }
    finally {
      reader.close()
    }
    return 0
  }
}

Currently the program waits here indefinitely: Client side:

Press the corresponding number for functionalities
1.Login
2.Create account
1
Enter username: bob
Enter password: cat

Server side:

Connection requests : 1
From a thread


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source