What OS are you on? IOt works perfectly as the following shows on freebsd
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#define MAXCONN OPEN_MAX-5
#define SRC_IP "206.83.178.152"
#define SRC_PORT 8000
#define LFD_PORT 9000
#define DATA "\
HTTP/1.0 200 OK\n\
Server: Digital Video Technology PUSH1\n\
Expires: 0\n\
Pragma: no-cache\n\
Content-Type: multipart/x-mixed-replace;boundary=ThisRandomString\n\n"
void bpipe();
int cur_fd;
main()
{
int i;
int maxfd;
int lfd;
int src; /* vid src */
int nfd=-1;
int slot[MAXCONN];
char s[500];
fd_set rset, wset;
struct timeval tv={0,0};
struct sockaddr_in sin;
signal(SIGPIPE,bpipe);
src=socket(AF_INET,SOCK_STREAM,0);
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr(SRC_IP);
sin.sin_port=htons(SRC_PORT);
connect(src,(struct sockaddr *) &sin,sizeof(sin));
lfd=socket(AF_INET,SOCK_STREAM,0);
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(LFD_PORT);
bind(lfd,(struct sockaddr *) &sin,sizeof(sin));
listen(lfd,1);
for(i=0;i<MAXCONN;i++)
slot[i]=-1;
while(1) {
maxfd=0;
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_SET(lfd,&rset);
FD_SET(src,&rset);
maxfd=src;
if(lfd>src)
maxfd=lfd;
for(i=0;i<MAXCONN;i++)
if(slot[i]!=-1) {
FD_SET(slot[i],&wset);
if(slot[i]>maxfd)
maxfd=slot[i];
}
printf("maxfd=%d\n",maxfd);
select(maxfd+1,&rset,&wset,NULL,&tv);
if(FD_ISSET(src,&rset)) {
readn(src,s,500);
}
if(FD_ISSET(lfd,&rset))
for(i=0;i<MAXCONN;i++)
if(slot[i]==-1) {
slot[i]=accept(lfd,NULL,NULL);
sprintf(s,"%s",DATA);
write(slot[i],s,strlen(DATA));
printf("accepted %d\n",slot[i]);
break;
}
for(i=0;i<MAXCONN;i++)
if(slot[i]!=-1)
if(FD_ISSET(slot[i],&wset)) {
printf("reading %d\n",slot[i]);
cur_fd=slot[i];
write(slot[i],s,500);
slot[i]=cur_fd;
}
}
Quote:}
readn(rfd,buf,len)
int rfd;
char *buf;
int len;
{
int n;
char *bp;
bp = buf;
while(len > 0) {
n = len;
if((n=read(rfd,bp,n)) < 0){
perror("readn: read error");
break;
}
else if(n == 0){
perror("readn: unexpected EOF");
break;
}
len -= n;
bp += n;
}
Quote:}
void bpipe()
{
printf("closing %d\n",cur_fd);
close(cur_fd);
cur_fd=-1;
Quote:}
Quote:>Hi,
>This is a question on select() usage:
>I've a scenerio wherein I expect connect() requests
>from multiple clients. My server application starts
>up different instances to service the client requests.
>I'm using select() call with readfd set to the bind'ed
>socket to accept the connections.
>I notice a peculiar thing:
>If the client sends a connect() to the server, the
>connect() call succeeds, but, the server doesn't come
>to know of the client connect request.
>However, if I change the listening socket on the server
>to NON-Blocking, (using ioctl with FIONBIO), the server
>does accept connection.
>I would like to know the concept behind this. Why doesn't
>the server get the connect request incase of blocking socket ?
>I sincerely appreciate your comments/suggestions.
>Regards,
>Vishal Murgai.
>------------------------------------------------------------------------