-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
228 lines (185 loc) · 5.66 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
void doprocessing (int sock,int port);
int main( int argc, char *argv[] ) {
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
FILE * output;
output = fopen("node.txt","w");
fclose(output);
if (argc < 2) {
fprintf(stderr,"usage %s port\n", argv[0]);
exit(0);
}
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
printf("Server started, now listening....\n");
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
/* Create child process */
pid = fork();
if (pid < 0) {
perror("ERROR on fork");
exit(1);
}
if (pid == 0) {
/* This is the client process */
close(sockfd);
//processing-------------------------------------------------
int n,flag=0,port=ntohs(cli_addr.sin_port)+200,sock=newsockfd;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
if(strcmp(buffer,"REQUEST : node")==0)
flag=1;
else if(strcmp(buffer,"REQUEST : client")==0)
flag=2;
printf("Received Message - %s\n",buffer);
if(flag==1){
printf("Received port from client on which it will listen - %d\n",port);
/* Store the IP address and port number of the clients------------------------------------------------- */
char clntName[INET_ADDRSTRLEN];
if(inet_ntop(AF_INET,&cli_addr.sin_addr.s_addr,clntName,sizeof(clntName))!=NULL){
output = fopen("node.txt","a+");
fprintf(output,"%s%c%d\n",clntName,' ',port);
fclose(output);
} else {
printf("Unable to get address\n");
}
char* resp="RESPONSE : Node: 1,";
char buffer[50];
sprintf(buffer,"%s %d",resp, port);
printf("sending the following message - %s\n",buffer);
n = write(sock,buffer,strlen(buffer));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
}//if node
else if(flag==2){
char* resp="RESPONSE : client: 1";
n = write(sock,resp,strlen(resp));
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("Received Message from client - %s\n",buffer);
if(strcmp(buffer,"REQUEST : peer info")==0){
//read the file that stored the peer info
FILE *f = fopen("node.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);//send the pointer to beginning of the file
char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
printf("Server has the following info:\n%s",string);
//send the info to client
n = write(sock,string,strlen(string));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}//n
}//if peero info
}// if client
else printf("ERROR : Unknown REQEST message, no action taken\n");
// doprocessing(newsockfd,ntohs(cli_addr.sin_port)+200);
exit(0);
}
else {
close(newsockfd);
}
} /* end of while */
}
void doprocessing (int sock,int port) {
int n,flag=0;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
if(strcmp(buffer,"REQUEST : node")==0)
flag=1;
else if(strcmp(buffer,"REQUEST : client")==0)
flag=2;
printf("Received Message from client - %s\n",buffer);
printf("Received port from client on which it will listen - %d\n",port);
if(flag==1){
char* resp="RESPONSE : Node: 1,";
char buffer[50];
sprintf(buffer,"%s %d",resp, port);
printf("sending the following message - %s\n",buffer);
n = write(sock,buffer,strlen(buffer));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
}
else if(flag==2){
char* resp="RESPONSE : client: 1";
n = write(sock,resp,strlen(resp));
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("Received Message from client - %s\n",buffer);
if(strcmp(buffer,"REQUEST : peer info")==0){
//read the file that stored the peer info
FILE *f = fopen("node.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);//send the pointer to beginning of the file
char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
printf("Server has the following info:\n%s",string);
//send the info to client
n = write(sock,string,strlen(string));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}//n
}//if peero info
}// if client
else printf("ERROR : Unknown REQEST message, no action taken\n");
}