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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21529030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:11:25  来源:igfitidea点击:

TCP Client Socket. Connect and wait for input stream

javaandroidsocketstcp

提问by Penta

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 connects to ip 192.168.11.254 and listen on port 5566. the second line is the image path which I receive.

在终端上,我可以使用这个 netcat 命令读取输入流。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. But I don't receive the inputstream like in netcat, nothing happens.

在我的应用程序上,我有一个 Java 客户端套接字,它连接到相同的 ip 和端口。但是我没有像在 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();
}

采纳答案by Rajan

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);   
    }
}

回答by BetaRide

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(). This doesn't do any buffering.

如果卡不发送某种类型的行尾,则此方法将永远不会返回。尝试从clientSocket.getInputStream(). 这不会做任何缓冲。