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

类别:VC语言 点击:0 评论:0 推荐:

 

//A simplest web server

//Written by Shen zhiliang for learning Winsock & HTTP

file://[email protected]

 

#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;

}

 

本文地址:http://com.8s8s.com/it/it3656.htm