Winsock Program Design Preliminary <2> Source Program Example (1)

zhaozj2021-02-11  196

// a SimpleSt Web Server // Written by Shen Zhiliang for Learning Winsock & http file: //zhiliang@sina.com

#include "winsock.h"

#include "stdio.h"

#include "conoe.h"

#include "io.h"

#define buflen 2048

#define defpath ("C: // FrontPage Webs // Content //")

#define defding ("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 (" / x0a)

#define htmltail ("/ n ")

Void P (char * a)

{

Printf ("Error IN:% S / N", A);

}

/ *

Char * httphead (file * fp)

{

StructTM * newtime;

Time_t ACLOCK;

Time (& ACLOCK);

NEWTIME = LocalTime (& Aclk);

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) == 0 || (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 ";

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 (StriLename [Strlen (FileName) -4], ". GIF") == 0)

Send (S, MIMEGIF, STRLEN (MIMEGIF), 0);

ELSE IF (& FileName [Strlen (FileName) -4], "JPG") == 0 || /

Stricmp (& FileName [Strlen (FileName) -5], ". JPEG") == 0)

Send (S, MIMEJPEG, STRLEN (MIMEJPEG), 0);

ELSE IF (& 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)

{

"" "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 = incdr_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, Bufflen, 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 = 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;

}

转载请注明原文地址:https://www.9cbs.com/read-3774.html

New Post(0)