提问者:小点点

非更新套接字消息


我一直在努力在我的Raspberry Pi和我的手机之间建立连接,为了实现它,我想使用插座。 凭着我极少的经验,我在互联网上四处搜寻,试图为我的Pi(C++)和我的手机(Java)找到一个Android应用程序的套接字代码。 这两项都列于下文。

我遇到的问题是,当从C++服务器发送新消息时,手机上的文本框不会更新。 我相信我用C++写的代码是可以的。 它所要做的就是不断要求用户提供新的输入,以便发送到客户端。

我相信问题在于我手机的Java应用程序。

我完全不知道我在做什么,我希望这里有人能用简单的术语解释我正在发生什么(或者说没有发生什么)。

提前谢谢你!

C++服务器代码:

#include <iostream>
#include <boost/asio.hpp>

using namespace boost::asio;
using ip::tcp;
using std::string;
using std::cout;
using std::endl;

string read_(tcp::socket & socket)
{
boost::asio::streambuf buf;
boost::asio::read_until(socket, buf, "\n" );
string data = boost::asio::buffer_cast<const char*>(buf.data());
return data;
}

void send_(tcp::socket & socket, const string& message)
{
const string msg = message + "\n";
boost::asio::write(socket, boost::asio::buffer(message));
}

int main()
{
boost::asio::io_service io_service;

tcp::acceptor acceptor_(io_service, tcp::endpoint(tcp::v4(), 41517));

tcp::socket socket_(io_service);

acceptor_.accept(socket_);
/*
string message = read_(socket_);
cout << message << endl;
*/
send_(socket_, "Hello from server!\n");
cout << "Server sent hello message to Client!" << endl;

char message[400];

while(true)
{
std::cin.getline(message, 400);
string messageStr = message;

if(messageStr.length() > 0)
{
send_(socket_, messageStr);
cout << "The following message has been sent:" << endl << endl;
cout << messageStr << endl << endl << "-----------------------------" << endl << endl;
}

else
{
cout << "No message has been sent." << endl << endl << "-----------------------------" << endl << endl;
}
}

return 0;
}

Java客户代码

package com.example.socket_test2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.SocketAddress;

import static java.lang.Integer.parseInt;

public class MainActivity extends AppCompatActivity {

    TextView responseField;
    EditText ipField, portField;
    Button randomButton;

    ConnectionMaker juin = new ConnectionMaker();

    Socket sock = new Socket();

    BufferedReader br;

    private class ConnectionMaker extends AsyncTask<String, Integer, Integer> {
        @Override
        protected Integer doInBackground(String... strings) {

            try {
                SocketAddress addr = new InetSocketAddress(strings[0], parseInt(strings[1]));
                sock.connect(addr);

                br = new BufferedReader(new InputStreamReader(sock.getInputStream()));

                while(true) {
                    responseField.setText(br.readLine());
                }

                //sock.close();
            } catch (Exception ex) {
                responseField.setText(ex.getLocalizedMessage());
            }



            return 1;
        }
    }

    View.OnClickListener action = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try{
                juin.execute(ipField.getText().toString(), portField.getText().toString());
            } catch (Exception ex) {
                responseField.setText(ex.getLocalizedMessage());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        responseField = findViewById(R.id.ResponseField);
        randomButton = findViewById(R.id.RandomButton);

        ipField = findViewById(R.id.IP_Field);
        portField = findViewById(R.id.Port_Field);

        randomButton.setOnClickListener(action);
    }
}

共1个答案

匿名用户

AsyncTaskDoInBackground方法不在UI线程上运行,因此不允许在那里调用ResponseField.SetText。 或者更确切地说:您可以调用它,但不能保证UI线程会看到它。

相反,使用PublishProgres,其OnProgressUpdate处理程序在UI线程上调用:

private class ConnectionMaker extends AsyncTask<String, String, Integer> {
    @Override
    protected Integer doInBackground(String... strings) {

        try {
            SocketAddress addr = new InetSocketAddress(strings[0], parseInt(strings[1]));
            sock.connect(addr);

            br = new BufferedReader(new InputStreamReader(sock.getInputStream()));

            while(true) {
                publishProgress(br.readLine());
            }

            //sock.close();
        } catch (Exception ex) {
            responseField.setText(ex.getLocalizedMessage());
        }

        return 1;
    }

    @Override
    protected void onProgressUpdate(String... progress) {
        responseField.setText(progres[0]);
    }
}