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