简体   繁体   English

Android TCP套接字无法接收数据

[英]Android tcp socket can not receive data

I am trying to work with sockets in android programming, sending to the server works fine but i have problems with receiving a string from the server. 我正在尝试使用android编程中的套接字,发送到服务器工作正常,但从服务器接收字符串时遇到问题。

the socket is connected, but if the server is sending a string the android app only prints out: java.io.BufferedReader@418daee0 套接字已连接,但是如果服务器正在发送字符串,则android应用只会打印出:java.io.BufferedReader@418daee0

i dont know what that mean..is it an error or something like that? 我不知道那是什么意思..这是错误还是类似的东西?

this is my code: Maybe someone could help me a little bit to get that work. 这是我的代码:也许有人可以帮我一点忙。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Client extends Activity {

    private Socket socket;


    private static final int SERVERPORT = 2000;
    private static final String SERVER_IP = "192.168.1.1";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      



        new Thread(new ClientThread()).start();
    }


    class ClientThread implements Runnable {

        @Override
        public void run() {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);

            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {


                InputStream in = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String test = br.toString();

                TextView message = (TextView) findViewById(R.id.received);
                message.setText(test);

                //in.close();

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

        }

    }

problem: 问题:

String test = br.toString();

You are basically referencing the memory location in string form of the br object thus printing the memory location in your TextView . 基本上,您是以br对象的字符串形式引用内存位置,从而在TextView打印内存位置。 What you need to do is to read the inputStream from the socket connection of the server from your BufferedReader object. 您需要做的是从BufferedReader对象的服务器的套接字连接中读取inputStream。

solution: 解:

String test = br.readLine();

The readLine method will wait for the response of the server from the InputStream where the socket is connected. readLine方法将等待服务器从连接套接字的InputStream响应。 Now the above will only get one line of response from the server if the server sends multiple line then you put it in a while loop. 现在,如果服务器发送多行,则将上面的内容从服务器获得一行响应,然后将其置于while循环中。

what you need to do is read from the BufferedReader . 您需要做的是从BufferedReader读取。 Currently you are just printing the br object. 当前,您只在打印br对象。

BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine ()) != null) {
   System.out.println (line);
}

as per the javadocs readLine will return null if the end of the stream has been reached 按照javadocs readLine将返回null,如果已到达流的末尾

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

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