gpt4 book ai didi

java - 在 AsynchronousSocketChannel Reas/Connection Handler 中找不到符号

转载 作者:行者123 更新时间:2023-12-02 10:18:41 24 4
gpt4 key购买 nike

我正在尝试通过 AsynchronousSocketChannel 使用 Java 在 Android 中开发 TCP/IP 通信应用程序。我在读取处理程序中收到私有(private)数据成员的符号未找到错误。我之前没有 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 :

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

最佳答案

在错误发生时,this 指的是您使用 new CompletionHandler ... 创建的匿名类,并且该类没有成员 socketAlive .

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

关于java - 在 AsynchronousSocketChannel Reas/Connection Handler 中找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54491075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com