pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C pastebin - collaborative debugging tool View Help


Posted by cmacgreg on Wed 12 Dec 17:30 (modification of post by cmacgreg view diff)
report abuse | download | new post

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netinet/tcp.h>
  9. #include <poll.h>
  10.  
  11. int main(){
  12.                
  13.     int it;
  14.         int fd_s, fd_c;
  15.         struct sockaddr_in sa_i;
  16.         struct pollfd pfd, *tpfd=&pfd;
  17.  
  18.         bzero(&sa_i,sizeof(struct sockaddr_in));
  19.    
  20.     sa_i.sin_family = AF_INET;
  21.     sa_i.sin_port = htons(9000);
  22.  
  23.         if(-1 == (it = fork()))
  24.                 perror("fork"),exit(1);
  25.        
  26.         if(it){
  27.                 sa_i.sin_addr.s_addr = htonl(INADDR_ANY);
  28.        
  29.                 if(-1 == (fd_s = socket(PF_INET, SOCK_STREAM, 0)))
  30.                 perror("socket"),exit(1);
  31.         if(-1 == bind(fd_s, (const struct sockaddr*) &sa_i, sizeof(struct sockaddr_in)))
  32.                 perror("bind"),exit(1);
  33.         if(-1 == listen(fd_s, 1))
  34.                 perror("listen"),exit(1);
  35.                 if(-1 == (fd_c = accept(fd_s,NULL,NULL)))
  36.                 perror("accept"),exit(1);
  37.                
  38.                 tpfd->fd = fd_c;
  39.                 tpfd->events = POLLIN | POLLOUT;
  40.                
  41.                 sleep(1);
  42.  
  43.                 if(-1 == (it = poll(tpfd, 1, 0)))
  44.                         perror("poll"),exit(1);
  45.  
  46.                 printf("poll return: %d\nfd(%d):\t%s%s%s%s%s%s\n", it, fd_c,
  47.                                 tpfd->revents & POLLIN ? " POLLIN" : "", tpfd->revents & POLLPRI ? " POLLPRI" : "",
  48.                                 tpfd->revents & POLLOUT ? " POLLOUT" : "", tpfd->revents & POLLERR ? " POLLERR" : "",
  49.                                 tpfd->revents & POLLHUP ? " POLLHUP" : "", tpfd->revents & POLLNVAL ? " POLLNVAL" : "");
  50.                
  51.                 if(it == 2)
  52.                         puts("BEEP! poll() may actually be returning from select()");
  53.                 else
  54.                         puts(":) your system's poll() appears to be working properly");
  55.  
  56.         }
  57.         else{
  58.  
  59.                 sa_i.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  60.    
  61.                 if(-1 == (fd_c = socket(PF_INET, SOCK_STREAM, 0)))
  62.                         perror("socket"),exit(1);
  63.                 if(-1 == connect(fd_c, (const struct sockaddr*) &sa_i, sizeof(struct sockaddr_in)))
  64.                         perror("connect"),exit(1);
  65.         }
  66.        
  67.         return 0;
  68.        
  69. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post