Git Product home page Git Product logo

Comments (5)

wangluolaowu avatar wangluolaowu commented on July 20, 2024

应用程序关于mqttclient的配置,除了服务器地址,用户名,密码这些必须的外,其它都是默认配置(我是参考例程写的);

from mqttclient.

jiejieTop avatar jiejieTop commented on July 20, 2024

首先,非常感谢您对这个项目的认可,也非常感谢提出这个测试的问题,

不知道这个issue是否对您有帮助,可以先看看#19

感觉现象有点像,可能是由于网络的原因,导致服务器将客户端主动断开了,

如果是服务端主动发起关闭,此时四次挥手的顺序会颠倒。那么此时客户端再向服务端发送数据时,根据TCP协议的规定,认为它是一个异常终止连接,客户端将会收到一个RST复位响应(而不是ACK响应),如果客户端再次向服务端发送数据,系统将会发送一个SIGPIPE信号给客户端进程,告诉客户端进程该连接已关闭,不要再写了。系统给SIGPIPE信号的默认处理是直接终止收到该信号的进程,所以此时客户端进程会被极不情愿地终止。
如果不希望客户端进程被终止,可以自定义一个该信号处理的函数,通过调用函数signal(SIGPIPE, handler)实现对信号的处理,其中handler就是可以自定义的函数。
所以说当服务端主动关闭,客户端继续写两次将会导致客户端进程被终止(服务端并不能接收),客户端不能向服务器写入数据。

进程会终止,是因为服务器主动close后,客户端还往服务器写数据。

在你的测试代码里面去添加这些测试代码,你就能捕获到异常情况了:

#include <signal.h>

....


void handler(int h)
{
    printf("signal handler\n");
}

....

int main(void)
{

....

    signal(SIGPIPE, handler);

....

}

....

from mqttclient.

wangluolaowu avatar wangluolaowu commented on July 20, 2024

谢谢大咖的回复,根据你的提示,在测试例程中加上signal(SIGPIPE, handler);捕获到异常情况了; 从打印信息来看,此时mqtt_get_client_state状态为仍为CLIENT_STATE_CONNECTED,程序就会进入到这个地方:
` /* mqtt connected, handle mqtt packet */
rc = mqtt_packet_handle(c, &timer);

    if (rc >= 0) {
        /* scan ack list, destroy ack handler that have timed out or resend them */
        mqtt_ack_list_scan(c, 1);`

已经确认mqtt服务端没有做什么特殊配置,基本上都是默认参数;我现在的问题是:接下来怎么处理这种情况,怎样才能重新连上服务器,正常发布消息?

from mqttclient.

wangluolaowu avatar wangluolaowu commented on July 20, 2024

我现在的处理方式是,当捕获到该异常情况后,在void handler(int h)函数中增加int mqtt_try_reconnect(mqtt_client_t* c);利用该函数进行重连,但一直打印的信息是:[W] >> ../mqttclient/mqttclient.c:603 mqtt_try_do_reconnect()... mqtt try connect result is -0x0004

from mqttclient.

wangluolaowu avatar wangluolaowu commented on July 20, 2024

谢谢大咖的回复;我目前的解决办法,捕获到SIGPIPE信号后, 先释放掉socket连接,然后通过mqtt_set_client_state()设置client状态为:CLIENT_STATE_DISCONNECTED;
` /must realse the socket file descriptor/
network_release(client->mqtt_network);

/* record too much retransmitted data, may be disconnected, need to reconnect */
mqtt_set_client_state(client, CLIENT_STATE_DISCONNECTED);

`

from mqttclient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.