J
Jim
Need some comments from anyone willing to help, please. See the code
included below. This compiles with GCC on FreeBSD 4.7. The only
point of it is to accept a socket connection. Nothing else has been
implemented.
It does not work. Everything matches how it should be (according to
the man pages on the relevant functions). It also matches line by
line with code from another application used as a known to be working
reference. Infact, some of the code is cut-and-paste from that
application. I re-compiled that application to ensure that it really
does work.. and it does. This code here, derived from it, does not.
As far as I can tell this should should be working but doesn't. It
failes with an EINVAL error on the accept() call.
I'd love any comments anyone would like to offer.
Thanks
-J
Here is the output of the compiled program being run (its called
"bcs") and the result when someone telnets to the port it is
listenining on (port 811):
bash-2.05b# uname -a
FreeBSD 4.7-RELEASE FreeBSD 4.7-RELEASE #22: Wed Aug 6 22:12:16 GMT
2003 root@:/usr/src/sys/compile/mykern i386
bash-2.05b#
bash-2.05b# ./bcs
DEBUG2
DEBUG3
accept: Invalid argument
bash-2.05b#
Here is the actual code in its entirety:
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <sys/file.h>
6 #include <netinet/in.h>
7 #include <time.h>
8 #include <ctype.h>
9 #include <unistd.h>
10 #include <errno.h>
11
12
13 int main ()
14 {
15 struct sockaddr addr;
16 int addrlen;
17 struct sockaddr salocal;
18 struct sockaddr_in *sin;
19 int rlistenfd;
20 int remotefd;
21 int result;
22 int status;
23 fd_set readfds;
24
25 remotefd = -1;
26
27 rlistenfd = socket(AF_INET, SOCK_STREAM,0);
28 if (rlistenfd < 0) {
29 (void)perror("socket");
30 exit(-1);
31 }
32 sin = (struct sockaddr_in *) & salocal;
33 memset ((char *) sin, '\0', sizeof (salocal));
34 sin->sin_family = AF_INET;
35 sin->sin_addr.s_addr = INADDR_ANY;
36 sin->sin_port = htons(811);
37 result = bind (rlistenfd, & salocal, sizeof (*sin));
38 if (result < 0) {
39 (void) perror ("bind");
40 exit(-1);
41 }
42 if(listen(rlistenfd, 2)) {
43 (void) perror ("listen");
44 exit(-1);
45 }
46
47 for(; {
48 FD_ZERO(&readfds);
49 FD_SET(rlistenfd, &readfds);
50
51 status = select(32, &readfds, NULL, NULL,
NULL);
52 if(status == -1) {
53 printf("DEBUG1\n");
54 if (errno == EINTR)
55 continue;
56 (void)perror("main:select");
57 printf("select status=%d\n", status);
58 exit(-1);
59 }
60 if(FD_ISSET(rlistenfd, &readfds)) {
61 printf("DEBUG2\n");
62 remotefd = accept(rlistenfd, &addr,
&addrlen);
63 if(remotefd==-1) {
64 printf("DEBUG3\n");
65 (void) perror("accept");
66 exit(1);
67 continue;
68 }
69 }
70 } /* for */
71 }
72
included below. This compiles with GCC on FreeBSD 4.7. The only
point of it is to accept a socket connection. Nothing else has been
implemented.
It does not work. Everything matches how it should be (according to
the man pages on the relevant functions). It also matches line by
line with code from another application used as a known to be working
reference. Infact, some of the code is cut-and-paste from that
application. I re-compiled that application to ensure that it really
does work.. and it does. This code here, derived from it, does not.
As far as I can tell this should should be working but doesn't. It
failes with an EINVAL error on the accept() call.
I'd love any comments anyone would like to offer.
Thanks
-J
Here is the output of the compiled program being run (its called
"bcs") and the result when someone telnets to the port it is
listenining on (port 811):
bash-2.05b# uname -a
FreeBSD 4.7-RELEASE FreeBSD 4.7-RELEASE #22: Wed Aug 6 22:12:16 GMT
2003 root@:/usr/src/sys/compile/mykern i386
bash-2.05b#
bash-2.05b# ./bcs
DEBUG2
DEBUG3
accept: Invalid argument
bash-2.05b#
Here is the actual code in its entirety:
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <sys/file.h>
6 #include <netinet/in.h>
7 #include <time.h>
8 #include <ctype.h>
9 #include <unistd.h>
10 #include <errno.h>
11
12
13 int main ()
14 {
15 struct sockaddr addr;
16 int addrlen;
17 struct sockaddr salocal;
18 struct sockaddr_in *sin;
19 int rlistenfd;
20 int remotefd;
21 int result;
22 int status;
23 fd_set readfds;
24
25 remotefd = -1;
26
27 rlistenfd = socket(AF_INET, SOCK_STREAM,0);
28 if (rlistenfd < 0) {
29 (void)perror("socket");
30 exit(-1);
31 }
32 sin = (struct sockaddr_in *) & salocal;
33 memset ((char *) sin, '\0', sizeof (salocal));
34 sin->sin_family = AF_INET;
35 sin->sin_addr.s_addr = INADDR_ANY;
36 sin->sin_port = htons(811);
37 result = bind (rlistenfd, & salocal, sizeof (*sin));
38 if (result < 0) {
39 (void) perror ("bind");
40 exit(-1);
41 }
42 if(listen(rlistenfd, 2)) {
43 (void) perror ("listen");
44 exit(-1);
45 }
46
47 for(; {
48 FD_ZERO(&readfds);
49 FD_SET(rlistenfd, &readfds);
50
51 status = select(32, &readfds, NULL, NULL,
NULL);
52 if(status == -1) {
53 printf("DEBUG1\n");
54 if (errno == EINTR)
55 continue;
56 (void)perror("main:select");
57 printf("select status=%d\n", status);
58 exit(-1);
59 }
60 if(FD_ISSET(rlistenfd, &readfds)) {
61 printf("DEBUG2\n");
62 remotefd = accept(rlistenfd, &addr,
&addrlen);
63 if(remotefd==-1) {
64 printf("DEBUG3\n");
65 (void) perror("accept");
66 exit(1);
67 continue;
68 }
69 }
70 } /* for */
71 }
72