Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sourceware DOT cygnus DOT com Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com Message-ID: <000b01bfce14$b52dab00$6ed599d4@pc1> Reply-To: "Peter P. Mooring" From: "Peter P. Mooring" To: Subject: Socket - connect problem: The descriptor is a file, not a socket Date: Sun, 4 Jun 2000 13:04:47 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0008_01BFCE25.75CC5360" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 ------=_NextPart_000_0008_01BFCE25.75CC5360 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable 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.=20 Please help, thanks, Peter This is the program, I call it like this 'client 1685 164.139.146.156' #include #include #include #include #include #include #include #include #include main(argc, argv) int argc; char *argv[]; { =20 struct hostent *hostp; struct servent *servp;=20 struct sockaddr_in server; int sock;=20 static struct timeval timeout =3D { 5, 0 }; /* five seconds */=20 fd_set rmask, xmask, mask; char buf[BUFSIZ];=20 int nfound, bytesread; if (argc !=3D 3) {=20 (void) fprintf(stderr,"usage: %s service host\n",argv[0]);=20 exit(1); }=20 if ((sock =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {=20 perror("socket"); exit(1); }=20 if (isdigit(argv[1][0])) { static struct servent s;=20 servp =3D &s;=20 s.s_port =3D htons((u_short)atoi(argv[1])); } else if ((servp =3D getservbyname(argv[1], "tcp")) =3D=3D 0) {=20 fprintf(stderr,"%s: unknown service\n",argv[1]);=20 exit(1); }=20 if ((hostp =3D gethostbyname(argv[2])) =3D=3D 0) {=20 fprintf(stderr,"%s: unknown host\n",argv[2]);=20 exit(1); }=20 memset((void *) &server, 0, sizeof server); server.sin_family =3D AF_INET;=20 memcpy((void *) &server.sin_addr, hostp->h_addr, hostp->h_length);=20 server.sin_port =3D servp->s_port;=20 if (connect(sock, (struct sockaddr *) &server, sizeof server) < 0) {=20 (void) close(sock); perror("connect"); =20 exit(1); } FD_ZERO(&mask); =20 FD_SET(sock, &mask); FD_SET(fileno(stdin), &mask);=20 for (;;) { rmask =3D mask;=20 nfound =3D select(FD_SETSIZE, &rmask, (fd_set *)0, (fd_set *)0, = &timeout);=20 if (nfound < 0) { if (errno =3D=3D EINTR) {=20 printf("interrupted system call\n");=20 continue; }=20 /* something is very wrong! */=20 perror("select"); exit(1);=20 } if (nfound =3D=3D 0) {=20 /* timer expired */=20 printf("Please type something!\n");=20 continue; }=20 if (FD_ISSET(fileno(stdin), &rmask)) {=20 /* data from keyboard */ =20 if (!fgets(buf, sizeof buf, stdin)) { =20 if (ferror(stdin)) {=20 perror("stdin");=20 exit(1); }=20 exit(0); }=20 if (write(sock, buf, strlen(buf)) < 0) {=20 perror("write"); exit(1);=20 } }=20 if (FD_ISSET(sock,&rmask)) {=20 /* data from network */=20 bytesread =3D read(sock, buf, sizeof buf);=20 buf[bytesread] =3D '\0';=20 printf("%s: got %d bytes: %s\n", argv[0], bytesread, buf);=20 } } } /* main - client.c */ =20 ------=_NextPart_000_0008_01BFCE25.75CC5360 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi all,
For a project I need a small program = using a=20 socket to communicate with a server so I downloaded and installed = cygwin/gcc,=20 got an example program from the net and compiled (and updated in.h and = compiled=20 again). No errors.
When I run the program (on Win98), = see below, I=20 get the message 'The descriptor is a file, not a socket'. I scanned the = mailing=20 lists , and the net, but am stuck.
Please help, thanks,
Peter
 
This is the program, I call it like = this 'client=20 1685 164.139.146.156'
 
#include = <sys/types.h>
#include=20 <sys/socket.h>
#include <sys/time.h>
#include=20 <netinet/in.h>
#include <errno.h>
#include=20 <ctype.h>
#include <netdb.h>
#include=20 <stdio.h>
#include <string.h>
 
main(argc, argv)
 int=20 argc;
 char *argv[];
 {
 
 struct = hostent=20 *hostp;
 struct servent *servp;
 struct sockaddr_in=20 server;
 int sock;
 static struct timeval timeout =3D { = 5, 0=20 };   /* five seconds */
 fd_set rmask, xmask,=20 mask;
 char buf[BUFSIZ]; =
       =20 int nfound, bytesread;
 
 if (argc !=3D 3) {
  = (void)=20 fprintf(stderr,"usage: %s service host\n",argv[0]);
  = exit(1);
 }
 if ((sock =3D socket(AF_INET, SOCK_STREAM, = IPPROTO_TCP)) < 0) {
  perror("socket");
  = exit(1);
 }
        if=20 (isdigit(argv[1][0])) {
  static struct servent s;
  = servp =3D=20 &s;
  s.s_port =3D = htons((u_short)atoi(argv[1]));
 } else if=20 ((servp =3D getservbyname(argv[1], "tcp")) =3D=3D 0) { =
 =20 fprintf(stderr,"%s: unknown service\n",argv[1]);
 =20 exit(1);
 }
        if = ((hostp =3D=20 gethostbyname(argv[2])) =3D=3D 0) {
  fprintf(stderr,"%s: = unknown=20 host\n",argv[2]);
  exit(1);
 } =
 memset((void *)=20 &server, 0, sizeof server);
 server.sin_family =3D AF_INET;=20
 memcpy((void *) &server.sin_addr, hostp->h_addr,=20 hostp->h_length);
 server.sin_port =3D servp->s_port; =
 if=20 (connect(sock, (struct sockaddr *) &server, sizeof server) < 0) { =
  (void) close(sock);
  = perror("connect"); =20
  exit(1);
 }
 FD_ZERO(&mask); =20
 FD_SET(sock, &mask);
 FD_SET(fileno(stdin), = &mask);=20
 for (;;) {
  rmask =3D mask;
  nfound =3D=20 select(FD_SETSIZE, &rmask, (fd_set *)0, (fd_set *)0, &timeout);=20
  if (nfound < 0) {
   if (errno =3D=3D EINTR) = {=20
    printf("interrupted system call\n");=20
    continue;
   }
   /* = something=20 is very wrong! */
   = perror("select");
  =20 exit(1);
  }
  if (nfound =3D=3D 0) {
   = /* timer=20 expired */
   printf("Please type = something!\n");=20
   continue;
  }
  if = (FD_ISSET(fileno(stdin),=20 &rmask)) {
   /* data from keyboard */  =
  =20 if (!fgets(buf, sizeof buf, stdin)) { 
    if=20 (ferror(stdin)) {
     = perror("stdin");=20
     exit(1);
    }=20
    exit(0);
   }
   if=20 (write(sock, buf, strlen(buf)) < 0) {
   =20 perror("write");
    exit(1); =
  =20 }
  }
  if (FD_ISSET(sock,&rmask)) { =
   /*=20 data from network */
   bytesread =3D read(sock, buf, = sizeof buf);=20
   buf[bytesread] =3D '\0';
   = printf("%s: got %d=20 bytes: %s\n", argv[0], bytesread, buf);
  = }
 }
} /*=20 main - client.c */

 
------=_NextPart_000_0008_01BFCE25.75CC5360--