MSG_PEEK (data snooping)

A copy of the receive buffer data can be obtained using the MSG_PEEK option
  • recv() special option, which can be used for data pre-reception
  • When the MSG_PEEK option is specified, 不会清空缓冲区
  • The amount of data (bytes) that can be used to get the receive buffer

当接收缓冲区中没有数据时,MSG_PEEK 也会导致线程阻塞

What does the code below output? Why?

 static char c_temp[1024 * 2] = {0};
char buf[32] = {0];

sleep(1);

r = recv(client, c_temp, sizeof(c_temp), MSG_PEEK);

c_temp[r] = 0;

printf("r = %d\n", r);
printf("c_temp = %s\n", c_temp);

r = recv(client, buf, sizeof(buf), 0);

buf[r] = 0;

printf("r = %d\n", r);
printf("buf = %s\n", buf);
client.c
 #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int sock = {0};
    struct sockaddr_in addr = {0};
    int len = 0;
    char *test = "Delpin-Tang";

    sock = socket(PF_INET, SOCK_STREAM, 0);

    if (sock == -1) {
        printf("socket error\n");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(8888);

    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        printf("connect error\n");
        return -1;
    }

    printf("connect success\n");

    len = send(sock, test, strlen(test), 0);

    getchar();

    close(sock);

    return 0;
}
server.c
 #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int server = 0;
    struct sockaddr_in saddr = {0};
    int client = 0;
    struct sockaddr_in caddr = {0};
    socklen_t asize = 0;
    int len = 0;
    char buf[32] = {0};
    int r = 0;

    server = socket(PF_INET, SOCK_STREAM, 0);

    if (server == -1) {
        printf("server socket error\n");
        return -1;
    }

    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
    saddr.sin_port = htons(8888);

    if (bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
        printf("server bind error\n");
        return -1;
    }

    if (listen(server, 1) == -1) {
        printf("server listen error\n");
        return -1;
    }

    printf("server start success\n");

    while (1) {
        asize = sizeof(caddr);

        client = accept(server, (struct sockaddr*)&caddr, &asize);

        if (client == -1) {
            printf("client accept error");
            return -1;
        }

        printf("client: %d\n", client);

        do {
            r = recv(client, buf, sizeof(buf), MSG_PEEK);

            if (r > 0) {
                buf[r] = 0;
                printf("r = %d\n", r);
                printf("data: %s\n", buf);

                r = recv(client, buf, sizeof(buf), 0);
                buf[r] = 0;
                printf("r = %d\n", r);
                printf("data: %s\n", buf);
            } else {
                printf("no data in receive buf\n");  // 注意这里!(如果未打印表示阻塞)
            }

        }while (r > 0);

        close(client);
    }

    close(server);

    return 0;
}
output:
 server start success
client: 4
r = 11
data: Delpin-Tang
r = 11
data: Delpin-Tang

MSG_DONTWAIT (immediate send and receive mode)

Data is sent and received without blocking, and returns immediately
  • sned() : If the data cannot be sent into the send buffer, then return an error directly (for example: the send buffer size is 1024 bytes, when you want to send 2048 bytes)
  • recv() : If there is no data in the receive buffer, return an error directly
 send() / recv() 返回值:
-1, 错误发生
0,  对端调用 close 关闭
n,   发送 / 接收 的数据量

What does the code below output? Why?

 printf("connect success\n");
sleep(1);
test = "D.T.software";
send(sock, test, strlen(test), 0);
sleep(2);
test = "quit";
send(sock, test, strlen(test), 0);

👇

 do {
    char buf[32] = {0};
    r = recv(client, buf, sizeof(buf), MSG_DONTWAIT);
    printf("r = %d\n", r);
    if (r > 0) {
        buf[r] = 0;
        printf("buf = %s\n", buf);
        if (strcmp(buf, "quit") == 0) {
            break;
        }
    }
    else {
        printf("no data receive\n");
        sleep(1);
    }
}while (1);
server.c
 #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int server = 0;
    struct sockaddr_in saddr = {0};
    int client = 0;
    struct sockaddr_in caddr = {0};
    socklen_t asize = 0;
    int len = 0;
    char buf[32] = {0};
    int r = 0;

    server = socket(PF_INET, SOCK_STREAM, 0);

    if (server == -1) {
        printf("server socket error\n");
        return -1;
    }

    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
    saddr.sin_port = htons(8888);

    if (bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
        printf("server bind error\n");
        return -1;
    }

    if (listen(server, 1) == -1) {
        printf("server listen error\n");
        return -1;
    }

    printf("server start success\n");

    while (1) {
        asize = sizeof(caddr);

        client = accept(server, (struct sockaddr*)&caddr, &asize);

        if (client == -1) {
            printf("client accept error");
            return -1;
        }

        printf("client: %d\n", client);

        do {
            char buf[32] = {0};
            r = recv(client, buf, sizeof(buf), MSG_DONTWAIT);
            printf("r = %d\n", r);
            if (r > 0) {
                buf[r] = 0;
                printf("buf = %s\n", buf);
                if (strcmp(buf, "quit") == 0) {
                    break;
                }
            }
            else {
                printf("no data receive\n");
                sleep(1);
            }
        }while (1);

        close(client);
    }

    close(server);

    return 0;
}
client.c
 #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int sock = {0};
    struct sockaddr_in addr = {0};
    int len = 0;
    char *test;

    sock = socket(PF_INET, SOCK_STREAM, 0);

    if (sock == -1) {
        printf("socket error\n");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(8888);

    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        printf("connect error\n");
        return -1;
    }

    printf("connect success\n");

    sleep(1);

    test = "D.T.software";

    send(sock, test, strlen(test), 0);

    sleep(2);

    test = "quit";

    send(sock, test, strlen(test), 0);

    getchar();

    close(sock);

    return 0;
}
output:
 server start success
