在AsynchronousSocketChannel重做/连接处理程序中找不到符号

时间:2019-02-02 07:40:55

标签: java android

我正在尝试通过Java通过AsynchronousSocketChannel使用Java开发TCP / IP通信应用程序。我的读取处理程序中的私有数据成员遇到符号未找到错误。 我在Android应用程序开发或Java中没有现成的经验,但我在C ++中做了大量工作。 该方案是基于模板由该工作正常的C ++应用程序Boost.Asio的。 操作系统-Windows 10 64位 目标平台-Android Things Raspberry Pi 1.08

public class Ethernet {
    private String                      ip;
    private int                         portNo;
    private Queue<String>               recvQueue, sendQueue;
    private InetAddress                 address;
    private String                      readData;
    private InputStream                 in;
    private OutputStream                out;
    private AsynchronousSocketChannel   socket;
    private boolean                     socketAlive, connectionInProgress;
    private ByteBuffer                  readBuffer;

    Ethernet(String ip, int port) {
        if (port > 65535 && port < 1024)
            port = 6666;
        this.ip = ip;
        this.portNo = port;
        this.socketAlive = false;
        this.connectionInProgress = false;
        this.readBuffer = ByteBuffer.allocate(8192);
    }

    private void recieveDataSocket(){
        this.socket.read(this.readBuffer, null, new CompletionHandler<Integer, Object>() {
            @Override
            public void completed(Integer result, Object attachment) {
                if (result < 0) {
                    this.socketAlive = false;  //Error
                }
                else if (this.readBuffer.remaining() > 0) {
                    // repeat the call with the same CompletionHandler
                    this.socket.read(this.readBuffer, null, this);//Error
                }
                else {
                    // got all data, process the buffer
                }
            }
            @Override
            public void failed(Throwable e, Object attachment) {
                // handle the failure
            }
        });
    } }
  

错误:

error: cannot find symbol
this.socketAlive = false;
^  symbol: variable socketAlive  error: cannot find symbol
else if (this.readBuffer.remaining() > 0) {
^  symbol: variable readBuffer  error: cannot find symbol
this.socket.read(this.readBuffer, null, this);
^   symbol: variable readBuffer   error: cannot find symbol
this.socket.read(this.readBuffer, null, this);
 ^   symbol: variable socket   error: cannot find symbol
eth.disconnect();
^   symbol:   method disconnect()   location: variable eth of type Ethernet 5 errors

1 个答案:

答案 0 :(得分:1)

在错误的点this是指你与new CompletionHandler ...创建匿名类和这个类不具有构件socketAlive

要解决此问题,只需省略this.