Android:套接字客户端未收到来自服务器

时间:2016-04-05 12:18:49

标签: android sockets

我已经阅读了很多相同问题的帖子,因为我们正在阅读BufferedReader.readLine()行,所以我们必须从服务器发送"\n",以便android客户端将其作为新行。但我的问题是我无法更改我的服务器代码,因为它也与桌面应用程序通信,它已经编码。有没有办法在不添加"\n"的情况下从服务器读取消息?

Android client class

public class TCPClient {

private String serverMessage;


public static String SERVERIP = "192.168.0.1"; // controllers IP address
public static final int SERVERPORT = 10001;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
private PrintWriter out = null;
private BufferedReader in = null;


public TCPClient(final OnMessageReceived listener) 
{
    mMessageListener = listener;
}

/**
  * Sends the message entered by client to the server
  * @param message text entered by client
  **/
public void sendMessage(String message)
{
    if (out != null && !out.checkError())
    {
        System.out.println("message: "+ message);
        out.println(message);
        out.flush();
    }
}

public void stopClient()
{
    mRun = false;
}

public void run() {

    mRun = true;

    try {
        if(!MainActivity.STATIC_IP.equals(""))
        {
            SERVERIP = MainActivity.STATIC_IP;
        }

        InetAddress serverAddr = InetAddress.getByName(SERVERIP);
        Log.e("TCP SI Client", "SI: Connecting...");

        Socket socket = new Socket(serverAddr, SERVERPORT);


        try {

            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            Log.e("TCP SI Client", "SI: Sent.");
            Log.e("TCP SI Client", "SI: Done.");
            Log.e("Out Message",out.toString());

            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));


            while (mRun)
            {
                serverMessage = in.readLine();

                if (serverMessage != null && mMessageListener != null) {

                    mMessageListener.messageReceived(serverMessage);
                    Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
                }
                serverMessage = null;
            }
        }
        catch (Exception e)
        {

            Log.e("TCP SI Error", "SI: Error", e);
            e.printStackTrace();
        }
        finally
        {
            socket.close();
        }

    } catch (Exception e) {
        Log.e("TCP SI Error", "SI: Error", e);
    }

}

    public interface OnMessageReceived {
       public void messageReceived(String message);
    }
}

其他客户(Python)如何收到消息。

data = self.sock.recv(1024)

服务器发送的是命令@alarm:On#, @alarm:Off#每秒4个命令,最后没有任何换行符。

2 个答案:

答案 0 :(得分:3)

谢谢@EJP你的线索帮助我理清issue。 这是工作code

try {
    out = new PrintWriter(socket.getOutputStream());
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));     
    int charsRead = 0;
    char[] buffer = new char[BUFFER_SIZE];
    while (mRun)
    {
         charsRead = in.read(buffer);
         serverMessage = new String(buffer).substring(0, charsRead);
         if (serverMessage != null && mMessageListener != null) {
             Log.i("Message from Server", serverMessage.toString());
         }
     }
}catch (Exception e){
     e.printStackTrace();
}

答案 1 :(得分:-2)

您应该自己完成Sockets上的Java教程,以便更好地了解如何使用它们。

由于您的问题建议您无法知道服务器消息何时结束,因此我使用缓冲流并计算可读字符,然后将这些字符读入数组并将其转换为字符串。