标签: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;
}
标签:nis udp names static types int return recvfrom res
原文地址:http://www.cnblogs.com/hugb/p/7425442.html