MSG_PEEK (資料窺探)
使用 MSG_PEEK 選項能夠獲取接收緩衝區資料的拷貝
- recv() 專用選項,可用於資料預接收
- 指定 MSG_PEEK 選項時,
不會清空緩衝區
- 可用於獲取接收緩衝區種的資料量(位元組數)
當接收緩衝區中沒有資料時,MSG_PEEK 也會導致執行緒阻塞
下面的程式碼輸出什麼?為什麼?
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;
}
輸出:
server start success
client: 4
r = 11
data: Delpin-Tang
r = 11
data: Delpin-Tang
MSG_DONTWAIT (立即收發模式)
資料收發時不阻塞,立即返回
- sned() : 如果無法將資料送入傳送緩衝區,那麼直接返回錯誤 (比如:傳送緩衝器 1024 位元組大小,欲想傳送 2048 位元組時)
- recv() : 如果接收緩衝區中沒有資料,那麼直接返回錯誤
send() / recv() 返回值:
-1, 錯誤發生
0, 對端呼叫 close 關閉
n, 傳送 / 接收 的資料量
下面的程式碼輸出什麼?為什麼?
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;
}
輸出:
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
再論阻塞傳送模式 (flags → 0)
send()
- 傳送資料長度 > 傳送緩衝區長度 ? 返回錯誤
- 傳送資料長度 <= 傳送緩衝區剩餘長度 ? 複製資料到傳送緩衝區 ? 返回傳送的位元組數
- 傳送緩衝區剩餘長度 < 傳送資料長度 <= 傳送緩衝區長度 ? 等待傳送緩衝器清空 ? 複製資料到傳送緩衝區 ? 返回傳送的位元組數
resv()
- 接收緩衝區中沒有資料時 ? 等待資料
- 接收緩衝區資料量 <= 接收區長度 ? 資料全部拷貝到接收區
- 接收緩衝區資料量 > 接收區長度 ? 拷貝部分資料到接收區 ? 返回接收的位元組數
通訊框架的迭代增強
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(等待資料)
- 接收專用,等待需要的資料完全滿足時,recv() 才返回
MSG_MORE(更多資料)
- 傳送專用,指示核心不著急將傳送緩衝區中的資料進行傳輸
下面的程式碼輸出什麼?為什麼?
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;
}
輸出:
client: 4
r = 11
data = Delphi-Tang
r = 4
data = quit
思考:如何使用 UDP 進行資料收發?