-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.c
249 lines (214 loc) · 6.12 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include "listFiles.h"
#define MAXLINE 1024
#define BUFFSIZE 100
#define ERROR 0 // Represents error
#define REQ 1 // Represents REQUEST for connection
#define ENDOFFILE 2 // Represents file is ended
#define ACK 3 // Represents acknowledgement of a particular sequence packet
#define DATA 4 // Represents DATA of the song requested
#define LIST 5 // Represents LIST OF THE SONGS is requested
int PORT1, PORT2;
/*
* error - wrapper for perror
*/
void error(char *msg)
{
perror(msg);
exit(1);
}
/*
* Datagram : sends song packets along with seqno, name of the file, and timestamps for latency calculation
*/
struct Datagram
{
int type;
int Seq_no;
char filename[25];
char buffer[MAXLINE];
struct timeval tv;
};
/*
* ListOfSongs : contains number of songs present and the name of the song files in 2d array of data
*/
struct ListOfSongs
{
int size;
char **data;
};
/*
* udp_send_list : sends number of songs and list of song file names using UDP protocol
*/
void *udp_send_list(char *ip_addr)
{
char buffer[BUFFSIZE];
char *message = "Hello Client";
int listenfd, len;
struct sockaddr_in servaddr, cliaddr;
bzero(&servaddr, sizeof(servaddr));
// Create a UDP Socket
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
servaddr.sin_addr.s_addr = inet_addr(ip_addr);
servaddr.sin_port = htons(PORT2);
servaddr.sin_family = AF_INET;
// bind server address to socket descriptor
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
while (1)
{
//receive the datagram
int size = 0;
len = sizeof(cliaddr);
int n = recvfrom(listenfd, buffer, sizeof(buffer),
0, (struct sockaddr *)&cliaddr, &len); //receive message from server
char **arr = listFiles(&size);
sendto(listenfd, &size, sizeof(size), 0,
(struct sockaddr *)&cliaddr, sizeof(cliaddr));
for (int j = 0; j < size; j++)
{
sendto(listenfd, arr[j], BUFFSIZE, 0,
(struct sockaddr *)&cliaddr, sizeof(cliaddr));
}
}
pthread_exit(NULL);
}
void usage()
{
printf("./server <ip-address> <port-no-1> <port-no-2>\n");
printf("<port-no-1> - This port is for song transmission.\n");
printf("<port-no-2> - This port is for transmitting list of songs available on the server side.\n");
exit(8);
}
int main(int argc, char **argv)
{
if(argc<4)
{
usage();
}
PORT1 = atoi(argv[2]);
PORT2 = atoi(argv[3]);
int sockfd; /* socket */
int clientlen; /* byte size of client's address */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
struct hostent *hostp; /* client host info */
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
int n; /* message byte size */
struct Datagram data;
/*
* socket: create the parent socket
*/
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval, sizeof(int));
/*
* build the server's Internet address
*/
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
serveraddr.sin_port = htons(PORT1);
/*
* bind: associate the parent socket with a port
*/
if (bind(sockfd, (struct sockaddr *)&serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
/*
* List of songs thread: A thread is created which will infinitely send number of songs and list of file names on PORT2
*/
pthread_t pthread;
pthread_create(&pthread, NULL, udp_send_list, argv[1]);
/*
* Infinitely accepts name of the file to be played from client side and sending that file packets to client side
*/
clientlen = sizeof(clientaddr);
while (1)
{
memset(&data, 0, sizeof(data));
n = recvfrom(sockfd, (struct Datagram *)&data, sizeof(data), 0,
(struct sockaddr *)&clientaddr, &clientlen);
if (data.type != REQ)
continue;
if (n < 0)
error("ERROR in recvfrom");
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
/*
* Opens file name received and shows error if it doesn't exist
*/
FILE *fp, *fp2;
printf("Client requested: %s\n", data.filename);
fp = fopen(data.filename, "r");
if (fp == NULL)
{
printf("File %s not found!\n", data.filename);
data.Seq_no = -1;
data.type = ERROR;
gettimeofday(&(data.tv), NULL);
sendto(sockfd, (struct Datagram *)&data, sizeof(data),
MSG_CONFIRM, (const struct sockaddr *)&clientaddr,
clientlen);
continue;
}
int cnt = 0;
while (1)
{
memset(&data, 0, sizeof(data));
data.Seq_no = cnt;
data.type = DATA;
gettimeofday(&(data.tv), NULL);
int siz = fread(&data.buffer, sizeof(data.buffer), 1, fp);
if (siz <= 0)
{
data.type = ENDOFFILE;
gettimeofday(&(data.tv), NULL);
sendto(sockfd, &data, sizeof(data),
0, (const struct sockaddr *)&clientaddr,
sizeof(clientaddr));
break;
}
gettimeofday(&(data.tv), NULL);
sendto(sockfd, (struct Datagram *)&data, sizeof(data),
MSG_CONFIRM, (const struct sockaddr *)&clientaddr,
clientlen);
/*
* STOP AND WAIT : Infinitely keeps receiving until appropiate ACK is received
*/
while (1)
{
struct Datagram data2;
n = recvfrom(sockfd, (struct Datagram *)&data2, sizeof(data2), 0,
(struct sockaddr *)&clientaddr, &clientlen);
if (data2.Seq_no == cnt)
break;
gettimeofday(&(data.tv), NULL);
sendto(sockfd, (struct Datagram *)&data, sizeof(data),
MSG_CONFIRM, (const struct sockaddr *)&clientaddr,
clientlen);
free((struct Datagram *)&data2);
}
cnt++;
}
printf("Song Buffered to the Client side.\n", data.filename);
}
}