Mail Archives: cygwin/2000/06/04/06:59:15
------=_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> </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> </DIV>
<DIV><FONT color=3D#000000 size=3D2>#include =
<sys/types.h><BR>#include=20
<sys/socket.h><BR>#include <sys/time.h><BR>#include=20
<netinet/in.h><BR>#include <errno.h><BR>#include=20
<ctype.h><BR>#include <netdb.h><BR>#include=20
<stdio.h><BR>#include <string.h></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 size=3D2>main(argc, argv)<BR> int=20
argc;<BR> char *argv[];<BR> {<BR> <BR> struct =
hostent=20
*hostp;<BR> struct servent *servp; <BR> struct sockaddr_in=20
server;<BR> int sock; <BR> static struct timeval timeout =3D { =
5, 0=20
}; /* five seconds */ <BR> fd_set rmask, xmask,=20
mask;<BR> char buf[BUFSIZ]; =
<BR> =20
int nfound, bytesread;</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 size=3D2> if (argc !=3D 3) { <BR> =
(void)=20
fprintf(stderr,"usage: %s service host\n",argv[0]); <BR> =
exit(1);<BR> } <BR> if ((sock =3D socket(AF_INET, SOCK_STREAM, =
IPPROTO_TCP)) < 0) { <BR> perror("socket");<BR> =
exit(1);<BR> } <BR> if=20
(isdigit(argv[1][0])) {<BR> static struct servent s; <BR> =
servp =3D=20
&s; <BR> s.s_port =3D =
htons((u_short)atoi(argv[1]));<BR> } else if=20
((servp =3D getservbyname(argv[1], "tcp")) =3D=3D 0) { =
<BR> =20
fprintf(stderr,"%s: unknown service\n",argv[1]); <BR> =20
exit(1);<BR> } <BR> if =
((hostp =3D=20
gethostbyname(argv[2])) =3D=3D 0) { <BR> fprintf(stderr,"%s: =
unknown=20
host\n",argv[2]); <BR> exit(1);<BR> } =
<BR> memset((void *)=20
&server, 0, sizeof server);<BR> server.sin_family =3D AF_INET;=20
<BR> memcpy((void *) &server.sin_addr, hostp->h_addr,=20
hostp->h_length); <BR> server.sin_port =3D servp->s_port; =
<BR> if=20
(connect(sock, (struct sockaddr *) &server, sizeof server) < 0) { =
<BR> (void) close(sock);<BR> =
perror("connect"); =20
<BR> exit(1);<BR> }<BR> FD_ZERO(&mask); =20
<BR> FD_SET(sock, &mask);<BR> FD_SET(fileno(stdin), =
&mask);=20
<BR> for (;;) {<BR> rmask =3D mask; <BR> nfound =3D=20
select(FD_SETSIZE, &rmask, (fd_set *)0, (fd_set *)0, &timeout);=20
<BR> if (nfound < 0) {<BR> if (errno =3D=3D EINTR) =
{=20
<BR> printf("interrupted system call\n");=20
<BR> continue;<BR> } <BR> /* =
something=20
is very wrong! */ <BR> =
perror("select");<BR> =20
exit(1); <BR> }<BR> if (nfound =3D=3D 0) { <BR> =
/* timer=20
expired */ <BR> printf("Please type =
something!\n");=20
<BR> continue;<BR> } <BR> if =
(FD_ISSET(fileno(stdin),=20
&rmask)) { <BR> /* data from keyboard */ =
<BR> =20
if (!fgets(buf, sizeof buf, stdin)) { <BR> if=20
(ferror(stdin)) { <BR> =
perror("stdin");=20
<BR> exit(1);<BR> }=20
<BR> exit(0);<BR> } <BR> if=20
(write(sock, buf, strlen(buf)) < 0) { <BR> =20
perror("write");<BR> exit(1); =
<BR> =20
}<BR> } <BR> if (FD_ISSET(sock,&rmask)) { =
<BR> /*=20
data from network */ <BR> bytesread =3D read(sock, buf, =
sizeof buf);=20
<BR> buf[bytesread] =3D '\0'; <BR> =
printf("%s: got %d=20
bytes: %s\n", argv[0], bytesread, buf); <BR> =
}<BR> }<BR>} /*=20
main - client.c */</FONT></DIV>
<DIV><FONT color=3D#000000 =
size=3D2><BR> </FONT></DIV></BODY></HTML>
------=_NextPart_000_0008_01BFCE25.75CC5360--
- Raw text -