【发布时间】:2017-08-13 20:41:36
【问题描述】:
我有一个 c 程序,它使用 libwebsockets 打开从客户端到服务器的 websocket。但是,有时网络可能会关闭。在这种情况下,我在下面写了一些逻辑,尝试连接 10 次,如果不成功,则应该退出应用程序。逻辑按预期工作,但看起来不是很整洁,我确信有更好的方法来做到这一点。我很感激 cmets 如何更好地编码这个逻辑。还有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(EXIT_FAILURE);更具描述性和便携性。
标签: c loops websocket logic exit