如果无法获得 websocket 连接,则退出应用程序的逻辑
Logic to exit application if it cannot get websocket connection
我有一个 c 程序,它使用 libwebsockets
打开从客户端到服务器的 websocket。但是,有时网络可能会关闭。在这种情况下,我在下面写了一些逻辑,尝试连接 10 次,如果不成功,那么它应该退出应用程序。逻辑按预期工作,但看起来不是很整洁,我相信有更好的方法。对于如何更好地编写此逻辑的代码,我将不胜感激。 exit(-1)
也是正确的退出方式吗?
//Try connect to websocket 10 times or else exit app
for (int j =0; j < 10; j++) {
web_socket = lws_client_connect_via_info ( &ccinfo);
//If you get a connection then exit the for loop
if(web_socket){
break;
}
}
//If you tried 10 times web_socket will be NULL so exit the application
if(!web_socket) {
exit(-1);
}
exit 从 stdlib.h
是最好的方法。从标准
Terminates the process normally, performing the regular cleanup for
terminating processes.
First, all functions registered by calls to atexit are executed in the
reverse order of their registration. Then, all streams are closed and
the temporary files deleted, and finally the control is returned to
the host environment.
The status argument is returned to the host environment.
我觉得代码逻辑简单,大家看得懂
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." - Donald E. Knuth
我有一个 c 程序,它使用 libwebsockets
打开从客户端到服务器的 websocket。但是,有时网络可能会关闭。在这种情况下,我在下面写了一些逻辑,尝试连接 10 次,如果不成功,那么它应该退出应用程序。逻辑按预期工作,但看起来不是很整洁,我相信有更好的方法。对于如何更好地编写此逻辑的代码,我将不胜感激。 exit(-1)
也是正确的退出方式吗?
//Try connect to websocket 10 times or else exit app
for (int j =0; j < 10; j++) {
web_socket = lws_client_connect_via_info ( &ccinfo);
//If you get a connection then exit the for loop
if(web_socket){
break;
}
}
//If you tried 10 times web_socket will be NULL so exit the application
if(!web_socket) {
exit(-1);
}
exit 从 stdlib.h
是最好的方法。从标准
Terminates the process normally, performing the regular cleanup for terminating processes.
First, all functions registered by calls to atexit are executed in the reverse order of their registration. Then, all streams are closed and the temporary files deleted, and finally the control is returned to the host environment.
The status argument is returned to the host environment.
我觉得代码逻辑简单,大家看得懂
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." - Donald E. Knuth