home *** CD-ROM | disk | FTP | other *** search
/ Phoenix Rising BBS / phoenixrising.zip / phoenixrising / unix / sock.arj / SOCK.C next >
C/C++ Source or Header  |  1993-05-13  |  1KB  |  60 lines

  1.  
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6.  
  7. #ifndef PORT
  8. #define PORT 3012
  9. #endif  PORT
  10.  
  11. get_connect()
  12. {
  13.   int s,s2,len;
  14.   struct sockaddr_in addr,remote;
  15.  
  16.   s=socket(AF_INET,SOCK_STREAM,0);
  17.   if(s<0) {
  18.     perror("socket");
  19.     return(-1);
  20.   }
  21.   addr.sin_addr.s_addr   = INADDR_ANY;
  22.   addr.sin_family = AF_INET;
  23.   addr.sin_port   = htons(PORT); 
  24.   bzero(&remote,sizeof(remote));
  25.   if(bind(s,&addr,sizeof(addr))<0) {
  26.     perror("bind");
  27.     return(-1);
  28.   }
  29.   if(listen(s,1)<0) {
  30.     perror("listen");
  31.     return(-1);
  32.   }
  33.   len = sizeof(remote);
  34.   s2=accept(s,&remote,&len);
  35.   close(s);
  36.   if(s2>=0) 
  37.     fprintf(stderr,"Connect from %s port %d\n",
  38.             inet_ntoa(remote.sin_addr),remote.sin_port);
  39.   else perror("accept");
  40.   return(s2);
  41. }
  42.  
  43.  
  44. main()
  45. {
  46.   char buf[2];
  47.   int s;
  48.  
  49.   s=get_connect();
  50.   if(s<0) {
  51.     fprintf(stderr,"Couldnt get a connection\n");
  52.     exit(-1);
  53.   }
  54.   while(read(s,buf,1)>0) {
  55.     if(*buf=='\0') return;
  56.     write(1,buf,1);
  57.   }
  58. }
  59.     
  60.