当前位置:网站首页>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;
}

边栏推荐
- 2022-7-7 Leetcode 844.比较含退格的字符串
- cmake 学习使用笔记(一)
- ESP32系列专栏
- [learning notes] zkw segment tree
- 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] Basic knowledge of ros2 - summary version of operation commands
- 为租客提供帮助
- Toraw and markraw
- RealBasicVSR测试图片、视频
- 一文读懂数仓中的pg_stat
猜你喜欢

Redis只能做缓存?太out了!

Esp32 construction engineering add components

Show the mathematical formula in El table

Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file

Vscode编辑器ESP32头文件波浪线不跳转彻底解决

Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)

Help tenants

室内ROS机器人导航调试记录(膨胀半径的选取经验)
![[dark horse morning post] Huawei refutes rumors about](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[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

【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
随机推荐
Leecode3. Longest substring without repeated characters
Scripy tutorial classic practice [New Concept English]
靠卖概念上市,认养一头牛能走多远?
Milkdown control icon
OSI 七层模型
How did Guotai Junan Securities open an account? Is it safe to open an account?
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
一文读懂数仓中的pg_stat
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
118. Yanghui triangle
MongoDB 分片总结
Ways to improve the performance of raspberry pie
DID登陆-MetaMask
postgresql array类型,每一项拼接
Getting started with MySQL
Write it down once Net a new energy system thread surge analysis
Mongodb meets spark (for integration)
单片机学习笔记之点亮led 灯
Flink | multi stream conversion
PHP - laravel cache