Android网络连接.可以写数据,但不能读数据.使用QT服务器
Android network connection. Can write data but cannot read it. Using QT Server
我正在遵循一个修改的例子,我在网上找到的,以便与我在我的PC(使用Qt)中编写的程序进行通信。当接收到数据时,Qt函数真的很简单
void FileTransferServer::on_dataReady(){
QByteArray buffer = socket->readAll();
ui->teLog->append("Received: " + QString(buffer));
QByteArray data;
QString msg = "Soy Qt!!";
data = msg.toLatin1();
qint32 value = socket->write(data);
ui->teLog->append("Sending reply of " + QString::number(value) + " bytes");
}
在android中,我已经为TCP客户端实现了AsyncTask。客户端主要功能的代码如下:
public void run(){
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVER_ADDRESS);
System.err.println("Attempting connection...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVER_PORT);
try {
//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
System.err.println("Connection successful attempting to send message");
//To receive messages
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int i = 0;
while (i < 10) {
i++;
out.println("This is Android!!");
// Waiting for response.
boolean msgRead = false;
String msg;
while (!msgRead) {
System.err.println("Attempting to read a message");
msg = in.readLine();
System.err.println("Message received: " + msg);
if (msg != null) {
//output.append("Reply: " + msg);
System.err.println("Reply: " + msg);
msgRead = true;
}
}
}
System.err.println("All done!");
} catch (Exception e) {
System.err.println("Error on sending message: " + e.getMessage());
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
System.err.println("Error on connection: " + e.getMessage() + " And also " + e.toString());
}
我的问题是,即使我可以从电脑发送数据到我的手机,它不能反过来工作。
这是我的控制台输出(用于我的手机)
09-06 20:54:32.619 31231-31245/? W/System.err: Attempting connection...
09-06 20:54:32.629 31231-31245/? W/System.err: Connection successful attempting to send message
09-06 20:54:32.629 31231-31245/? W/System.err: Attempting to read a message
而我的简单Qt App的输出是:
Accepted connection from ::ffff:192.168.1.107. Local address is ::ffff:192.168.1.110
Received: This is Android!!
Sending reply of 8 bytes
但是,程序在尝试读取消息时挂起,如上所示。
当我退出Qt程序时,我得到了大量的这些
09-06 20:56:39.569 31231-31245/cmp.cmpreceiver W/System.err: Attempting to read a message
09-06 20:56:39.569 31231-31245/cmp.cmpreceiver W/System.err: Message received: null
谁能告诉我我做错了什么? 由于Nagle的算法(即TCP_NODELAY),您的有效负载可能太小而无法立即发送。
你可能需要像这样禁用它:
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
相关文章:
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- 为什么我不能从结构将数据输入到字符数组中?
- 为什么静态数据成员不能在c++11中的类中初始化
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 为什么不能使用"( )"为类的非静态数据成员提供默认值?
- 为什么我的节点临时节点不能显示正确的数据?
- 什么形式上保证了非原子变量不能看到凭空出现的值,并像理论上原子一样创造数据竞赛?
- 为什么SDL_RWFromFile(文件,"w+")不能像它应该的那样读写?它只打开文件进行写入
- 为什么Ubuntu c++中的eclipse不能打印出数据
- C++为什么 int 变量可以声明为类的私有数据成员,但不能声明为字符串变量
- 蓝牙可以接收数据,但不能传输数据(用C++进行套接字编程以与Matlab通信)
- 为什么初始化捕获可变 lambda 不能具有可变数据成员?
- 为什么我不能从输入文件中检索简单数据并使用C++将其写入输出文件?
- 提升ASIO TCP,为什么我不能在服务器端只有一个数据套接字可以打开和关闭
- C/C++嵌入python模块PIL,python函数想返回所有像素数据,但我为什么不能?
- 为什么类数据成员不能通过直接初始化语法初始化?
- 不能将Const数据类型分配到非Const数据类中
- 为什么我不能在复制构造函数中更改实例的私有数据,如果它是 const 的?
- 通过套接字接收的数据不能用于C++
- Android网络连接.可以写数据,但不能读数据.使用QT服务器