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

 Insert picture description here

原网站

版权声明
本文为[weixin_ fifty-one million one hundred and eighty-seven thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071136083456.html