cmacgreg
By: a guest | Dec 12th, 2007 | Syntax:
C | Size: 1.81 KB | Hits: 47 | Expires: Never
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
int main(){
int it;
int fd_s, fd_c;
struct sockaddr_in sa_i;
struct pollfd pfd, *tpfd=&pfd;
bzero(&sa_i,sizeof(struct sockaddr_in));
sa_i.sin_family = AF_INET;
sa_i.sin_port = htons(9000);
if(-1 == (it = fork()))
perror("fork"),exit(1);
if(it){
sa_i.sin_addr.s_addr = htonl(INADDR_ANY);
if(-1 == (fd_s = socket(PF_INET, SOCK_STREAM, 0)))
perror("socket"),exit(1);
if(-1 == bind(fd_s, (const struct sockaddr*) &sa_i, sizeof(struct sockaddr_in)))
perror("bind"),exit(1);
if(-1 == listen(fd_s, 1))
perror("listen"),exit(1);
if(-1 == (fd_c = accept(fd_s,NULL,NULL)))
perror("accept"),exit(1);
tpfd->fd = fd_c;
tpfd->events = POLLIN | POLLOUT;
sleep(1);
if(-1 == (it = poll(tpfd, 1, 0)))
perror("poll"),exit(1);
printf("poll return: %d\nfd(%d):\t%s%s%s%s%s%s\n", it
, fd_c
,
tpfd->revents & POLLIN ? " POLLIN" : "", tpfd->revents & POLLPRI ? " POLLPRI" : "",
tpfd->revents & POLLOUT ? " POLLOUT" : "", tpfd->revents & POLLERR ? " POLLERR" : "",
tpfd->revents & POLLHUP ? " POLLHUP" : "", tpfd->revents & POLLNVAL ? " POLLNVAL" : "");
if(it == 2)
puts("BEEP! poll() may actually be returning from select()");
else
puts(":) your system's poll() appears to be working properly");
}
else{
sa_i.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if(-1 == (fd_c = socket(PF_INET, SOCK_STREAM, 0)))
perror("socket"),exit(1);
if(-1 == connect(fd_c, (const struct sockaddr*) &sa_i, sizeof(struct sockaddr_in)))
perror("connect"),exit(1);
}
return 0;
}