分享
 
 
 

Winsock程序设计初步之<二> 源程序实例(一)

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

//A simplest web server

//Written by Shen zhiliang for learning Winsock & HTTP

file://zhiliang@sina.com

#include "winsock.h"

#include "stdio.h"

#include "conio.h"

#include "io.h"

#define BUFLEN 2048

#define DEFPATH ("C:\\frontpage webs\\content\\")

#define DEFFILE ("INDEX.HTM")

#define HTTPHEAD ("HTTP/1.0 200 OK\010Date: Monday,

04-Jan-99 17:06:17 GMT\x0aServer: HTTP-Server/1.0\x0a

MIME-version: 1.0\x0a")

#define MIMEHTML ("Content-type: text/html\x0a

Last-modified: Friday, 26-Sep-97 09:36:54 GMT\x0a")

#define MIMEGIF ("Content-type: /image/gif\x0a

Last-modified: Friday, 26-Sep-97 09:36:54 GMT\x0a")

#define MIMEJPEG ("Content-type: /image/jpeg\x0a

Last-modified: Friday, 26-Sep-97 09:36:54 GMT\x0a")

#define MIMEPAIN ("Content-type: text/pain\x0a

Last-modified: Friday, 26-Sep-97 09:36:54 GMT\x0a")

#define HTMLHEAD ("<html><body>\x0a")

#define HTMLTAIL ("\n</body></html>")

void P(char * a)

{

printf("Error in : %s\n",a);

}

/*

char * httphead(FILE * fp)

{

struct tm *newtime;

time_t aclock;

time( &aclock );

newtime = localtime( &aclock );

printf( "The current date and time are: %s",

asctime( newtime ) );

}

*/

void HtmlError(SOCKET s,unsigned int code,char * msg)

{

char tmp[1024];

sprintf(tmp,"Error code: %d %s",code,msg);

send(s,HTTPHEAD,strlen(HTTPHEAD),0);

send(s,HTMLHEAD,strlen(HTMLHEAD),0);

send(s,tmp,strlen(tmp),0);

send(s,HTMLTAIL,strlen(HTMLTAIL),0);

}

void SendHtmlFile(SOCKET s,char * filename)

{

FILE * fp;

char * tmp;

char fullname[512];

char buf[1024];

int i;

strcpy(fullname,DEFPATH);

if(strlen(filename)==0||(strlen(filename)==1&&

filename[0]=='/'))

strcat(fullname,DEFFILE);

else

{

do{

tmp=strchr(filename,'/');

if(tmp!=NULL)

tmp[0]='\\';

}while(tmp!=NULL);

if(filename[0]=='\\')

strcat(fullname,&filename[1]);

else

strcat(fullname,filename);

}

FILE * fpt=fopen("recv.dat","a+b");

fwrite(fullname,sizeof(char),strlen(fullname),fpt);

fclose(fpt);

fp=fopen(fullname,"rb");

if(fp==NULL)

{

char msg[512];

if(filename[0]=='\\')

filename[0]='/';

sprintf(msg," URI no found: %s",filename);

HtmlError(s,404,msg);

return;

}

send(s,HTTPHEAD,strlen(HTTPHEAD),0);

if(stricmp(&filename[strlen(filename)-4],".GIF")==0)

send(s,MIMEGIF,strlen(MIMEGIF),0);

else if(stricmp(&filename[strlen(filename)-4],".JPG")==0||

stricmp(&filename[strlen(filename)-5],".JPEG")==0)

send(s,MIMEJPEG,strlen(MIMEJPEG),0);

else if(stricmp(&filename[strlen(filename)-4],".HTM")==0||

stricmp(&filename[strlen(filename)-5],".HTML")==0)

{

send(s,MIMEHTML,strlen(MIMEHTML),0);

}

long fs=_filelength(_fileno(fp));

buf[0]=0;

sprintf(buf,"Content-length: %ld\x0a\x0a",fs);

send(s,buf,strlen(buf),0);

for(i=0;!feof(fp);i++)

{

buf[i]=fgetc(fp);

if(i>=1023||feof(fp))

{

send(s,buf,i,0);

i=0;

}

}

fclose(fp);

}

void SocketError(char * call)

{

fprintf(stderr," WinSock Error# function: %s, error code:%ld\n",call,WSAGetLastError());

}

main(int argc,char ** argv)

{

int iRes,iPort,iTmp;

SOCKET s,rs;

SOCKADDR_IN sin,rsin;

WSADATA wsad;

WORD wVersionReq;

char recvBuf[BUFLEN];

if(argc<2)

{

fprintf(stderr,"Usage: WebServer 999\n\t999 - Port

number for this server.");

return -1;

}

iPort=0;

iPort=atoi(argv[1]);

if(iPort<=0)

{

fprintf(stderr,"must specify a port number");

return -1;

}

wVersionReq=MAKEWORD(1,1);

iRes=WSAStartup(wVersionReq,&wsad);

if(iRes!=0)

{

SocketError("WSAStartup()");

return -1;

}

s=socket(PF_INET,SOCK_STREAM,0);

if(s==INVALID_SOCKET)

{

SocketError("socket()");

return -1;

}

sin.sin_family=PF_INET;

sin.sin_port=htons(iPort);

sin.sin_addr.s_addr=INADDR_ANY;

iTmp=sizeof(sin);

if(bind(s,(LPSOCKADDR)&sin,iTmp)==SOCKET_ERROR)

{

SocketError("bind()");

closesocket(s);

WSACleanup();

return -1;

}

if(listen(s,1)==SOCKET_ERROR)

{

SocketError("listen()");

closesocket(s);

WSACleanup();

return -1;

}

fprintf(stderr,"WebServer Running......\n");

iTmp=sizeof(rsin);

rs=0;

while(1)

{

if(_kbhit()!=0)

{

if(_getch()!=27)

break;

if(rc!=0)

closesocket(rs);

closesocket(s);

WSACleanup();

fprintf(stderr,"WebServer Stopped......\n");

return 0;

}

rs=accept(s,(LPSOCKADDR)&rsin,&iTmp);

if(rs==INVALID_SOCKET)

{

SocketError("accept()");

closesocket(s);

WSACleanup();

return -1;

}

iRes=recv(rs,recvBuf,BUFLEN,0);

printf("RECEIVED DATA: \n---------------------------------\n%s\n---------------------------------\n",recvBuf);

if(iRes==SOCKET_ERROR)

{

SocketError("recv()");

closesocket(rs);

closesocket(s);

WSACleanup();

return -1;

}

char * sRes;

sRes=strstr(recvBuf,"GET");

if(sRes!=NULL&&(sRes-recvBuf)<3)

sRes=strchr(recvBuf,'/');

if(sRes!=NULL)

{

char * sRes1;

sRes1=strchr(sRes,'\r');

if(strchr(sRes,' ')<sRes1)

sRes1=strchr(sRes,' ');

if(sRes1!=NULL&&(sRes1-sRes)<256)

{

char tmp[256];

strncpy(tmp,sRes,(sRes1-sRes));

tmp[sRes1-sRes]=0;

int i;

for(i=strlen(tmp)-1;(tmp[i]==' '||tmp[i]=='\t')&&i>=0;i--)

tmp[i]=0;

for(i=0;tmp[i]==' '||tmp[i]=='\t';i++);

SendHtmlFile(rs,&tmp[i]);

}

}else

{

HtmlError(rs,202,"Bad request");

}

closesocket(rs);

}

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有