Can someone help me explain why the following program
exits where it does on a Digital UNIX box in violation
(I would say) of the manual entry as well as the
Sun and BSDI behavior?
Subsequent writing from the socket that select
reports is okay get a Broken Pipe error.
Jason
------------------------------------------------
/*
* Possible bug in non-blocking connect handling.
*/
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#define PORT 12037 /* port with nothing bound */
int main()
{
struct sockaddr_in sadr;
struct hostent *hostp;
struct timeval timeout;
fd_set fds;
char **addrp;
int sock, ret;
/* locate host */
hostp = gethostbyname("localhost");
if (hostp == NULL) {
perror("gethostbyname");
exit(1);
}
/* setup internet port */
sadr.sin_family = (u_short)AF_INET;
sadr.sin_port = htons(PORT);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
perror("socket");
exit(1);
}
/* make the fd non-blocking */
(void)fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
(void)memcpy((void *)&sadr.sin_addr, *hostp->h_addr_list,
sizeof(sadr.sin_addr));
/* connect or start the connection */
if (connect(sock, (void *)&sadr, sizeof(sadr)) == 0) {
(void)printf("Connected\n");
exit(0);
}
/* not waiting for connection? */
if (errno != EINPROGRESS && errno != EWOULDBLOCK) {
perror("socket");
/*
* THIS IS THE PROPER ERROR
*/
exit(1);
}
FD_ZERO(&fds);
timeout.tv_sec = 10;
timeout.tv_usec = 0;
for (;;) {
FD_SET(sock, &fds);
ret = select(sock + 1, 0L, &fds, 0L, &timeout);
if (ret > 0 && FD_ISSET(sock, &fds)) {
(void)printf("Huh? Connected to what????\n");
/*
* THIS IS NOT!! PROPER
*/
exit(0);
}
if (ret == 0) {
(void)printf("Timeout\n");
exit(1);
}
if (ret < 0 && errno != EINTR) {
perror("select");
exit(1);
}
}
}
Received on Tue Mar 26 1996 - 23:05:15 NZST