How to communicate with a python Twisted server from Java successfully?

These questions may sound silly, but I am new to this networking thing. I have been trying for quite a few days now to implement a client that works with a Twisted server, but I am failing to get any response back from the server. I have read a lot of docs and watched a few tutorials and I got some of the stuff fixed and got some of the concepts better understood.

Before I step on to asking any questions, I wanna show you my code first. This is what I use to talk to the Twisted-based server:

        val socketfactory: SocketFactory = SocketFactory.getDefault()
        val socket = socketfactory.createSocket(host, port)
        socket.keepAlive = true
        socket.tcpNoDelay = true
        val isSocketConnected = socket.isConnected //this checks socket's connectivity
        val dOut = DataOutputStream(socket.getOutputStream())
        val dIn = DataInputStream(socket.getInputStream())
        
        val teststring = "Hi server!"
        dOut.writeUTF(teststring)
        Log.d("MILESTONE", "MESSAGE SENT AT THIS POINT, Socket is connected ?: $isSocketConnected")
        
        var testreader = ""
        while (true) {
            testreader = dIn.readUTF()
            Log.d("READING:", "RECEIVED THIS: $testreader")
        }
        

My code seems to never get to the second “Log” line. It never gets there. I assume that’s because I never get any input from the server. This is getting me confused. Because “socket.isConnected” returns true. Doesn’t that mean there is an ongoing connection between the client (me) and the server ? But when I send any output the server doesn’t talk back.

So my questions are: 1- Am I doing something wrong? Why do I receive no talk from the server and it blocks the code? 2- Is SocketFactory necessary ? 3- Is there any library that communicates with Twisted from Java ?

Thanks in advance !

Answer

For everyone who’s struggling to communicate with a Twisted-running python server, I came with the absolutely best solution ever! After inspecting Twisted’s open source code, I came to realize it has a “LineReceiver” class that only responds to a message if the line is finished. In other words, you can keep sending data forever and it will never respond until you finish the line and start a new one. Twisted will know the line has finished when a delimiter is used. (It is configured on the server-side). Most servers running Twisted will use a line delimiter : “rn”

That’s the tricky thing! Once you send that little string, it will start responding to you. Here it is in an example :

      val dOut = DataOutputStream(socket.getOutputStream()) //This is my favorite way of sending data!
      val dIn = socket.getInputStream().bufferedReader(Charsets.UTF_8) //This is my favorite way of reading data ! 
      val teststring = "hi server! rn" //This is the tricky part !

That’s it ! all you have to do after that is read the lines from the bufferedReader, like this !

var testreader: List<String>
        while (true) {
            testreader = dIn.bufferedReader(Charsets.UTF_8).readLines()
            for (line in testreader)
                Log.e("MILESTONE", line)

        }

After I started reading the input, I found out the server started actually sending me strings and communicating back with me. I hope everyone will get their codes working concerning this or any other thing!