-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.c
More file actions
91 lines (84 loc) · 1.9 KB
/
main.c
File metadata and controls
91 lines (84 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include "file.h"
#include "headers.h"
short SocketCreate(void)
{
short hSocket;
printf("create the socket:\n");
hSocket=socket(AF_INET,SOCK_STREAM,0);
return hSocket;
}
int BindCreatedSocket(int hSocket)
{
int iRetval=-1;
int Clientport=9000;
struct sockaddr_in remote={0};
remote.sin_family=AF_INET;
remote.sin_addr.s_addr=htonl(INADDR_ANY);
remote.sin_port=htons(Clientport);
iRetval=bind(hSocket,(struct sockaddr*)&remote,sizeof(remote));
return iRetval;
}
int main(int argc,char *argv[])
{
int socket_desc,sock,clientLen,read_size,recevied;
struct sockaddr_in server, client;
char client_message[20000]={0};
char message[100]={0};
const char *pMessage="hello article world.com";
socket_desc=SocketCreate();
if(socket_desc==-1)
{
printf("could not create socket");
return 1;
}
printf("socket created\n");
if(BindCreatedSocket(socket_desc)<0)
{
perror("bind failed");
return 1;
}
printf("bind done\n");
listen(socket_desc,3);
printf("waiting for incoming connections...\n");
clientLen=sizeof(struct sockaddr_in);
sock=accept(socket_desc,(struct sockaddr*)&client,(socklen_t*)&clientLen);
if(sock<0)
{
perror("accept failed\n");
return 1;
}
printf("connection accepted \n");
memset(client_message,'\0',sizeof client_message);
memset(message,'\0',sizeof message);
if(recv(sock,client_message,19999,0)<0)
{
printf("recv failed");
//break;
}
//printf("client reply:%s\n",client_message);
char response[10000];
char final[400];
attach(response);
checkstatus(response);
dateandtime(response);
serverused(response);
contentlen(response);
contenttype(response);
connection(response);
lastmodified(response);
//strcat(final,response);
FILE *fp;
fp=fopen("index.html","r");
array(fp,response);
printf("response:%s",response);
int total=sizeof((response)-1);
if(sock>0)
send(sock,response,strlen(response),0);
return 0;
}