网络编程-TCP粘包

//TCP数据传输是以无边界的数据流传输形式,所谓无边界是指数据发送端发送的字节数,
//在数据接收端接受时并不一定等于发送的字节数,可能会出现粘包情况。

//TCP粘包情况:
    //(1)发送方引起的粘包是由TCP协议本身造成的,TCP为提高传输效率,发送方往往要收集到足够多的数据后才发送一包数据。
    //若连续几次发送的数据都很少,通常TCP会根据优化算法把这些数据合成一包后一次发送出去,这样接收方就收到了粘包数据。
//(2)接收方引起的粘包是由于接收方用户进程不及时接收数据,从而导致粘包现象。
//这是因为接收方先把收到的数据放在系统接收缓冲区,用户进程从该缓冲区取数据,若下一包数据到达时前一包数据尚未被用户进程取走,
//则下一包数据放到系统接收缓冲区时就接到前一包数据之后,而用户进程根据预先设定的缓冲区大小从系统接收缓冲区取数据,这样就一次取到了多包数据。

//解决办法:
//(1)发送固定长度的消息
//(2)把消息的尺寸与消息一块发送
//(3)使用特殊标记来区分消息间隔

//下面介绍一种方法:
/*1. 数据头:数据包的大小,固定长度。
2. 数据内容:数据内容,长度为数据头定义的长度大小。
实际操作如下:
a)发送端:先发送数据包的大小,再发送数据内容。
b)接收端:先解析本次数据包的大小N,在读取N个字节,这N个字节就是一个完整的数据内容。*/

/*demo讲解:
1. 客户端负责模拟发送数据,服务端负责接受数据,处理粘包问题

a)emulate_subpackage

模拟情况1,一个长数据经过多次才到达目的地,

在客户端字符串“This is a test case for client send subpackage data. data is not send complete at once.”
每次只发送6个字节长度。服务端要把字符串集满才能处理数据(打印字符串)

b)emulate_adheringpackage

模拟情况2,多个数据在一次性到达目的地

在客户端将字符串“Hello I’m lucky. Nice too me you”切成三个数据段(都包含数据头和数据内容),
然后一次性发送,服务端读取数据时对三个数据段逐个处理。*/

bool readPack(int sock, char* buf, size_t len) {    
    if (NULL == buf || len < 1) {    
        return false;    
    }    
    memset(buf, 0, len); // only reset buffer len.    
    ssize_t read_len = 0, readsum = 0;    
    do {    
        read_len = read(sock, buf + readsum, len - readsum);    
        if (-1 == read_len) { // ignore error case    
            return false;    
        }    
        printf("receive data: %s\n", buf + readsum);    
        readsum += read_len;    
    } while (readsum < len && 0 != read_len);    
    return true;    
}  

案例

client.cpp

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <time.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

void safe_close(int &sock);
void emulate_subpackage(int sock);
void emulate_adheringpackage(int sock);

int main(int argc, char *argv[]) {
    char buf[128] = {0};
    int sockfd = -1;
    struct sockaddr_in serv_addr;

    // Create sock
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd) {
        printf("new socket failed. errno: %d, error: %s\n", errno, strerror(errno));
        exit(-1);
    }

    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(7890);

    // Connect to remote server
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("connection failed. errno: %d, error: %s\n", errno, strerror(errno));
        exit(-1);
    }
    emulate_subpackage(sockfd);
    emulate_adheringpackage(sockfd);

    const int HEAD_SIZE = 9;
    const char temp[] = "exit";
    memset(buf, 0, sizeof(buf));
    snprintf(buf, sizeof(buf), "%0.*zu", HEAD_SIZE - 1, sizeof(temp));
    write(sockfd, buf, HEAD_SIZE);
    write(sockfd, temp, sizeof(temp));

    printf("send complete.\n");
    memset(buf, 0, sizeof(buf));
    read(sockfd, buf, sizeof(buf));
    printf("receive data: %s\n", buf);
    printf("client finish.\n");

    safe_close(sockfd);
    return 0;
}

void safe_close(int &sock) {
    if (sock > 0) {
        close(sock);
        sock = -1;
    }
}

/**
 * emulate socket data write multi part.
 */
void emulate_subpackage(int sock) {
    printf("emulate_subpackage...\n");
    char text[] = "This is a test case for client send subpackage data. data is not send complete at once.";
    const size_t TEXTSIZE = sizeof(text);
    ssize_t len = 0;
    size_t sendsize = 0, sendsum = 0;

    const int HEAD_SIZE = 9;
    char buf[64] = {0};
    snprintf(buf, HEAD_SIZE, "%08zu", TEXTSIZE);
    write(sock, buf, HEAD_SIZE);
    printf("send data size: %s\n", buf);

    do {
        sendsize = 6;   //六个字节多次发送
        if (sendsum + sendsize > TEXTSIZE) {
            sendsize = TEXTSIZE - sendsum;
        }
        len = write(sock, text + sendsum, sendsize);
        if (-1 == len) {
            printf("send data failed. errno: %d, error: %s\n", errno, strerror(errno));
            return;
        }
        memset(buf, 0, sizeof(buf));
        snprintf(buf, len + 1, text + sendsum);
        printf("send data: %s\n", buf);
        sendsum += len;
        sleep(1);
    } while (sendsum < TEXTSIZE && 0 != len);
}

/**
 * emualte socket data write adhering.
 */
