荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: jjk (UNIX+C+XML+?? 傻了?), 信区: Linux
标  题: HTTP相关小程序                         guru (转寄)[转载]
发信站: 荔园晨风BBS站 (Thu Apr 25 10:25:46 2002), 转信

【 以下文字转载自 jjk 的信箱 】
【 原文由 jjk.bbs@apue.dhs.org 所发表 】
发信人: guru (好读书,不求甚解), 信区: UNP
标  题: HTTP相关小程序
发信站: UNIX编程 (2001年08月18日20:15:06 星期六), 站内信件

  Re: Linux C 编程大家玩! [re: wjsoft]
作者 kenvin (stranger)
时间 02/23/00 10:00 PM



希望能对你有所帮助。

/********************
By kenvin hwang,1999.11
E-Mail:kenvin@21cn.com
Version revision:
1999.11.11: Version0.97,The first available version
*/

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

#define PORT 9242

void main(int argc, char* argv[]){
struct sockaddr_in server,client;
int fd,peer,size,pid;
int service(int fd);
void *sig_wait();

signal(SIGCLD,sig_wait());

//Create a new socket
if((fd = socket(AF_INET,SOCK_STREAM,0))<0){
printf("%s","Create socket failed!");
exit(-1);
}

//Populate the sockaddr_in struct
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(server.sin_zero),8);

//Bind the address to a socket describtor
if( bind(fd,(struct sockaddr *)&server,sizeof(struct sockaddr))<0){
close(fd);
printf("%s","Bind failed\n");
exit(-1);
}

//I'a ready now :-)
listen(fd,5);

for(;;){
if((peer = accept(fd,(struct sockaddr *)&client,&size))<0){
continue;
}

if((pid=fork())<0){
close(peer);
printf("%s","Fork() failed\n");
exit(-1);
}else if(pid==0){ //The child service the requery,parent back to listen
close(fd);
service(peer);
close(peer);
exit(0);
}
close(peer);
}
}

void *sig_wait(){
int pid,*status;
while((pid=wait3(status,WNOHANG,(struct rusage *)0))>0);
}

/*Service the client*/
int service(int sockfd){
char *data,*host;
void *buffer;
int fd, pos1, len;
char *str_clump(char *source, char *left, char *right) ;
char *str_mid(char *source , long start, long len);
char *str_replace(char *source ,char *pattern, char *new);
int sock_connect(int fd, char *host);

buffer = (void *)malloc(4096);
data = (char *)malloc(4096);
if(read(sockfd,buffer,4096)<=0) return(-1);
data = (char *)buffer;
host = str_clump(data,"//","/"); //Get the web server's host name

//Connect to remote host
fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_connect(fd,host)<0){
free(buffer);
free(data);
close(fd);
return(-1);
}

pos1 = str_pos(data,"GET ",0);
len = str_pos(data,"/",13) - pos1;
data = str_replace(data,str_mid(data, pos1, len), "GET ");
printf("%s",data);

//Read data from remote host and write it to the requery host
write(fd, (void *)data, strlen(data));
while((len=recv(fd, buffer,4096,0))>0){
printf("Read %d bytes data from %s\n",len,host);
if( send(sockfd, buffer,len,0)<0){
printf("Send failed!\n");
break;
}
}

close(fd);
free(data);
free(buffer);
return(0);
}

/*Connect to remote host*/
int sock_connect(int sockfd,char *host){

struct sockaddr_in target;
struct hostent *he;
u_long inaddr;

target.sin_family = AF_INET;
target.sin_port = htons(80);
bzero(&(target.sin_zero),8);

//First try number-dotted host name then try the offical host name
if((inaddr=inet_addr(host)) !=INADDR_NONE){
bcopy((char *)inaddr,(char *)&target.sin_addr,sizeof(inaddr));
}else if((he = gethostbyname(host))!=NULL){
bcopy(he->h_addr,(char *)&target.sin_addr,sizeof(inaddr));
}else{
printf("Resolve the host % failed!\n\n", host);
return(-1);
}

if(connect(sockfd,(struct sockaddr *)&target,sizeof(struct sockaddr))<0){
printf("Connect to host %s failed\n\n",host);
return(-1);
}

printf("Connect to host %s OK\n\n",host);
return(0);
}


/*Location the substring is source string*/
int str_pos(char *source, char *substring,int start ){
long pos;

if(start>strlen(source)){
return(-1);
}

pos = strstr(source+start,substring)-source;
return(pos+1);
}

/*Get a substring from start,and the lenght is len*/
char *str_mid(char *source, long start,long len){
char *temp,*ptr;
long i;

if(start>strlen(source)){
return(0);
}

temp =(char *) malloc(len+1);
ptr = temp;
source +=(--start);
for(i=0;i *(temp++)= *(source++);
}
*(temp+len)=0;

return(ptr);
}

/*Return the substring between left and right*/
char *str_clump(char *source, char *left, char *right){
int pos1,pos2,len;
pos1 = str_pos(source, left,0)+strlen(left);
pos2 = str_pos(source, right,pos1);
len = pos2 - pos1;
if(len<=0){
return(0);
}

return(str_mid(source,pos1,len));
}


/*Repalce a specifical substring is source with new substring */
char *str_replace(char *source,char *pattern,char *new){
int pos1,len;
char *buffer,*temp;

pos1 = str_pos(source,pattern,0);
if(pos1<=0){
return(source);
}

len = strlen(pattern);
buffer = source+pos1+len-1;
temp =str_mid(source,1,pos1-1);
temp = strcat(temp,new);
buffer = strcat(temp,buffer);

return(buffer);
}




--
Target Locked:Guru In Darkness.
我只是一只静静卧着的狮子。。。
※ 来源:·UNIX编程 www.tiaozhan.com/unixbbs/·[FROM: 202.114.36.176] --
※ 转寄:·UNIX编程 apue.dhs.org·[FROM: 210.39.3.50]
--
※ 转载:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.0.146]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店