Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet.h>
#include<string.h>
#define port 1234
#define buffer_size 1024
#define CLEARSCREEN "\x1B[2J"
#define MAXCONNECTIONS 3
void sending (int sd,char *f1, char *prt);
struct sockaddr cli,ser;
main(){
int d,t=0,r,q,sd,b,i,j,con=1,id;
char buf[buffer_size+1],*method,*path,*prt;
char line[buffer_size];
struct sockaddr_in cli,ser;
int sw;
printf(CLEARSCREEN);
d=socket(AF_INET,SOCKSTREAM,0);
if(d>0)
printf(("\n Socket created");
else{
puts("socket not created");
exit(0);
}
ser.sin_family=AF_INET;
ser,sin_port=htons(port);
ser.sin_addr.s_addr=INADDR_ANY;
r=sizeof(struct sockadrr);
b=bind(d,(struct sockaddr *)&ser,r);
if(b<0)
exit(0);
listen(d,MAXCONNECTIONS);
do{
q=accept(d,(struct sockaddr *)&cli,&r)
id=fork()//unix multitasking
if(id==0)
printf("The client %S is connected", inet_ntoa(cli.sin_addr);
sz=sizeof(buf);
i=read(q,buf,sz);
buf[i]=0;
for(i=0;i<buffer_size && buf[i]!='\r'&&buf[i]='\n';i++)
{
line[i]=buf[i];
}
line[i]=0;
method=strtok(line," ");
path=strtok(NULL, " ");
prt=strtok(NULL," ");
if(strcmp(method, "GET")!=0)
exit(0);
sending(q, path, prt);
con++;
}while (con<=MAXCONNECTIONS);
close(d);
}
void sending(int sd,char *path, char *prt)
{
char ch, header[buffer_size + 1];
int len;
FILE *fp;
path++;
fp=fopen(path, "r");
sprintf(header, "%s 200 ok\r\nContent_type :text\html\r\n\r\n", prt);
len=strlen(header);
write(sd,header, len);
if(fp==0)
fp=fopen(filenotfound.html,"r");
do{
ch=fgetc(fp);
if(ch!=EOF)
write(sd,&ch,1);
}while(ch!=EOF)
fclose(fp);
shutdown(sd,1);
}
Please use [code][/code]