Kotlin BufferReader is not getting Server response
Asked Answered
M

0

0

I'm really new in Kotlin. I have two application one is Client in Android emulator and one is in Windows Form Application Server (My server is Using SimpleTCP library in C#) .

Here is the Server Code:

    private void Form1_Load(object sender, EventArgs e)
    {
        server = new SimpleTcpServer();
        server.Delimiter = 0x13;
        server.StringEncoder = Encoding.UTF8;
        server.DataReceived += Server_DataReceived;
        
        messageList.Text += "Server starting...";
        server.Start("192.168.1.7", 5000);
        messageList.Text += "\r\n Server started...";
    }
    private void Server_DataReceived(object sender, SimpleTCP.Message e)
    {
                messageList.Invoke((MethodInvoker)delegate ()
                {
                     messageList.Text += string.Format("\r\n Client Message: {0} \n ", e.MessageString);
                });
                e.ReplyLine("Hello from Server");
    }

I'm communicating via localhost. I can send request and Server can get this request without problem but when i try to get response from Server unfortunately cannot receive response. val message = input!!.readLine() is always returning null

Can you help me why i cannot do this?

Here my code

var thread: Thread? = null
var etIP: EditText? = null
var etPort: EditText? = null
var tvMessages: TextView? = null
var etMessage: EditText? = null
var btnSend: Button? = null
var SERVER_IP: String? = null
var SERVER_PORT = 0
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    etIP = findViewById(R.id.etIP)
    etPort = findViewById(R.id.etPort)
    tvMessages = findViewById(R.id.tvMessages)
    etMessage = findViewById(R.id.etMessage)
    btnSend = findViewById(R.id.btnSend)
    val btnConnect: Button = findViewById(R.id.btnConnect)
    btnConnect.setOnClickListener {
        tvMessages!!.text = ""
        SERVER_IP = etIP!!.text.toString().trim { it <= ' ' }
        SERVER_PORT = etPort!!.text.toString().trim { it <= ' ' }.toInt()
        thread = Thread(Thread1())
        thread!!.start()
    }
    btnSend!!.setOnClickListener {
        val message = "Android!"+etMessage!!.text.toString().trim { it <= ' ' }
        if (!message.isEmpty()) {
            Thread(Thread3(message)).start()
        }
    }
}

private var output: PrintWriter? = null
private var input: BufferedReader? = null

internal inner class Thread1 : Runnable {
    override fun run() {
        val socket: Socket
        try {
            socket = Socket(SERVER_IP, SERVER_PORT)
            output = PrintWriter(socket.getOutputStream())
            input = BufferedReader(InputStreamReader(socket.getInputStream()))
            runOnUiThread(Runnable { tvMessages!!.text = "Connected\n" })
            Thread(Thread2()).start()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

internal inner class Thread2 : Runnable {
    override fun run() {
        while (true) {
            try {
                val message = input!!.readLine().toString()
                run {
                    runOnUiThread(Runnable { tvMessages!!.append("server: $message\n") })
                }
                run {
                    thread = Thread(Thread1())
                    thread!!.start()
                    return
                }
            } catch (e: Exception) {
                tvMessages!!.text = e.toString()
            }
        }
    }
}

internal inner class Thread3(private val message: String) : Runnable {
    override fun run() {
        output!!.write(message)
        output!!.flush()
        runOnUiThread(Runnable {
            tvMessages!!.append("client: $message\n")
            tvMessages!!.setMovementMethod(ScrollingMovementMethod())
            etMessage!!.setText("")
        })
    }
}
fun clearAll(view: View){
   tvMessages!!.text = ""
}
Manteltree answered 12/10, 2020 at 9:56 Comment(8)
What do you mean by "cannot get request"? Do you get any exceptions or does it just block indefinitely? What does the server look like? The BufferedReader might be waiting for a new line character that the server isn't sending.Highlight
@JoachimSauer when i send server mesaage like “hi Server” i’m response from Server “hi Client” but Client cannot receive itManteltree
I guess you read wrong because in my question there is no ”cannot get request”...Manteltree
Sure, that was a typo on my part, but the question still stands: what happens instead of you receiving the response? Do you get an exception or does everything just stop? Can you describe the problematic behaviour. Something is happening, what is that. Also: what does the server look like? That's kind of important to understand if the client is written correctly.Highlight
@JoachimSauer Ok You are right i will edit my questionManteltree
@JoachimSauer so do you have any suggession about solution or is it still not clear to you?Manteltree
I don't know what SimpleTCP is so I don't know how it behaves, sorry. You might need someone who understands that (and adopt the tags accordingly).Highlight
@JoachimSauer i solved it my self thanksManteltree

© 2022 - 2025 — McMap. All rights reserved.