当前位置:网站首页>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;
}
边栏推荐
- Introduce six open source protocols in detail (instructions for programmers)
- 干货|总结那些漏洞工具的联动使用
- Why can basic data types call methods in JS
- How to make join run faster?
- Flink | 多流转换
- 一文读懂数仓中的pg_stat
- Esp32 ① compilation environment
- 简单好用的代码规范
- 2022-7-7 Leetcode 844.比较含退格的字符串
- Write it down once Net a new energy system thread surge analysis
猜你喜欢
Ways to improve the performance of raspberry pie
Talk about pseudo sharing
Vscade editor esp32 header file wavy line does not jump completely solved
Help tenants
PAcP learning note 1: programming with pcap
作战图鉴:12大场景详述容器安全建设要求
Deep understanding of array related problems in C language
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
118. Yanghui triangle
随机推荐
Split screen bug notes
Use of polarscatter function in MATLAB
高端了8年,雅迪如今怎么样?
Distributed transaction solution
供应链供需预估-[时间序列]
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
Final review notes of single chip microcomputer principle
xshell连接服务器把密钥登陆改为密码登陆
MySQL error 28 and solution
一文读懂数仓中的pg_stat
简单好用的代码规范
QQ medicine, Tencent ticket
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
[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
118. 杨辉三角
华为镜像地址
QQ的药,腾讯的票
[daily training -- Tencent select 50] 231 Power of 2
High end for 8 years, how is Yadi now?
Introduce six open source protocols in detail (instructions for programmers)