当前位置:网站首页>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 .
边栏推荐
- Observation configuring wmic
- Cve-2022-23131 - bypass SAML SSO authentication
- Ht81293 built in adaptive dynamic boost 20W mono class D power amplifier IC solution
- Single lithium battery 3.7V power supply 2x12w stereo boost audio power amplifier IC combination solution
- Preliminary practice of niuke.com (summary)
- STL map的用法
- JS dynamic table creation
- Kotlin reflection -- Notes
- JD 8 fleet stores search history, deletes history, clears history (not finished)
- Is the number of indexes in a table the more the better?
猜你喜欢

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

2022-02-19: fence installation. In a two-dimensional garden, there are some trees represented by (x, y) coordinates. As the installation cost is very expensive, your task is to enclose all the trees w

Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things

Acwing2013. three lines
![[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]](/img/a1/0cb61368cb1d0817d74781084a4466.jpg)
[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]

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

Cs4344/ht5010 stereo d/a digital to analog converter

System dilemma and software complexity: Why are our systems so complex?

DataX tutorial (10) - hot plug principle of dataX plug-in

ACWING/2004. Misspelling
随机推荐
BGP - basic concept
Uncaught typeerror cannot set properties of undefined (setting 'classname') reported by binding onclick event in jsfor loop
Usage of STL map
有能力的人从不抱怨大环境!
Why study discrete mathematics
Global and Chinese kaolin market operation scale and investment development proposal report 2022
What does cardinality mean in set
Missing libgmp-10 dll - libgmp-10. dll is missing
Zhinai's database
Global and Chinese gallium nitride (GAN) market output value scale forecast and application prospect analysis report 2022
Wechat applet authorization login + mobile phone sending verification code +jwt verification interface (laravel8+php)
STL map的用法
Can TCP syn handshake messages transmit data
Ht8513 single lithium battery power supply with built-in Dynamic Synchronous Boost 5W mono audio power amplifier IC solution
What is VLAN
这些老系统代码,是猪写的么?
Comparison test of mono 120W high power class D power amplifier chip cs8683-tpa3116
JS to determine whether an element exists in the array (four methods)
Research Report on global and Chinese vaccine market profit forecast and the 14th five year plan development strategy 2022-2028
Face++ realizes face detection by flow