client: 4
r = -1
no data receive
r = -1
no data receive
r = 12
buf = D.T.software
r = -1
no data receive
r = -1
no data receive
r = 4
buf = quit

Let's talk about blocking transmission mode again (flags → 0)

send()
  • Send data length > send buffer length 👉 return error
  • Send data length <= remaining length of send buffer 👉 Copy data to send buffer 👉 Return the number of bytes sent
  • Remaining length of send buffer < length of send data <= length of send buffer 👉 Wait for send buffer to be empty 👉 Copy data to send buffer 👉 Return the number of bytes sent
resv()
  • When there is no data in the receive buffer 👉 wait for data
  • The amount of data in the receiving buffer <= the length of the receiving area 👉 All data is copied to the receiving area
  • The amount of data in the receiving buffer > the length of the receiving area 👉 Copy part of the data to the receiving area 👉 Return the number of bytes received

Iterative enhancements to the communication framework

 int TcpClient_Available(TcpClient *client)
{
    static char c_temp[1024 * 2] = {0};
    int ret = -1;
    Client *c = (Client*)client;
    
    if (c) {
        ret = recv(c->fd, c_temp, sizeof(c_temp), MSG_PEEK | MSG_DONTWAIT);
    }
    
    return -1;
}
tcp_client.h
 #ifndef TCP_CLIENT_H
#define TCP_CLIENT_H

#include "message.h"

typedef void TcpClient;

TcpClient *TcpClient_New();
TcpClient *TcpClient_From(int fd);

int TcpClient_SendMsg(TcpClient *client, Message *msg);
int TcpClient_SendRaw(TcpClient *client, char *buf, int length);
Message *TcpClient_RecvMsg(TcpClient *client);
int TcpClient_RecvRaw(TcpClient *client, char *buf, int length);
int TcpClient_Available(TcpClient *client);

int TcpClient_Connect(TcpClient *client, char *ip, int port);
int TcpClient_IsValid(TcpClient *client);
void TcpClient_Close(TcpClient *client);
void TcpClient_Del(TcpClient *client);

void TcpClient_SetData(TcpClient *client, void *data);
void *TcpClient_GetDate(TcpClient *client);

#endif
tcp_client.c
 #include "tcp_client.h"

#include "msg_parser.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h> 
#include <arpa/inet.h>
#include <unistd.h>
#include <malloc.h>

typedef struct tcp_client {
    int fd;
    MParser *parser;
    void *data;
}Client;

TcpClient *TcpClient_New()
{
    return TcpClient_From(-1);
}

TcpClient *TcpClient_From(int fd)
{
    Client *ret = malloc(sizeof(Client));

    if (ret) {
        ret->fd     = fd;
        ret->parser = MParser_New();
        ret->data   = NULL;
    }

    return (ret && ret->parser) ? ret : (free(ret), NULL);
}

int TcpClient_SendMsg(TcpClient *client, Message *msg)
{
    int ret = 0;
    Client *c = (Client*)client;

    if (c && msg) {
        int len = Message_Size(msg);
        char *data = (char*)Message_H2N(msg);

        ret = (send(c->fd, data, len, 0) != -1);

        Message_N2H(msg);
    }

    return ret;
}

int TcpClient_SendRaw(TcpClient *client, char *buf, int length)
{
    int ret = 0;
    Client *c = (Client*)client;

    if (c && buf)  {
        ret = send(c->fd, buf, length, 0);
    }

    return ret;
}

Message *TcpClient_RecvMsg(TcpClient *client)
{
    Message *ret = NULL;
    Client *c = (Client*)client;

    if (c) {
        ret = MParser_ReadFd(c->parser, c->fd);
    }

    return ret;
}

