-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
95 lines (84 loc) · 1.74 KB
/
server.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <strings.h>
#define SIZE 1024
#define STD_IN 0
void communicate(int fd)
{
char buff[SIZE];
while(1)
{
bzero(buff,SIZE);
read(fd,buff,SIZE);
printf("Client Sent : %s",buff);
printf("Sent a response \n");
read(STD_IN,buff,SIZE);
write(fd,buff,SIZE);
if(strncmp("exit",buff,4) == 0)
{
printf("Exit...");
break;
}
}
}
int main()
{
int server_file_descriptor, connection_file_descriptor;
struct sockaddr_in server, client;
//create socket
server_file_descriptor = socket(AF_INET,SOCK_STREAM,0);
if(server_file_descriptor == -1)
{
perror("Failes to create socket");
exit(1);
}
else
{
printf("Socket created successfully ...");
}
//Socket configuration
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(8080);
//Binding
if(bind(server_file_descriptor,(struct sockaddr *)&server,sizeof(server)) == 0)
{
printf("\nBinding successful...");
}
else
{
perror("Bind failed");
exit(1);
}
if(listen(server_file_descriptor,5) == 0)
{
printf("\nListening..,");
}
else
{
perror("\nListening failed...");
exit(1);
}
//Accepting connections
int size = sizeof(client);
connection_file_descriptor = accept(server_file_descriptor,(struct sockaddr *)&client,&size);
if(connection_file_descriptor < 0)
{
perror("Connection failed..\n");
exit(1);
}
else
{
printf("Connected..\n");
}
communicate(connection_file_descriptor);
close(server_file_descriptor);
return 0;
}