简体   繁体   English

Kotlin 客户端从 Linux python3 套接字服务器发送和接收图像

[英]Kotlin Client send and receive image from Linux python3 socket Server

I'm trying to send and receive Image with Socket connect However, the string between the client and the server can be exchanged, but it does not proceed from exchanging images.我正在尝试使用 Socket connect 发送和接收图像但是,客户端和服务器之间的字符串可以交换,但它不会从交换图像开始。

This is My Kotlin Client code这是我的 Kotlin 客户端代码

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    Client().start()

}



class Client : Thread()
{
    var readImage = ReadImage()
    override fun run(){
        try{
            var socket = Socket("35.216.104.58",3000)
            var input = socket.getInputStream()
            var dis = DataInputStream(input)

            var output = socket.getOutputStream()
            var dos = DataOutputStream(output)
            var data  = getImageByte(readImage.setImage(0))
            dos.write(data)

            dos.close()
            output.close()
            socket.close()



        }catch (e: Exception){
            e.printStackTrace()
        }


    }

  }
}

and this is My Python3 Code这是我的 Python3 代码

import socket
import cv2

ip = ''
port = 3000

server_socket = socket.socket(socket.AF_INET)
server_socket.bind((ip,port))
server_socket.listen(1)

print('Waiting Client...')

client_socket, addr = server_socket.accept()
print('Connet Client!!')

data = client_socket.recv(1024)
encoded_img = np.fromstring(data,dtype = np.uint8)
image = cv2.imdecode(encoded_img,cv2.IMREAD_COLOR)

How can I send and receive images between clients and servers?如何在客户端和服务器之间发送和接收图像?

Note that you are not sending any data, just creating a connection.请注意,您没有发送任何数据,只是创建了一个连接。

You can refer to this example of how to send data from kotlin: How to send and receive strings through TCP connection using kotlin你可以参考这个如何从 kotlin 发送数据的例子: How to send and receive strings through TCP connection using kotlin

And this example of how to receive data in python: https://docs.python.org/3/library/socket.html#example而这个如何在 python 中接收数据的例子: https ://docs.python.org/3/library/socket.html#example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM