Socket - connect problem: The descriptor is a file, not a socket

Peter P. Mooring peterpm@xs4all.nl
Sun Jun 4 03:58:00 GMT 2000


Hi all,
For a project I need a small program using a 
socket to communicate with a server so I downloaded and installed cygwin/gcc, 
got an example program from the net and compiled (and updated in.h and compiled 
again). No errors.
When I run the program (on Win98), see below, I 
get the message 'The descriptor is a file, not a socket'. I scanned the mailing 
lists , and the net, but am stuck. 
Please help, t hanks,
Peter
 
This is the program, I call it like this 'client 
1685 164.139.146.156'
 
#include <sys/types.h> #include 
<sys/socket.h> #include <sys/time.h> #include 
<netinet/in.h> #include <errno.h> #include 
<ctype.h> #include <netdb.h> #include 
<stdio.h> #include <string.h>
 
main(argc, argv)  int 
argc;  char *argv[];  {    struct hostent 
*hostp;  struct servent *servp;  struct sockaddr_in 
server;  int sock;  static struct timeval timeout = { 5, 0 
};   /* five seconds */  fd_set rmask, xmask, 
mask;  char buf[BUFSIZ];         
int nfound, bytesread;
 
 if (argc != 3) {   (void) 
fprintf(stderr,"usage: %s service host\n",argv[0]);   
exit(1);  }  if ((sock = socket(AF_INET, SOCK_STREAM, 
IPPROTO_TCP)) < 0) {   perror("socket");   
exit(1);  }         if 
(isdigit(argv[1][0])) {   static struct servent s;   servp = 
&s;   s.s_port = htons((u_short)atoi(argv[1]));  } else if 
((servp = getservbyname(argv[1], "tcp")) == 0) {   
fprintf(stderr,"%s: unknown service\n",argv[1]);   
exit(1);  }         if ((hostp = 
gethostbyname(argv[2])) == 0) {   fprintf(stderr,"%s: unknown 
host\n",argv[2]);   exit(1);  }  memset((void *) 
&server, 0, sizeof server);  server.sin_family = AF_INET; 
 memcpy((void *) &server.sin_addr, hostp->h_addr, 
hostp->h_length);  server.sin_port = servp->s_port;  if 
(connect(sock, (struct sockaddr *) &server, sizeof server) < 0) { 
  (void) close(sock);   perror("connect");  
  exit(1);  }  FD_ZERO(&mask);  
 FD_SET(sock, &mask);  FD_SET(fileno(stdin), &mask); 
 for (;;) {   rmask = mask;   nfound = 
select(FD_SETSIZE, &rmask, (fd_set *)0, (fd_set *)0, &timeout); 
  if (nfound < 0) {    if (errno == EINTR) { 
    printf("interrupted system call\n"); 
    continue;    }    /* something 
is very wrong! */    perror("select");    
exit(1);   }   if (nfound == 0) {    /* timer 
expired */    printf("Please type something!\n"); 
   continue;   }   if (FD_ISSET(fileno(stdin), 
&rmask)) {    /* data from keyboard */     
if (!fgets(buf, sizeof buf, stdin)) {      if 
(ferror(stdin)) {      perror("stdin"); 
     exit(1);     } 
    exit(0);    }    if 
(write(sock, buf, strlen(buf)) < 0) {     
perror("write");     exit(1);    
}   }   if (FD_ISSET(sock,&rmask)) {    /* 
data from network */    bytesread = read(sock, buf, sizeof buf); 
   buf[bytesread] = '\0';    printf("%s: got %d 
bytes: %s\n", argv[0], bytesread, buf);   }  } } /* 
main - client.c */
 



More information about the Cygwin mailing list