当前位置:网站首页>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 .
边栏推荐
- China rehabilitation hospital industry operation benefit analysis and operation situation investigation report 2022
- Report on the application prospect and investment potential of global and Chinese cell therapy industry 2022-2028
- How to realize hierarchical management of application and hardware in embedded projects
- Investment opportunities and operational risk assessment report of China's engineering consulting industry during the 14th Five Year Plan period 2022-2028
- sin(a+b)=sina*cosb+sinb*cosa的推导过程
- Zero foundation wants to learn web security, how to get started?
- Global and Chinese kaolin market operation scale and investment development proposal report 2022
- Grouped uitableview has 20px of extra padding at the bottom
- Face++ realizes face detection by flow
- Can TCP syn handshake messages transmit data
猜你喜欢

What is the slice flag bit

Sleep quality today 67 points

Acwing2013. three lines

keil debug查看变量提示not in scope

Self adjustment process of MySQL index tree when adding data

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

Cs4344/ht5010 stereo d/a digital to analog converter

CTFSHOW

Understand what ICMP Protocol is

Laravel8+ wechat applet generates QR code
随机推荐
Zero foundation wants to learn web security, how to get started?
Derivation of sin (a-b) =sina*cosb-sinb*cosa
Cs8126t 3.1w mono ultra low EMI unfiltered class D audio power amplifier IC
mysql 表查询json数据
Ht7180 3.7V L 12v/2a built in MOS high current boost IC solution
【ROS2】为什么要使用ROS2?《ROS2系统特性介绍》
Which of the top ten securities companies has the lowest Commission and is the most safe and reliable? Do you know anything
Methods for obtaining some information of equipment
[从零开始学习FPGA编程-43]:视野篇 - 后摩尔时代”芯片设计的技术演进-2-演进方向
System dilemma and software complexity: Why are our systems so complex?
Ht513 I2S input 2.8W mono class D audio power amplifier IC
How do I check swift if two arrays contain the same elements, regardless of the order in which they appear?
Period to string [repeat] - period to string [duplicate]
Sword finger offer II 095 Longest common subsequence
Arm instructions and others
Advantages and disadvantages of using SNMP and WMI polling
Single lithium battery 3.7V power supply 2x12w stereo boost audio power amplifier IC combination solution
@Detailed explanation of valid annotation usage
How to chain multiple different InputStreams into one InputStream
PHP converts strings to hex and hex to string - PHP convert string to hex and hex to string