Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!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: RE: HELP! BSD select() on socket (WIN/TCP for VMS) -> updated Message-ID: <22826@adm.BRL.MIL> Date: 21 Mar 90 15:00:45 GMT Sender: news@adm.BRL.MIL Lines: 161 I posted a old version source file in the previous posting. Below are the latest source code and still has the same problem. I apologize for the mistake made. I am new to this list and I am learning, please give me time to learn. Thanks! -Beng Hang --------------------------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, 0, 0, &to)) <0) */ if ((nfd = select(sock+1, &ready, &write, &except, &to)) <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 into the if-then */ 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); } }