Path: utzoo!attcan!uunet!samsung!zaphod.mps.ohio-state.edu!uwm.edu!bionet!ames!haven!ncifcrf!nlm-mcs!adm!news From: TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu Newsgroups: comp.unix.wizards Subject: HELP! BSD select() on socket (WIN/TCP for VMS) Message-ID: <22825@adm.BRL.MIL> Date: 21 Mar 90 15:00:34 GMT Sender: news@adm.BRL.MIL Lines: 191 Subject: Re: HELP! BSD select() on socket (WIN/TCP for VMS) In arctle <9003211059.AA22332@portia.stanford.edu> Cuong Writes: >>>gives without setting any fle descriptors (channels). For example, >> >>> if (nfd = (select(sock+1, &ready, &write, &except, &timeout)) <0) >>> perror("select"); >>Just take a very sharp look at the code and especially the parentheses. >> < has a higher priority than = >>-> >> if ((nfd = select(sock+1, &ready, &write, &except, &timeout)) <0) >> perror("select"); > >Yes, but it doesn't help explain non-blocking behavior for >Beng Hang. What does this perror() report? Bad access? >If so, have you considered Holger's comment re. "&read" instead >of "read"? What is the actual code? > >Good luck, > >Cuong I admit the mistake made of assigning nfd. Thanks for the correction! Below is the full source code (written in VAX-C). In the message <9003202303.AA24028@uunet.uu.net> sent by Larry Jones: >Since this is VMS, you might have more luck posting to a VMS news >group like comp.os.vms rather than a unix group! I know I should post it somewhere else, but since there are a lot of experts in this list, I push my luck. I apologize if I posted wrongly. -Beng Hang Tay (email address: taybengh@nusdiscs.bitnet) Department of Information Systems and Computer Science National University of Singapore ----------------------cut here--------------------------------------------- /* Filename : select_recv.c Author : Beng Hang Tay (taybengh) Description : Test program that receive buffers via stream service using socket accept and recv. Note that raccept routine does not distinguish the source, thus remote_sock is just a memory space that store the incoming struct sockaddr_in value. */ #include #include #include #include #include #include main() { int sock, socklen, size, newsock, flag, nfd; struct sockaddr_in local_in, remote_in; char inbuf[1024], outbuf[1024], hostname[15], str[10]; u_short local_port, remote_port; fd_set ready, write, except; struct timeval to; printf("pls input the local port (>1000)\n"); gets(str); local_port = atoi(str); printf("pls input the source hostname if any\n"); gets(hostname); printf("pls input the source port if any, otherwise 0\n"); gets(str); remote_port = atoi(str); printf("pls input the option if any, otherwise default=0\n"); gets(str); flag = atoi(str); /* Create socket from which to read */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock <0) { perror("opening stream socket"); exit(1); } /* create sockaddr */ local_in.sin_family = AF_INET; local_in.sin_addr.s_addr = INADDR_ANY; local_in.sin_port = htons(local_port); if (bind(sock, &local_in, sizeof(local_in))) { perror("binding stream socket"); exit(1); } /* create sockaddr */ printf("hostname =%s!\n", hostname); remote_in.sin_family = AF_INET; /* There is always a acess violation error due to the following statement, so it was masked off. The reason unknown! remote_in.sin_addr.s_addr = rhost(&hostname); */ remote_in.sin_port = htons(remote_port); #ifdef BDEBUG printf("\n\nbefore accept\n"); printf("sock: \n"); printf("family = %d \n", remote_in.sin_family); printf("sin_addr.s_addr = %x \n", ntohl(remote_in.sin_addr.s_addr)); printf("net number = %x \n", inet_netof(remote_in.sin_addr)); printf("local address part = %x \n", inet_lnaof(remote_in.sin_addr)); printf("port # = %u\n", ntohs(remote_in.sin_port)); #endif listen(sock, 5); for (;;) { /* * set the poll time to very large value */ to.tv_sec = 100000; to.tv_usec = 100000; FD_ZERO(&ready); FD_ZERO(&write); FD_ZERO(&except); FD_SET(sock, &ready); FD_SET(sock, &write); FD_SET(sock, &except); /* Null pointer is not working, giving acess violation problems! */ if ((nfd = select(sock+1, &ready, &write, &except, 0)) <0) { perror("select"); break; } /* * select() always return with nfd set to 1 */ #ifdef BDEBUG printf("select polling return nfd = %d\n", nfd); printf("timeval.tv_sec = %d, tv_usec = %d\n", to.tv_sec, to.tv_usec); printf("ready = %d, write =%d, except = %d\n", ready, write, except); #endif /* * NEVER COME INSIDE THE IF */ if (FD_ISSET(sock, &ready)) { socklen = sizeof(remote_in); newsock = accept(sock, &remote_in, &socklen); #ifdef BDEBUG printf("\nafter accept\n"); printf("newsock = %d: \n", newsock); printf("family = %d \n", remote_in.sin_family); printf("sin_addr.s_addr = %x \n", ntohl(remote_in.sin_addr.s_addr)); printf("net number = %x \n", inet_netof(remote_in.sin_addr)); printf("local address part = %x \n", inet_lnaof(remote_in.sin_addr)); printf("port # = %u\n", ntohs(remote_in.sin_port)); #endif if (newsock == -1) perror("accept"); else for (;;) { /* receive message */ if ((size = recv(newsock, inbuf, 1024, flag)) < 0) perror("receiving stream message"); if (size == 0) printf("ending connection\n"); else printf("received-->%s\n", inbuf); if (inbuf[0] == '\n' || inbuf[0] == '\0') exit(0); /* send message */ printf("\nPls input the messages:\n"); gets(outbuf); if ((size = send(newsock, outbuf, sizeof(outbuf), flag)) < 0) perror("send"); printf("buf sent-->%s, size =%d\n", outbuf, size); if (outbuf[0] == '\n' || outbuf[0] == '\0') exit(0); } } close(newsock); } } -----------------cut here-----------------------------------------------------