void emulate_adheringpackage(int sock) {
    printf("emulate_adheringpackage...\n");
    const int HEAD_SIZE = 9;
    char buf[1024] = {0};
    char text[128] = {0};
    char *pstart = buf;

    // append text
    memset(text, 0, sizeof(text));
    snprintf(text, sizeof(text), "Hello ");
    snprintf(pstart, HEAD_SIZE, "%08zu", strlen(text) + 1);
    pstart += HEAD_SIZE;
    snprintf(pstart, strlen(text) + 1, "%s", text);
    pstart += strlen(text) + 1;

    // append text
    memset(text, 0, sizeof(text));
    snprintf(text, sizeof(text), "I'm lucky.");
    snprintf(pstart, HEAD_SIZE, "%08zu", strlen(text) + 1);
    pstart += HEAD_SIZE;
    snprintf(pstart, strlen(text) + 1, "%s", text);
    pstart += strlen(text) + 1;

    // append text
    memset(text, 0, sizeof(text));
    snprintf(text, sizeof(text), "Nice too me you");
    snprintf(pstart, HEAD_SIZE, "%08zu", strlen(text) + 1);
    pstart += HEAD_SIZE;
    snprintf(pstart, strlen(text) + 1, "%s", text);
    pstart += strlen(text) + 1;
    write(sock, buf, pstart - buf);
}


server.cpp


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>

void newclient(int sock);
bool readPack(int sock, char* buf, size_t len);
void safe_close(int &sock);

int main(int argc, char *argv[]) {
    int sockfd = -1, newsockfd = -1;
    socklen_t c = 0;
    struct sockaddr_in serv_addr, cli_addr;

    // Create socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd) {
        printf("new socket failed. errno: %d, error: %s\n", errno, strerror(errno));
        exit(-1);
    }

    // Prepare the sockaddr_in structure
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(7890);

    // bind
    if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("bind failed. errno: %d, error: %s\n", errno, strerror(errno));
        exit(-1);
    }

    // listen
    listen(sockfd, 5);

    printf("listening...\n");
    // accept new connection.
    c = sizeof(struct sockaddr_in);
    int i = 0;
    while (i++ < 3) {
        printf("waiting for new socket accept.\n");
        newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, (socklen_t*)&c);
        if (newsockfd < 0) {
            printf("accept connect failed. errno: %d, error: %s\n", errno, strerror(errno));
            safe_close(sockfd);
            exit(-1);
        }
        pid_t pid = fork();
        if (0 == pid) {
            newclient(newsockfd);
            safe_close(sockfd);
            break;
        } else if (pid > 0) {
            safe_close(newsockfd);
        }
    }
    safe_close(sockfd);
    return 0;
}

void newclient(int sock) {
    printf("newclient sock fd: %d\n", sock);
    int datasize = 0;
    const int HEAD_SIZE = 9;
    char buf[512] = {0};
    while (true) {
        memset(buf, 0, sizeof(buf));
        if (! readPack(sock, buf, HEAD_SIZE)) {
            printf("read head buffer failed.\n");
            safe_close(sock);
            return;
        }

        datasize = atoi(buf);
        printf("data size: %s, value:%d\n", buf, datasize);
        memset(buf, 0, sizeof(buf));
        if (! readPack(sock, buf, datasize)) {
            printf("read data buffer failed\n");
            safe_close(sock);
            return;
        }
        printf("data size: %d, text: %s\n", datasize, buf);
        if (0 == strcmp(buf, "exit")) {
            break;
        }
    }
    memset(buf, 0, sizeof(buf));
    snprintf(buf, sizeof(buf), "from server read complete.");
    write(sock, buf, strlen(buf) + 1);
    printf("newclient sockfd: %d, finish.\n", sock);
    safe_close(sock);
}

void safe_close(int &sock) {
    if (sock > 0) {
        close(sock);
        sock = -1;
    }
}

/**
 * read size of len from sock into buf.
 */
bool readPack(int sock, char* buf, size_t len) {
    if (NULL == buf || len < 1) {
        return false;
    }
    memset(buf, 0, len); // only reset buffer len.
    ssize_t read_len = 0, readsum = 0;
    do {
        read_len = read(sock, buf + readsum, len - readsum);
        if (-1 == read_len) { // ignore error case
            return false;
        }
        printf("receive data: %s\n", buf + readsum);
        readsum += read_len;
    } while (readsum < len && 0 != read_len);
    return true;
}

结果:

客户端发送数据
emulate_subpackage...
send data size: 00000088
send data: This i
send data: s a te
send data: st cas
send data: e for
send data: client
send data: send
send data: subpac
send data: kage d
send data: ata. d
send data: ata is
send data: not s
send data: end co
send data: mplete
send data: at on
send data: ce.
emulate_adheringpackage...
send complete.
receive data: from server read complete.
client finish.


服务端模拟接受数据
$ ./server.o 
listening...
waiting for new socket accept.
waiting for new socket accept.
newclient sock fd: 4
receive data: 00000088
data size: 00000088, value:88
receive data: This i
receive data: s a te
receive data: st cas
receive data: e for
receive data: client
receive data: send
receive data: subpac
receive data: kage d
receive data: ata. d
receive data: ata is
receive data: not s
receive data: end co
receive data: mplete
receive data: at on
receive data: ce.
data size: 88, text: This is a test case for client send subpackage data. data is not send complete at once.
receive data: 00000007
data size: 00000007, value:7
receive data: Hello
data size: 7, text: Hello
receive data: 00000011
data size: 00000011, value:11
receive data: I'm lucky.
data size: 11, text: I'm lucky.
receive data: 00000016
data size: 00000016, value:16
receive data: Nice too me you
data size: 16, text: Nice too me you
receive data: 00000005
data size: 00000005, value:5
receive data: exit
data size: 5, text: exit
newclient sockfd: 4, finish.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章