-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.c
66 lines (46 loc) · 1.38 KB
/
webserver.c
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
#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <unistd.h>
int main(){
int socketfd=socket(AF_INET,SOCK_STREAM,0);
if (socketfd<0){
perror("error creating socket");
}
printf("socket created sucessfully....\n");
struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(20001);
myaddr.sin_addr.s_addr=htons(INADDR_ANY);
if (bind(socketfd,(struct sockaddr *)&myaddr,sizeof(myaddr))<0){
perror("bind failed:");
return 1;
}
printf("bind was sucessfull...\n");
if((listen(socketfd,10))<0){
perror("error trying to listen on port:");
}
struct sockaddr_in clientaddr;
int addrsize = sizeof(clientaddr);
int *pointer=&addrsize;
//clientaddr.sin_family=AF_INET;
int clientFD=accept(socketfd,(struct sockaddr *)&clientaddr,&addrsize);//alternatively in the last argument give pointer
char buffer[1024];
while(true){
ssize_t amntrecv=recv(clientFD,buffer,1024,0);
if (amntrecv>0){
buffer[amntrecv]=0;
printf("-------------\n");
printf("%s \n",buffer);
printf("-------------\n");
}
if (amntrecv==0){
break;
}
}
close(clientFD);
shutdown(socketfd,SHUT_RDWR);
return 0;
}