3

我编写了一个客户端/服务器 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/break-tcpip-simple-clientserver-implementation-qt

4

0 回答 0