Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

how to receive information in kotlin from a server in python (socketserver)

server I have tried almost everything to receive text from python I don't know where the problem comes from from the client or from the server

          try:
              llamadacod = self.request.recv(1024)
              llamada = self.decode(llamadacod)
              print(f"{color.A}{llamada}")
              time.sleep(0.1)
              if llamada == "conectado":
                  msg = "Hello"
                  msgcod = self.encode(msg)
                  print(f"{color.G}{msg}")
                  self.request.send(msgcod)

client

      val thread = Thread(Runnable {
          try{
              val client = Socket("localHost",25565)
              client.setReceiveBufferSize(1024)
              client.outputStream.write("conectado".toByteArray())
              val text = InputStreamReader(client.getInputStream())
              recibir = text.toString()
              client.outputStream.write("Client_desconect".toByteArray())
              client.close()



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I already solved it, the solution was very simple, you just had to ensure that both the server and the client would occupy the same way of communicating

client :

val input = DataInputStream(client.getInputStream())
            id = input.readUTF()

server:

self.request.send(len(msg).to_bytes(2, byteorder='big'))
self.request.send(msg)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...