当前位置:网站首页>2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
2022-07-07 13:39:00 【weixin_ fifty-one million one hundred and eighty-seven thousand】
End sending data
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
int main(void){
struct sockaddr_in server_address;
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr.s_addr);
server_address.sin_port = htons(9999);
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
assert(sockfd >= 0);
if(connect(sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0){
perror("connect");
printf("connection failed\n");
}else {
const char*oob_data = "abc";
const char*normal_data = "123";
send(sockfd, normal_data, strlen(normal_data), 0);
send(sockfd, oob_data, strlen(oob_data), MSG_OOB);
send(sockfd, normal_data, strlen(normal_data), 0);
}
close(sockfd);
return 0;
}
Accept data terminal
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#include<signal.h>
#include<fcntl.h>
#define BUF_SIZE 1024
static int connfd;
void sig_urg(int sig){
/* If the system call or library function is executed correctly , *errno The value of is not cleared ( Set up 0, Note that this will not be cleared , * It's not that it won't be changed ) Of , If you execute a function A There was a mistake errno Be changed , * Next, execute the function directly B, If the function B If executed correctly , *errno Also keep functions A The value set when an error occurs . */
// Keep the original errno It can ensure the reentrancy of the function
/* “ Reentrant (reentrant) Functions can be used concurrently by more than one task , You don't have to worry about data errors . Reentrant functions can be interrupted at any time , We'll keep running later , No loss of data ; Reentrant functions either use local variables , Or protect your data when using global variables .” */
int save_errno = errno;
char buffer[BUF_SIZE];
memset(buffer, '\0', BUF_SIZE);
// Receive out of band data
int ret = recv(connfd, buffer, BUF_SIZE, MSG_OOB);
printf("got %d bytes of oob data '%s'\n", ret, buffer);
errno = save_errno;
}
void addsig(int sig, void(*sig_handler)(int)){
//
struct sigaction sa;
memset(&sa, '\0', sizeof(sa));
// Signal processing functions define signal processing functions
sa.sa_handler = sig_handler;
// Set the behavior of receiving the signal
sa.sa_flags |= SA_RESTART;
// Automatically restart the system call interrupted by the signal processor program
//sigfillset Used to initialize a signal set
// utilize sa_mask Field can specify a set of signals , They are not allowed to interrupt the execution of this processor program
sigfillset(&sa.sa_mask);
// Interrupts to the same signal will not be called recursively
// You may need to repeat the signal explanation of Niuke
assert(sigaction(sig, &sa, NULL) != -1);
}
int main(void){
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
//inet_pton(AF_INET, "127.0.0.1", &address.sin_addr.s_addr);
address.sin_port = htons(9999);
int sock = socket(PF_INET, SOCK_STREAM, 0);
assert(sock >= 0);
int ret = bind(sock, (struct sockaddr*)& address, sizeof(address));
assert(ret != -1);
ret = listen(sock, 5);
assert(ret != -1);
struct sockaddr_in client;
socklen_t client_addrlength = sizeof(client);
connfd = accept(sock, (struct sockaddr*)& client, &client_addrlength);
if (connfd < 0){
perror("accept");
}else{
// Add to the signal processing set
// The signal processing function sig_urg It is used to print out take out data
addsig(SIGURG, sig_urg);
fcntl(connfd, F_SETOWN, getpid());
char buffer[BUF_SIZE];
// Receive common data
while (1){
memset(buffer, '\0', BUF_SIZE);
ret = recv(connfd, buffer, BUF_SIZE-1, 0);
if (ret <= 0){
break;
}
printf("got %d bytes of normal data '%s'\n", ret, buffer);
}
close(connfd);
}
close(sock);
return 0;
}
边栏推荐
- Data refresh of recyclerview
- 《厌女:日本的女性嫌恶》摘录
- Realize the IP address home display function and number home query
- 记一次 .NET 某新能源系统 线程疯涨 分析
- Why can basic data types call methods in JS
- 一文读懂数仓中的pg_stat
- [dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
- PAcP learning note 3: pcap method description
- [learning notes] segment tree selection
- Scripy tutorial classic practice [New Concept English]
猜你喜欢
Cinnamon taskbar speed
Navicat运行sql文件导入数据不全或导入失败
LeetCode简单题分享(20)
ESP32 ① 编译环境
Digital IC Design SPI
[Presto profile series] timeline use
Vscade editor esp32 header file wavy line does not jump completely solved
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
Custom thread pool rejection policy
随机推荐
DID登陆-MetaMask
数字ic设计——SPI
Show the mathematical formula in El table
User management summary of mongodb
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
118. Yanghui triangle
OSI 七层模型
Esp32 ① compilation environment
如何让join跑得更快?
Read PG in data warehouse in one article_ stat
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
Mongodb meets spark (for integration)
MongoDB优化的几点原则
一文读懂数仓中的pg_stat
一文读懂数仓中的pg_stat
得物客服热线的演进之路
1、深拷贝 2、call apply bind 3、for of for in 区别
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
[1] ROS2基础知识-操作命令总结版
1. Deep copy 2. Call apply bind 3. For of in differences