-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
228 lines (190 loc) · 6.25 KB
/
socket.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
/* ____ _ __ _ _ _
| _ \ _ __ ___ _ __ ___ | |/ /__ _ _ __ ___ __ _| | | | __ _(_)_ __
| |_) | '__/ _ \ '_ ` _ \ | ' // _` | '_ ` _ \ / _` | | _ | |/ _` | | '_ \
| __/| | | __/ | | | | | | . \ (_| | | | | | | (_| | | | |_| | (_| | | | | |
|_| |_| \___|_| |_| |_| |_|\_\__,_|_| |_| |_|\__,_|_| \___/ \__,_|_|_| |_|
*/
/*Used domain sockets(AF_UNIX)*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#define BACKLOG 100 // The size of listen() queue for the server
void server(const char *socketname, int numberofclients, const char *filename)
{
struct sockaddr_un name;
int connectionsocketfd, returnvalue;
if ((connectionsocketfd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
{
perror("SERVER: socket");
exit(EXIT_FAILURE);
}
memset(&name, 0, sizeof(name));
name.sun_family = AF_UNIX;
strncpy(name.sun_path, socketname, sizeof(name.sun_path) - 1);
printf("SERVER: Binding socket to address %s\n", socketname);
if (bind(connectionsocketfd, (const struct sockaddr *)&name, sizeof(name)) < 0)
{
perror("SERVER: bind");
exit(EXIT_FAILURE);
}
printf("SERVER: Listening on socket %d\n", connectionsocketfd);
if (listen(connectionsocketfd, BACKLOG) < 0)
{
perror("SERVER: listen");
exit(EXIT_FAILURE);
}
puts("SERVER: Waiting for connections...");
fflush(stdout);
for (int i = 0; i < numberofclients; i++)
{
int datasocketfd = accept(connectionsocketfd, NULL, NULL);
if (datasocketfd < 0)
{
perror("SERVER: accept");
fflush(stderr);
continue;
}
printf("SERVER: Accepted connection on socket %d\n", datasocketfd);
fflush(stdout);
FILE *filepointer = fopen(filename, "r");
if (filepointer == NULL)
{
perror("SERVER: fopen");
close(datasocketfd);
close(connectionsocketfd);
exit(EXIT_FAILURE);
}
// Inform the filename to the client in a single write() call
int filenamebytes = write(datasocketfd, filename, strlen(filename)+1);
if (filenamebytes <= 0)
{
perror("SERVER: write");
fflush(stderr);
close(datasocketfd);
fclose(filepointer);
continue;
}
while (!feof(filepointer))
{
char buffer[50*1024]; // Asked to read() and write() BYTE-BY-BYTE in the question
int bytesread = fread(buffer, sizeof(char), sizeof buffer, filepointer);
if (ferror(filepointer))
{
perror("SERVER: fread");
fprintf(stderr, "SERVER: Warning: Client %d may receive corrupted file\n", datasocketfd);
fflush(stderr);
close(datasocketfd);
fclose(filepointer);
continue;
}
int byteswritten = write(datasocketfd, buffer, bytesread);
if (byteswritten <= 0)
{
perror("SERVER: write");
fprintf(stderr, "SERVER: Warning: Client %d may receive corrupted file\n", datasocketfd);
fflush(stderr);
close(datasocketfd);
fclose(filepointer);
continue;
}
}
printf("SERVER: Closing connection on socket %d\n", datasocketfd);
fflush(stdout);
close(datasocketfd);
fclose(filepointer);
}
}
void client(const char *socketname)
{
// retries
struct sockaddr_un serveraddr;
int returnvalue;
int datasocketfd;
if ((datasocketfd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
{
perror("CLIENT: socket");
fflush(stderr);
exit(EXIT_FAILURE);
}
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strncpy(serveraddr.sun_path, socketname, sizeof(serveraddr.sun_path) - 1);
// printf("CLIENT: Connecting to address %s\n", socketname);
while (connect(datasocketfd, (const struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0);
// Receive the filename from the server in a single read() call
char outputfilename[256];
if (read(datasocketfd, outputfilename, sizeof outputfilename) <= 0)
{
perror("CLIENT: read");
fflush(stderr);
close(datasocketfd);
exit(EXIT_FAILURE);
}
char buffer[50*1024];
char outputfilepath[512];
int bytesread;
sprintf(outputfilepath, "received-files/pid-%d_%s", getpid(), outputfilename);
FILE *filepointer = fopen(outputfilepath, "w");
if (filepointer == NULL)
{
perror("CLIENT: fopen");
close(datasocketfd);
exit(EXIT_FAILURE);
}
while ((bytesread = read(datasocketfd, buffer, sizeof buffer)) != 0)
{
if (bytesread < 0)
{
perror("CLIENT: read");
fflush(stderr);
close(datasocketfd);
fclose(filepointer);
exit(EXIT_FAILURE);
}
int byteswritten = fwrite(buffer, sizeof(char), bytesread, filepointer);
if (ferror(filepointer))
{
perror("CLIENT: fwrite");
fflush(stderr);
close(datasocketfd);
fclose(filepointer);
exit(EXIT_FAILURE);
}
}
close(datasocketfd);
fclose(filepointer);
}
int main(int argc, char *argv[])
{
srand(time(NULL));
if (argc < 3)
{
fprintf(stderr, "USAGE: %s <number-of-peers> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
int numberofclients = atoi(argv[1]);
char *filename = argv[2];
if (numberofclients < 1)
{
fprintf(stderr, "Number of peers should be atleast 1. Received %d\n", numberofclients);
exit(EXIT_FAILURE);
}
int parentPID = getpid();
char socketname[128];
sprintf(socketname, "/tmp/%d_%d", parentPID, rand());
for (int i = 0; i < numberofclients; i++)
{
if (fork() == 0)
{
client(socketname);
return 0;
}
}
server(socketname, numberofclients, filename);
return 0;
}