服务器QTcpSocket"中断"太快了,客户端没有收到所有数据,知道为什么?

时间:2012-12-20 11:20:15

标签: qt tcp client-server

我写了一个客户端/服务器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文件编译服务器&amp;客户。编译完成后,我们有一个可执行文件: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文件,服务器读取并发送给客户端的内容):< / p>

http://linux.m2osw.com/breaking-tcpip-simple-clientserver-implementation-qt

0 个答案:

没有答案