hi all
i want develop a network_server. in my case the os is centos 4.2
which kernel is linux 2.6.xx so i can use the epoll function.

what i want. the server like Web Crawlers. it connect to the web
site and get the web

what i do. i create one pthread that make the new connct to the web
site.the main pthread use epoll function to process the connect socket
and send http request and get http responsion and close the socket.

my code:

#include
#include
#include

#include
#include
#include
#include
#include
#include


#define maxevents 65535

void *work(void *arg);
void setnonblocking(int sock);
void do_use_fd(struct epoll_event *events);

int lines ;
int epollfd = 0;
int main(){
pthread_t pid_t;
struct epoll_event ev, events[maxevents];

int nfds = 0;
int n = 0;
int status;

status = pthread_create(&pid_t, NULL, work, NULL); //
create one thread

if (status == 0) {
printf(" %d created \n", n);
} else {
printf(" %d create fail\n",
n);
exit(0);
}

epollfd = epoll_create(maxevents); // make epoll
socket

if (-1 == epollfd){
printf("error cannot epoll_create\n");
return 0;
}

sleep(10); // wait the pthread make connect first

for (;{
nfds = epoll_wait(epollfd, events, maxevents, 5); //
for(n = 0; n < nfds; ++n) {
printf("do do_use \n");
do_use_fd(events+n);
}
}
return 0;
}

void *work(void *arg){ // the thread make connect
int i = 0 ;
struct sockaddr_in clientadd;
int client;
struct epoll_event ev;
pthread_detach(pthread_self()); // this
int len = 0;
for (i = 0 ; i < 5 ;i ++){

client = socket(AF_INET , SOCK_STREAM, 0);
if (client < 0){
continue;
}
printf("begin memset \n");
memset(&clientadd,0 , sizeof(clientadd));
clientadd.sin_family = AF_INET;
clientadd.sin_addr.s_addr = inet_addr("192.168.1.150");
clientadd.sin_port = htons(80);
setnonblocking(client);

ev.events = EPOLLIN|EPOLLOUT |EPOLLERR;
ev.data.fd = client;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, client, &ev) < 0) {
fprintf(stderr, "epoll set insertion error: fd=%d",client);
return NULL;
}
printf("begin connect \n");
connect(client, (struct sockaddr*)&clientadd, sizeof(clientadd));
printf("begin connect \n");
sleep(5);

}
}

void setnonblocking(int sock){
int opts;
opts=fcntl(sock,F_GETFL);
if(opts<0)
{
perror("fcntl(sock,GETFL)");
exit(1);
}

opts = opts|O_NONBLOCK;
if(fcntl(sock,F_SETFL,opts)<0)
{
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
}

void do_use_fd(struct epoll_event *events){
struct epoll_event ev;
int n ;
int client = events->data.fd;
char buff[] ="get / http/1.1\r\n\r\n";
char inbuff[1024] ={0};
if (events->events |EPOLLIN){ // it can read
while (n = read(client, inbuff, 1024)){
printf("%d get %d \n", events->data.fd,
%d);
}

} else if (events->events |EPOLLOUT) {
write(events->data.fd, buff, strlen(buff));
return ; // if it is write break;
}

close(events->data.fd); // close socket
}


now in my case. i always get read(client, inbuff, 1024) = -1;
i think when i make connect for web site. it can not read(because the
web site didnot send data to me).
only can write. how to fix this promble.

the other question. 1 if the connct error. is the epoll_wait() can
catch it? otherwise the open sockets can not close. or the other way
to fix the promble?
2 what is different between the blocking read,write and no blocking
read write in my case?
thank you

ppmm