码迷,mamicode.com
首页 > 其他好文 > 详细

测试sigaction重启动标识

时间:2017-08-24 22:36:31      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:nis   udp   names   static   types   int   return   recvfrom   res   

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <iostream>

using namespace std;

bool flag = true;

static void sig_func(int signo){

  flag = false;
  cout<<"signal function called";
}


int main(){

 

  struct sigaction newact, oldact;
  newact.sa_handler = sig_func;
  sigemptyset(&newact.sa_mask);
  newact.sa_flags = 0;
  newact.sa_flags |= SA_RESTART;

  sigaction(SIGINT, &newact, &oldact);


//  signal(SIGINT, sig_func);


  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  int broad = 1;
  setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broad, sizeof(broad) );

  struct sockaddr_in addr;

  struct sockaddr_in addr2;
  socklen_t len = sizeof(addr2);

  memset(&addr, 0x00, sizeof(addr) );
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(3000);

  bind(sock, (struct sockaddr*)&addr, sizeof(addr) );
 
  while( true && flag){

    char msgbuf[1024];
    int nbytes = recvfrom(sock, msgbuf, sizeof(msgbuf), 0, (struct sockaddr*)&addr2, &len);

    if (nbytes < 0 && !flag){
      cerr<<"interrupted by ctrl c"<<endl;

    }

 


  }

 


  return 0;

}

测试sigaction重启动标识

标签:nis   udp   names   static   types   int   return   recvfrom   res   

原文地址:http://www.cnblogs.com/hugb/p/7425442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!