-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathser.c
292 lines (246 loc) · 9.06 KB
/
ser.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*#include "headsock.h"
void str_ser1(int sockfd); // transmitting and receiving function
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); //create socket
if (socket<0) {
printf("error in socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYUDP_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) { //bind socket
printf("error in binding");
exit(1);
}
printf("start receiving\n");
while(1) {
str_ser1(sockfd); // send and receive
}
close(sockfd);
exit(0);
}
#include "headsock.h"
// Function Protoypes
void readfromsocket(int sockfd); // Receive data from socket sockfd
void send_ack(int sockfd, struct sockaddr *client_addr, socklen_t client_addrlen, long fileoffset); // Block until acknowledge is sent
int main(int argc, char *argv[]) {
// Setup server socket address
struct sockaddr_in server_addr_in;
server_addr_in.sin_family = AF_INET;
server_addr_in.sin_port = htons(MYUDP_PORT); // htons() converts port number to big endian
server_addr_in.sin_addr.s_addr = INADDR_ANY; // INADDR_ANY=0, 0 means receive data from any IP address
memset(&(server_addr_in.sin_zero), 0, sizeof(server_addr_in.sin_zero));
// Typecast internet socket address (struct sockaddr_in) to generic socket address (struct sockaddr)
struct sockaddr *server_addr = (struct sockaddr *)&server_addr_in;
socklen_t server_addrlen = sizeof(struct sockaddr);
// Create UDP socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) { printf("error creating socket"); exit(1); }
// Bind socket address to socket (server only, client don't need)
if (bind(sockfd, server_addr, server_addrlen) == -1) { printf("error in binding"); exit(1); }
// Read file data from socket
while (1) {
printf("Ready to receive data\n");
readfromsocket(sockfd);
}
close(sockfd);
// Compare myfile.txt and myUDPreceive.txt
printf("Differences between myfile.txt and myUDPreceive.txt:\n");
char * const args[]={"diff", "myfile.txt", "myUDPreceive.txt"};
execv("/usr/bin/diff", args);
}
void readfromsocket(int sockfd) {
//---------------------------------------------//
// buffer used to contain the incoming packet. //
//---------------------------------------------//
char packet[DATAUNIT];
// Create empty struct to contain client address
// It will be filled in by recvfrom()
struct sockaddr client_addr;
socklen_t client_addrlen = sizeof(struct sockaddr);
// Get filesize from client
long filesize;
int n = recvfrom(sockfd, &filesize, sizeof(filesize), 0, &client_addr, &client_addrlen);
if (n < 0) { printf("error in receiving packet\n"); exit(1); }
send_ack(sockfd, &client_addr, client_addrlen, 0); // Acknowledge that filesize has been received
printf("Client says the file is %ld bytes big. DATAUNIT %d bytes\n", filesize-1, DATAUNIT);
//----------------------------------------//
// buffer used to contain the entire file //
//----------------------------------------//
char filebuffer[filesize];
int dum = 1; // data unit multiple
long fileoffset = 0; // Tracks how many bytes have been received so far
char packetlastbyte; // Tracks the last byte of the latest packet received
//-----------------------------------------------------------------------------------------------------------//
// Keep reading from socket until the last byte of the latest packet is an End of Transmission character 0x4 //
//-----------------------------------------------------------------------------------------------------------//
do {
// Depending on dum, read packets of DATAUNIT size 1, 2, 3 or 4 times before sending an ACK
for (int i=0; i<dum; i++) {
// Read incoming packet (recvfrom will block until data is received)
int bytesreceived = recvfrom(sockfd, &packet, DATAUNIT, 0, &client_addr, &client_addrlen);
if (bytesreceived < 0) {
printf("error in receiving packet\n");
exit(1);
}
// Append packet data to filebuffer
memcpy((filebuffer + fileoffset), packet, bytesreceived);
fileoffset += bytesreceived;
if ((packetlastbyte = packet[bytesreceived-1]) == 0x4) break;
}
// Acknowledge that packet has been received
send_ack(sockfd, &client_addr, client_addrlen, fileoffset);
dum = (++dum % 5 == 0) ? 1 : dum % 5;
} while (packetlastbyte != 0x4);
fileoffset-=1; // Disregard the last byte of filebuffer because it is the End of Transmission character 0x4
// Open file for writing
char filename[] = "myUDPreceive.txt";
FILE* fp = fopen(filename, "wt");
if (fp == NULL) { printf("File %s doesn't exist\n", filename); exit(1); }
// Copy the filebuffer contents into file
fwrite(filebuffer, 1, fileoffset, fp);
fclose(fp);
printf("File data received successfully, %d bytes written\n", (int)fileoffset);
}
void send_ack(int sockfd, struct sockaddr *addr, socklen_t addrlen, long fileoffset) {
const int ACKNOWLEDGE = 1;
int ack_sent = 0;
int ack_thresh = 10;
while (!ack_sent) {
if (sendto(sockfd, &ACKNOWLEDGE, sizeof(ACKNOWLEDGE), 0, addr, addrlen) >= 0) {
ack_sent = 1;
} else {
if (ack_thresh-- <= 0) {
printf("(%ld) emergency breakout of ack error loop\n", fileoffset * DATAUNIT);
exit(1);
} else printf("error sending ack, trying again..\n");
}
}
}*/
/**********************************
udp_ser.c: the source file of the server in udp transmission
***********************************/
#include "headsock.h"
void str_ser(int sockfd, struct sockaddr *addr, int addrlen); // transmitting and receiving function
void compareFiles(); // check if the file received is correct by comparing with original
int main(void)
{
int sockfd, ret;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); //create socket
if (sockfd<0)
{
printf("error in socket!");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYUDP_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr("172.0.0.1");
bzero(&(my_addr.sin_zero), 8);
ret = bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)); //bind socket
if (ret<0)
{
printf("error in binding");
exit(1);
}
while(1) {
printf("waiting for data\n");
str_ser(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_in)); //receive packet and response
compareFiles();
}
close(sockfd);
exit(0);
}
void str_ser(int sockfd, struct sockaddr *addr, int addrlen)
{
char buf[BUFSIZE];
FILE *fp;
char recvs[4*DATALEN];
struct ack_so ack;
int end, n = 0;
long lseek=0;
end = 0;
while(!end)
{
if ((n= recvfrom(sockfd, &recvs, 4*DATALEN, 0, addr, (socklen_t *)&addrlen))==-1) //receive the packet
{
printf("error when receiving\n");
exit(1);
}
if (recvs[n-1] == '\0') //if it is the end of the file
{
end = 1;
n --;
}
memcpy((buf+lseek), recvs, n);
lseek += n;
ack.num = 1;
ack.len = 0;
if ((n = sendto(sockfd, &ack, 2, 0, addr, addrlen))==-1)
{
printf("send error!"); //send the ack
exit(1);
}
}
if ((fp = fopen ("myUDPreceive.txt","wt")) == NULL)
{
printf("File doesn't exist\n");
exit(0);
}
fwrite (buf , 1 , lseek , fp); //write data into file
fclose(fp);
printf("a file has been successfully received!\nthe total data received is %d bytes\n", (int)lseek);
}
void compareFiles()
{
FILE *fp1;
FILE *fp2;
if((fp1 = fopen ("myfile.txt","r+t")) == NULL)
{
printf("File fp1 doesn't exit\n");
exit(0);
}
if((fp2 = fopen ("myUDPreceive.txt","r+t")) == NULL)
{
printf("File fp2 doesn't exit\n");
exit(0);
}
// fetching character of two file
// in two variable ch1 and ch2
char ch1 = getc(fp1);
char ch2 = getc(fp2);
// error keeps track of number of errors
// pos keeps track of position of errors
// line keeps track of error line
int error = 0, pos = 0, line = 1;
// iterate loop till end of file
while (ch1 != EOF && ch2 != EOF)
{
pos++;
// if both variable encounters new
// line then line variable is incremented
// and pos variable is set to 0
if (ch1 == '\n' && ch2 == '\n')
{
line++;
pos = 0;
}
// if fetched data is not equal then
// error is incremented
if (ch1 != ch2)
{
error++;
printf("Line Number : %d \tError"
" Position : %d \n", line, pos);
}
// fetching character until end of file
ch1 = getc(fp1);
ch2 = getc(fp2);
}
printf("Total Errors : %d\t \n", error);
}