- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个客户端/服务器 TCP/IP 程序,在服务器生成一个更大的文件之前它运行良好。那时它坏了,虽然不是一直坏,但经常足以让它成为一个真正的问题。我写了一个只有 TCP/IP 的小版本,它重现了我觉得非常令人印象深刻的问题。
我尝试了各种方法来解决这个问题,但我不太确定下一步该怎么做!
/*
* Test showing that at times a socket close() + exit(0) prevents all the
* data from reaching the client.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QCoreApplication>
#include <QByteArray>
#include <QFile>
#include <QTcpSocket>
#include <QTcpServer>
#include <QHostAddress>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
void
client()
{
QHostAddress a("192.168.2.1");
int port = 4004;
QTcpSocket socket;
socket.connectToHost(a, port);
if(socket.waitForConnected())
{
// if we get here then we can just copy the output of the child to Apache2
// the wait will flush all the writes as necessary
if(!socket.waitForReadyRead())
{
fprintf(stderr, "error: waitForReadyRead() timed out.\n");
exit(1);
}
// buffer somewhere to check the output validity
FILE *f = fopen("/tmp/out.html", "w");
if(f == NULL)
{
fprintf(stderr, "error: could not create /tmp/out.html file.\n");
exit(1);
}
for(;;)
{
char buf[64 * 1024];
qint64 r(socket.read(buf, sizeof(buf)));
if(r > 0)
{
fwrite(buf, r, 1, f);
}
else if(r == -1)
{
fprintf(stderr, "error: an error occured while read()'ing from the socket.\n");
break;
}
else if(r == 0)
{
// no more data (since our socket is blocking this really
// only happens when no more data is available)
break;
}
}
fclose(f);
return;
}
fprintf(stderr, "error: client could not connect to server.\n");
exit(1);
}
void
server()
{
QTcpServer s;
QHostAddress a("0.0.0.0");
if(!s.listen(a, 4004))
{
fprintf(stderr, "error: server is not able to listen, port 4004 not available?\n");
exit(1);
}
pid_t pid(0);
for(;;)
{
if(pid > 0)
{
int status;
if(waitpid(pid, &status, WNOHANG) == pid)
{
pid = 0;
}
}
s.waitForNewConnection(-1);
if(pid > 0)
{
int status;
if(waitpid(pid, &status, 0) == pid)
{
pid = 0;
}
}
QTcpSocket *socket(s.nextPendingConnection());
pid = fork();
if(pid == 0)
{
// this makes it work a bit better but we still have the
// early disconnection
int optval(1);
socklen_t optlen(sizeof(optval));
if(setsockopt(socket->socketDescriptor(), SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
fprintf(stderr, "warning: could not mark the new connection with keepalive flag.\n");
}
// we are the child, write in the socket and close it
QFile f("example.html");
f.open(QIODevice::ReadOnly);
QByteArray buffer(f.readAll());
f.close();
socket->write(buffer);
// the sleep makes it slow, but it doesn't make any difference
//sleep(2);
socket->flush();
// extra sleep/flush don't make any difference
//sleep(1);
//socket->flush();
//sleep(1);
//socket->flush();
// the read doesn't seem to fix anything
socket->waitForReadyRead();
socket->disconnectFromHost();
delete socket;
exit(0);
}
else {
if(pid == -1) {
fprintf(stderr, "error: could not create child process to handle the socket.\n");
}
socket->close();
}
}
}
int
main(int argc, char *argv[])
{
if(argc == 1)
{
client();
}
else if(strcmp(argv[1], "-s") == 0)
{
server();
}
else
{
printf("Usage: %s [-s]\n", argv[0]);
exit(1);
}
exit(0);
}
我使用 CMakeLists.txt 文件来编译服务器和客户端。一旦编译,我们就有了一个可执行文件:tcp-bug。以 -s 作为服务器启动它。然后在不带 -s 的不同控制台中启动相同的可执行文件。我写了一个 shell 脚本来重复测试,因为它一般不会第一次发生,虽然它一般在 20 次以内。
这是我的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.8)
project(tcp-bug)
find_package(Qt4 4.8.1 REQUIRED QtCore QtNetwork)
include(${QT_USE_FILE})
include_directories(${QT_INCLUDES})
add_definitions(${QT_DEFINITIONS})
add_executable(tcp-bug tcp-bug.cpp)
target_link_libraries(tcp-bug ${QT_LIBRARIES})
set(CPACK_PACKAGE_NAME "tcp-bug")
set(CPACK_SOURCE_IGNORE_FILES ".swp$;/BUILD/")
include(CPack)
还有我的 shell 脚本:
#!/bin/sh
for g in a b c d e f g h i j k l m n o p q r s t u v w x y z
do
for f in 1 2 3 4 5 6 7 8 9 10
do
BUILD/tcp-bug
if [ `stat -c %s /tmp/out.html` -ne 41812 ]
then
echo "Error occured! Iteration: $g - $f"
ls -l /tmp/out.html
exit 1
fi
done
done
请注意,该脚本需要在下一页的 .tar.gz 中提供一个恰好 41,812 字节的文件(您也可以创建自己的 example.html 文件,服务器读取并发送给客户端的内容):
http://linux.m2osw.com/breaking-tcpip-simple-clientserver-implementation-qt
最佳答案
添加一个答案,因为我认为很多人会遇到类似的问题,我已经解决了它......尽管那是根本不使用 Qt。
问题是 Qt 打开套接字并将其置于 O_NONBLOCK
模式,因此,它将忽略正确的关闭信号/刷新。当您点击 exit(0)
时,它会立即关闭,并且客户端不会刷新任何内容,因此服务器无法获取最终数据,反之亦然。
当时,我试图删除 O_NONBLOCK
认为这可能有所帮助,但出于某种原因(至少在 2012 年左右),它不会真正回到阻塞套接字。所以那是没用的。如果您对套接字不小心,阻塞也可能导致其他问题(即您的应用程序可能会阻止尝试从套接字读取或写入)。
我的解决方案是实现我自己的事件库,现在称为 eventdispatcher .该库支持 TCP、UDP、Unix 套接字(DGRAM/Stream),也可用于监视文件更改、Unix 信号、线程终止、线程间通信。所有这些连接都支持定时器,定时器也可以单独使用(没有套接字连接)。每个连接还支持消息实现,这意味着您可以将库用作 RPC 系统。该消息可以是我们内部基于文本的消息(优化)或 JSON(更大的消息)。这工作得很好,我真的不后悔为了所有网络功能而放弃 Qt。
为了在 Qt 应用程序中支持这些类型的连接,我还有一个特殊的 Qt 连接,它使用 Qt 套接字(实际上是 X11),因此它仍然可以与 Qt 一起使用。只是有点不同。使用 eventdispatcher,您必须在它完全退出之前删除所有连接,因此它使正确清理套接字变得容易得多,因为它变得显式。
关于qt - 服务器 QTcpSocket "breaks"太快了,客户端没有收到所有数据,知道为什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13971048/
是否有效率偏好以下控制流选项之一用于循环或切换另一个? 选项 1: switch(...){ case 1: if (...) { ... } else if
我需要在某个地方修复一些 CSS,因为我的文本没有环绕,如果它是一个非常长的单词,它会无限期地继续下去。 在大多数情况下,我在我的 CSS 文件中尝试了 word-wrap: break-word;
这个问题在这里已经有了答案: What is the difference between "word-break: break-all" versus "word-wrap: break-word
我的问题: overflow-wrap: break-word 和 word-break: break-word 有区别吗? 非重复: 这里有一些现有的问题,乍一看可能是重复的,但实际上不是。 Wha
我目前想知道两者之间有什么区别。当我同时使用它们时,如果它不适合容器,它们似乎会打破这个词。但为什么 W3C 做了两种方式来做呢? 最佳答案 word-wrap: break-word 最近更改为 o
满足条件时如何跳出循环? 例如: for (i in 0..10){ if (i==3){ // equivalent of break } } 最佳答案 Q#没有中
CSS3 规范说部分支持 word-wrap:break-word; 在 Chrome 中你必须使用 word-wrap:break-all; 但是没有连字符。我正在使用 text-align:jus
我想在我的 View 列表中显示来自数据库的所有评论。但有时,评论会很长。我试图用 word-wrap:break-word 来“打破”它们,但没有结果——像这样的评论 aaaaaaaaaaaaaaa
我正在尝试将一个长 URL 放置在侧边栏中,以在表格单元格宽度的范围内中断和换行。我已将 style="word-break:break-all;" 添加到 td 和围绕文本和 URL 的 span
我有以下代码: public void post(String message) { final String mess = message; (new Thread() {
是否有 word-break: break-word 的 alternative 可以在 firefox 中使用? 在 firefox[版本 57.0] 中 在 Chrome [版本 62.0.320
在this question以及this blog ,其中提到了样式 word-break 及其值。此外,还有值 break-word 作为属于例如的东西。 自动换行类。 在最近的 VS 中 MVC
我正在尝试将文本包装在 td 中并使用以下样式 word-break:break-all 在 IE 中工作得很好,但在 Firefox 中失败,请注意这是不支持的!尝试了 http://petesbl
我正在使用“word-break: break-all”在任何字符之间插入分隔符。 但是,在 Chrome 中,它无法正常处理特定字符(例如冒号、分号、逗号),如下所示。 (我的代码笔在这里:http
它只有在我用不同的元素包裹 anchor 标记时才有效,但我正在阅读 html,所以我想将它添加为整个页面的样式。 最佳答案 我不确定你想在这里实现什么,如果你能分享一些代码片段会很好。 无论如何,我
这些看起来都是一样的,有什么区别? https://jsfiddle.net/pmuub8w1/2/ p{ word-break:normal; } p{
目前下面的代码,保持div宽度固定,换行由于break-all .Test{ float: left; margin-bottom: 2px; word-wrap:break-word; word-b
我正在使用 word-break: break-all; 并想知道如何让浏览器自动插入 hyphens ,如 MDN example 中所示. div { width: 80px; heigh
我的 html 有这种结构: content1 content2 content3 content4 content5 content6 co
我正在尝试对齐图像旁边的文本。我想考虑没有空格的长字符串。 我的css如下 .new_item { width: 100%; float: left; border-bottom: 1px solid
我是一名优秀的程序员,十分优秀!