简体   繁体   English

TCP客户端套接字。 连接并等待输入流

[英]TCP Client Socket. Connect and wait for input stream

I have a wifi sd card which sends me TCP packages with information about new images on the memory after I shoot a photo with the camera. 我有一张wifi sd卡,在我用相机拍摄照片后,会向我发送TCP包,其中包含有关新内存图像的信息。

On the terminal I can read the inputstream with this netcat command. 在终端上,我可以使用此netcat命令读取输入流。 netcat connects to ip 192.168.11.254 and listen on port 5566. the second line is the image path which I receive. netcat连接到ip 192.168.11.254并侦听端口5566.第二行是我收到的图像路径。

$ nc 192.168.11.254 5566
>/mnt/sd/DCIM/109_0302/IMGP0101.JPG

On my app I have a Java client socket which is connected to the same ip and port. 在我的应用程序上,我有一个Java客户端套接字连接到相同的IP和端口。 But I don't receive the inputstream like in netcat, nothing happens. 但我没有像netcat那样收到输入流,没有任何反应。

void startListenForTCP (){
    Thread TCPListenerThread = new Thread(new Runnable() {
        @Override
        public void run() {

            Boolean run = true;
            String serverMessage = null;
            InetAddress serverAddr = null;

            try {
                serverAddr = InetAddress.getByName("192.168.11.254");
                Socket clientSocket = new Socket(serverAddr, 5566);

                try{
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                    while (run) {
                        serverMessage = in.readLine();
                        mHandler.post(new DisplayToast(context, "TCP : " + serverMessage));
                    }

                } catch(UnknownHostException e) {
                    mHandler.post(new DisplayToast(context, "UnknownHostException"));

                } catch(IOException e) {
                    mHandler.post(new DisplayToast(context, "IOException"));
                } finally {
                    clientSocket.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
    TCPListenerThread.start();
}

try this may it works 试试这可能有效

Step 1 第1步

Remove these two lines: 删除这两行:

serverAddr = InetAddress.getByName("192.168.11.254");
Socket clientSocket = new Socket(serverAddr, 5566);

Add this line: 添加此行:

Socket clientSocket = new Socket("192.168.11.254", 5566);

Step 2 第2步

Remove this code: 删除此代码:

try {
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while (run) {
        serverMessage = in.readLine();
        mHandler.post(new DisplayToast(context, "TCP : " + serverMessage));
    }
}

Add this code: 添加此代码:

try {
    char[] buffer = new char[2048];
    int charsRead = 0;
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while ((charsRead = in.read(buffer)) != -1) {
        String message = new String(buffer).substring(0, charsRead);
        Log.e("In While", "msg :"+message);   
    }
}

The javadoc of BufferedReader.readLine() states: BufferedReader.readLine()的javadoc指出:

Read a line of text. 阅读一行文字。 A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed. 一条线被认为是由换行符('\\ n'),回车符('\\ r')或回车符中的任何一个终止,后面紧跟换行符。

If the card doesn't send some kind of end of line, this method will never return. 如果卡没有发送某种行尾,这种方法永远不会返回。 Try to read directyl from clientSocket.getInputStream() . 尝试从clientSocket.getInputStream()读取directyl。 This doesn't do any buffering. 这不做任何缓冲。

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

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