当前位置:网站首页>MSG_ OOB MSG_ PEEK
MSG_ OOB MSG_ PEEK
2022-06-25 06:38:00 【Python's path to becoming a God】
This article introduces you to an example to illustrate MSG_OOB MSG_PEEK MSG_DONTWAIT, It mainly includes an example to illustrate MSG_OOB MSG_PEEK MSG_DONTWAIT Using examples 、 Application skills 、 Summary of basic knowledge points and precautions , It has certain reference value , If you need a friend, please refer to it .
This kind of data is transmitted through the signal SIGURG To inform , So we need to use signal / sigaction To register a signal processing function , stay signal First, you need to fcntl(clientfd,F_SETOWN,getpid()) , Since this signal is generated by the socket , The owner of the socket is the operating system , Only set the owner of the socket to the current process ,SIGURG Will be passed to this process , Otherwise, you will not receive SIGURG The signal , Only ordinary data can be accepted .
MSG_PEEK : Peek for data in the buffer , recv(clientfd,buf,BUFSIZ,MSG_PEEK) , Data is not removed from the buffer .
MSG_DONTWAIT : Don't block
MSG_OOB : Just by tcp The head of the urgent Mode transmitted , And only one byte will be read as oob data . for example :send(sockfd,"1234",strlen("1234"),MSG_OOB); Only the last byte :4 Will be treated as oob data , Other data "123" Will be read as normal data .
receive oob data :
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include <sys/types.h> /* See NOTES */
6 #include <sys/socket.h>
7
8 #include <netinet/in.h>
9 #include <netinet/tcp.h>
10 #include <netdb.h>
11
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 #include <errno.h>
16 #include <signal.h>
17
18 #define PORT 10000
19
20 static void urg_handler(int sig);
21 int clientfd = 0;
22
23 int main(int argc, char ** argv)
24 {
25 int listenfd = socket(AF_INET, SOCK_STREAM, 0);
26
27 struct sockaddr_in serv_addr, client_addr;
28 memset(&serv_addr,0,sizeof(serv_addr));
29 memset(&client_addr,0,sizeof(client_addr));
30 serv_addr.sin_family = AF_INET;
31 serv_addr.sin_port = htons(PORT);
32 serv_addr.sin_addr.s_addr = INADDR_ANY;
33 if(bind(listenfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
34 perror("bind");
35 return 0;
36 }
37
38 if(listen(listenfd, 10) < 0) {
39 perror("listen");
40 return 0;
41 }
42
43 socklen_t client_socklen = sizeof(client_addr);
44 clientfd = accept(listenfd, (struct sockaddr *)&client_addr, &client_socklen);
45
46 // Be careful , If the socket owner is not set , This process will not receive SIGURG The signal .
47 fcntl(clientfd, F_SETOWN, getpid());
48 // Register signal functions
49 signal(SIGURG, urg_handler);
50
51 int n = 0;
52 char buf[BUFSIZ];
53
65
66
67 // Accept common data .
68 while((n = recv(clientfd, buf, BUFSIZ, 0)) != 0){
69 if( -1 == n){
70 perror("recv error");
71 printf("error no : %d\n" ,errno);
72 continue;
73 }
74 buf[n] = 0;
75 printf("normal buf:%s\n", buf);
76 }
77 close(clientfd);
78 close(listenfd);
79 return 0;
80 }
81
82 static void urg_handler(int sig)
83 {
84 char buf[30];
85
86 // Accept OOB data , Only the last byte can be accepted , Other bytes are accepted as normal data
87 int n = recv(clientfd ,buf, 30, MSG_OOB);
88 printf("\noob len : %d, " , n);
89 buf[n] = 0;
90 printf("oob data:%s\n\n" , buf);
91 }
send out oob data :
test result :
[email protected]:~/a_test/a02_MSG_OOB$ ./oob_send 127.0.0.1 10000
data1: data1
oobdata1: oobdata1
data2: data2
oob_data2: oob_data2
Above : A total of 4 Time data : Normal transmission data1,oob send out oobdata1, Normal transmission data2,oob send out oob_data2
[email protected]:~/a_test/a02_MSG_OOB$ ./oob_recv
normal buf:data1
oob len : 1, oob data:1
normal buf:oobdata
normal buf:data2
oob len : 1, oob data:2
normal buf:oob_data
Above : Normal reception data1 and data2 The data of ; but oob Data sent , branch 2 Partial reception : Part of it is oob The last time you receive and send data 1 Bytes of data , Above :oob data:1 and 2, The rest of the data , Are received normally .
边栏推荐
- [short time average zero crossing rate] short time average zero crossing rate of speech signal based on MATLAB [including Matlab source code 1721]
- ACWING/2004. 错字
- Cs4344/ht5010 stereo d/a digital to analog converter
- Report on strategic suggestions on investment direction and Prospect of global and Chinese marine biological industry (2022 Edition)
- Cs8126t 3.1w mono ultra low EMI unfiltered class D audio power amplifier IC
- Face++ realizes face detection by flow
- Derivation of COS (a-b) =cosa*cosb+sina*sinb
- Laravel8+ wechat applet generates QR code
- JD 8 fleet stores search history, deletes history, clears history (not finished)
- What is cloud primordial?
猜你喜欢

The five minute demonstration "teaches" actors to speak foreign languages and can seamlessly switch languages. This AI dubbing company has just received a round a financing of 20million US dollars

Ht513 I2S input 2.8W mono class D audio power amplifier IC

JS to determine whether an element exists in the array (four methods)

How to deploy locally developed SAP ui5 applications to ABAP servers

DNS domain name system

Cve-2022-23131 - bypass SAML SSO authentication

In depth inventory: 23 vscode plug-in artifacts that improve development efficiency and aesthetics

有能力的人从不抱怨大环境!

Ht8513 single lithium battery power supply with built-in Dynamic Synchronous Boost 5W mono audio power amplifier IC solution

TCP BBR as rate based
随机推荐
Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
The "&" character will destroy the data stored in the web The "&" character breaks passwords that are stored in the web config
Unity获取资源路径
fastadmin 联级清空数据
What is VLAN
How to realize the stable output of 3.3v/3.6v (1.2-5v) voltage of lithium battery by using the voltage rise and fall chip cs5517
keil debug查看变量提示not in scope
[200 opencv routines of youcans] 104 Motion blur degradation model
Is the number of indexes in a table the more the better?
From file system to distributed file system
Research Report on brand strategic management and marketing trends in the global and Chinese preserved fruit market 2022
What is cloud primordial?
Are these old system codes written by pigs?
Why study discrete mathematics
Baidu map - introductory tutorial
Period to string [repeat] - period to string [duplicate]
Understand what MTU is
We cannot activate inspection type for article master in transaction code MM41?
Understand what MSS is
Is it safe to open a stock account on the Internet in Beijing?