int TcpClient_RecvRaw(TcpClient *client, char *buf, int length)
{
    int ret = 0;
    Client *c = (Client*)client;

    if (c && buf) {
        ret = recv(c->fd, buf, length, 0);
    }

    return ret;
}

int TcpClient_Connect(TcpClient *client, char *ip, int port)
{
    int ret = TcpClient_IsValid(client);
    Client *c = (Client*)client;

    if (!ret && ip && c && ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) != -1)) {
        struct sockaddr_in addr = {0};

        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(ip);
        addr.sin_port = htons(port);

        ret = (connect(c->fd, (struct sockaddr*)&addr, sizeof(addr)) != -1);
    }

    return ret;
}

int TcpClient_IsValid(TcpClient *client)
{       
    int ret = 0;
    Client *c = (Client*)client;

    if (c) {
        struct tcp_info info = {0};
        int l = sizeof(info);

        getsockopt(c->fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&l);

        ret = (info.tcpi_state == TCP_ESTABLISHED);
    }

    return ret;
}

void TcpClient_Close(TcpClient *client)
{
    Client *c = (Client*)client;

    if (c) {
        close(c->fd);

        c->fd = -1;

        MParser_Reset(c->parser);
    }
}

void TcpClient_Del(TcpClient *client)
{
    Client *c = (Client*)client;

    if (c) {
        TcpClient_Close(c);
        MParser_Del(c->parser);
        free(c);
    }   
}

void TcpClient_SetData(TcpClient *client, void *data)
{
    Client *c = (Client*)client;

    if (c) {
        c->data = data;
    }
}

void *TcpClient_GetDate(TcpClient *client)
{
    void *ret = NULL;
    Client *c = (Client*)client;

    if (c) {
        ret = c->data;
    }

    return ret;
}

int TcpClient_Available(TcpClient *client)
{
    static char c_temp[1024 * 2] = {0};
    int ret = -1;
    Client *c = (Client*)client;
    
    if (c) {
        ret = recv(c->fd, c_temp, sizeof(c_temp), MSG_PEEK | MSG_DONTWAIT);
    }
    
    return ret;
}

MSG_WAITALL (waiting for data)

  • It is dedicated to receiving, and recv() returns only when the required data is fully satisfied.

MSG_MORE (more data)

  • Send dedicated, instructs the kernel to not be in a hurry to transfer the data in the send buffer

What does the code below output? Why?

client.c

 #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int sock = {0};
    struct sockaddr_in addr = {0};
    int len = 0;
    char *test;
    int i = 0;

    sock = socket(PF_INET, SOCK_STREAM, 0);

    if (sock == -1) {
        printf("socket error\n");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(8888);

    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        printf("connect error\n");
        return -1;
    }

    printf("connect success\n");

    /***************************************/
    test = "Delphi-Tang";
    for (i=0; i<strlen(test); ++i) {
        send(sock, test+i, 1, 0);
        if (i % 2) {
            sleep(1);
        }
    }

    test = "quit";
    for (i=0; i<(strlen(test)-1); ++i) {
        send(sock, test + i, 1, MSG_MORE);
        sleep(1);
    }

    send(sock, test + i, 1, 0);
    /***************************************/
    
    getchar();

    close(sock);

    return 0;
}
server.c
 #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int server = 0;
    struct sockaddr_in saddr = {0};
    int client = 0;
    struct sockaddr_in caddr = {0};
    socklen_t asize = 0;
    char buf[32] = {0};
    int r = 0;
    

    server = socket(PF_INET, SOCK_STREAM, 0);

    if (server == -1) {
        printf("server socket error\n");
        return -1;
    }

    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
    saddr.sin_port = htons(8888);

    if (bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
        printf("server bind error\n");
        return -1;
    }

    if (listen(server, 1) == -1) {
        printf("server listen error\n");
        return -1;
    }

    printf("server start success\n");

    while (1) {
        asize = sizeof(caddr);

        client = accept(server, (struct sockaddr*)&caddr, &asize);

        if (client == -1) {
            printf("client accept error");
            return -1;
        }

        printf("client: %d\n", client);

        do {
            /***************************************/
            int len[2] = {11, 4};
            int i = 0;

            for (i=0; i<2; ++i) {
                r = recv(client, buf, len[i], MSG_WAITALL);
                printf("r = %d\n", r);
                if (r > 0) {
                    buf[r] = 0;
                    printf("data = %s\n", buf);

                    if (strcmp(buf, "quit") == 0) {
                        break;
                    }
                }
            }
            /***************************************/
        }while (0);

        close(client);
    }

    close(server);

    return 0;
}
output:
 client: 4
r = 11
data = Delphi-Tang
r = 4
data = quit

Thinking: How to use UDP to send and receive data?

TianSong
737 声望139 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