delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2000/06/04/06:59:15

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>, <http://sourceware.cygnus.com/ml/#faqs>
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" <peterpm AT xs4all DOT nl>
From: "Peter P. Mooring" <peterpm AT xs4all DOT nl>
To: <cygwin AT sourceware DOT cygnus DOT com>
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
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 <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[];
 {
=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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.72.3110.7"' name=3DGENERATOR>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT color=3D#000000 size=3D2>Hi all,</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>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.</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>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. </FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>Please help, t</FONT><FONT =
color=3D#000000=20
size=3D2>hanks,</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>Peter</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>This is the program, I call it like =
this 'client=20
1685 164.139.146.156'</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>#include =
&lt;sys/types.h&gt;<BR>#include=20
&lt;sys/socket.h&gt;<BR>#include &lt;sys/time.h&gt;<BR>#include=20
&lt;netinet/in.h&gt;<BR>#include &lt;errno.h&gt;<BR>#include=20
&lt;ctype.h&gt;<BR>#include &lt;netdb.h&gt;<BR>#include=20
&lt;stdio.h&gt;<BR>#include &lt;string.h&gt;</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>main(argc, argv)<BR>&nbsp;int=20
argc;<BR>&nbsp;char *argv[];<BR>&nbsp;{<BR>&nbsp;<BR>&nbsp;struct =
hostent=20
*hostp;<BR>&nbsp;struct servent *servp; <BR>&nbsp;struct sockaddr_in=20
server;<BR>&nbsp;int sock; <BR>&nbsp;static struct timeval timeout =3D { =
5, 0=20
};&nbsp;&nbsp; /* five seconds */ <BR>&nbsp;fd_set rmask, xmask,=20
mask;<BR>&nbsp;char buf[BUFSIZ]; =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
int nfound, bytesread;</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>&nbsp;if (argc !=3D 3) { <BR>&nbsp; =
(void)=20
fprintf(stderr,&quot;usage: %s service host\n&quot;,argv[0]); <BR>&nbsp; =

exit(1);<BR>&nbsp;} <BR>&nbsp;if ((sock =3D socket(AF_INET, SOCK_STREAM, =

IPPROTO_TCP)) &lt; 0) { <BR>&nbsp; perror(&quot;socket&quot;);<BR>&nbsp; =

exit(1);<BR>&nbsp;} <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
(isdigit(argv[1][0])) {<BR>&nbsp; static struct servent s; <BR>&nbsp; =
servp =3D=20
&amp;s; <BR>&nbsp; s.s_port =3D =
htons((u_short)atoi(argv[1]));<BR>&nbsp;} else if=20
((servp =3D getservbyname(argv[1], &quot;tcp&quot;)) =3D=3D 0) { =
<BR>&nbsp;=20
fprintf(stderr,&quot;%s: unknown service\n&quot;,argv[1]); <BR>&nbsp;=20
exit(1);<BR>&nbsp;} <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if =
((hostp =3D=20
gethostbyname(argv[2])) =3D=3D 0) { <BR>&nbsp; fprintf(stderr,&quot;%s: =
unknown=20
host\n&quot;,argv[2]); <BR>&nbsp; exit(1);<BR>&nbsp;} =
<BR>&nbsp;memset((void *)=20
&amp;server, 0, sizeof server);<BR>&nbsp;server.sin_family =3D AF_INET;=20
<BR>&nbsp;memcpy((void *) &amp;server.sin_addr, hostp-&gt;h_addr,=20
hostp-&gt;h_length); <BR>&nbsp;server.sin_port =3D servp-&gt;s_port; =
<BR>&nbsp;if=20
(connect(sock, (struct sockaddr *) &amp;server, sizeof server) &lt; 0) { =

<BR>&nbsp; (void) close(sock);<BR>&nbsp; =
perror(&quot;connect&quot;);&nbsp;=20
<BR>&nbsp; exit(1);<BR>&nbsp;}<BR>&nbsp;FD_ZERO(&amp;mask);&nbsp;=20
<BR>&nbsp;FD_SET(sock, &amp;mask);<BR>&nbsp;FD_SET(fileno(stdin), =
&amp;mask);=20
<BR>&nbsp;for (;;) {<BR>&nbsp; rmask =3D mask; <BR>&nbsp; nfound =3D=20
select(FD_SETSIZE, &amp;rmask, (fd_set *)0, (fd_set *)0, &amp;timeout);=20
<BR>&nbsp; if (nfound &lt; 0) {<BR>&nbsp;&nbsp; if (errno =3D=3D EINTR) =
{=20
<BR>&nbsp;&nbsp;&nbsp; printf(&quot;interrupted system call\n&quot;);=20
<BR>&nbsp;&nbsp;&nbsp; continue;<BR>&nbsp;&nbsp; } <BR>&nbsp;&nbsp; /* =
something=20
is very wrong! */ <BR>&nbsp;&nbsp; =
perror(&quot;select&quot;);<BR>&nbsp;&nbsp;=20
exit(1); <BR>&nbsp; }<BR>&nbsp; if (nfound =3D=3D 0) { <BR>&nbsp;&nbsp; =
/* timer=20
expired */ <BR>&nbsp;&nbsp; printf(&quot;Please type =
something!\n&quot;);=20
<BR>&nbsp;&nbsp; continue;<BR>&nbsp; } <BR>&nbsp; if =
(FD_ISSET(fileno(stdin),=20
&amp;rmask)) { <BR>&nbsp;&nbsp; /* data from keyboard */&nbsp; =
<BR>&nbsp;&nbsp;=20
if (!fgets(buf, sizeof buf, stdin)) {&nbsp; <BR>&nbsp;&nbsp;&nbsp; if=20
(ferror(stdin)) { <BR>&nbsp;&nbsp;&nbsp;&nbsp; =
perror(&quot;stdin&quot;);=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp; exit(1);<BR>&nbsp;&nbsp;&nbsp; }=20
<BR>&nbsp;&nbsp;&nbsp; exit(0);<BR>&nbsp;&nbsp; } <BR>&nbsp;&nbsp; if=20
(write(sock, buf, strlen(buf)) &lt; 0) { <BR>&nbsp;&nbsp;&nbsp;=20
perror(&quot;write&quot;);<BR>&nbsp;&nbsp;&nbsp; exit(1); =
<BR>&nbsp;&nbsp;=20
}<BR>&nbsp; } <BR>&nbsp; if (FD_ISSET(sock,&amp;rmask)) { =
<BR>&nbsp;&nbsp; /*=20
data from network */ <BR>&nbsp;&nbsp; bytesread =3D read(sock, buf, =
sizeof buf);=20
<BR>&nbsp;&nbsp; buf[bytesread] =3D '\0'; <BR>&nbsp;&nbsp; =
printf(&quot;%s: got %d=20
bytes: %s\n&quot;, argv[0], bytesread, buf); <BR>&nbsp; =
}<BR>&nbsp;}<BR>} /*=20
main - client.c */</FONT></DIV>
<DIV><FONT color=3D#000000 =
size=3D2><BR>&nbsp;</FONT></DIV></BODY></HTML>

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019