-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestClient.c
124 lines (106 loc) · 3.15 KB
/
TestClient.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>
#define BUFSIZE 1254
#define PORT 8888
unsigned short calculateChecksum(FILE *file)
{
fseek(file, 0, SEEK_SET);
unsigned short checksum = 0;
int ch;
while ((ch = fgetc(file)) != EOF)
{
checksum += (unsigned char)ch;
}
fseek(file, 0, SEEK_SET);
return checksum;
}
void fileRecive(int sock, struct sockaddr_in *serv_addr, socklen_t adr_sz)
{
FILE *file = fopen("received_image.jpg", "wb");
if (file == NULL)
{
perror("File opening failed");
exit(EXIT_FAILURE);
}
char message[BUFSIZE];
size_t nbyte = BUFSIZE;
while (1)
{
nbyte = recvfrom(sock, message, nbyte, 0, (struct sockaddr *)serv_addr, &adr_sz);
if (!strcmp(message, "fin"))
{
printf("File received successfully.\n");
break;
}
fwrite(message, nbyte, 1, file);
}
fclose(file);
}
int main()
{
int sock;
char message[BUFSIZE];
int str_len;
socklen_t adr_sz;
struct sockaddr_in serv_addr, client_addr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
printf("socket() error\n");
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(PORT);
char hello_message[] = "hello i am client";
sendto(sock, hello_message, strlen(hello_message), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
adr_sz = sizeof(client_addr);
str_len = recvfrom(sock, message, BUFSIZE, 0, (struct sockaddr *)&client_addr, &adr_sz);
message[str_len] = '\0';
printf("Server say: %s\n", message);
fileRecive(sock, &serv_addr, adr_sz);
FILE *file2;
char *filename = "received_image.jpg";
if (file2 == NULL)
{
perror("File opening failed");
exit(EXIT_FAILURE);
}
// checksum 검사
recvfrom(sock, message, BUFSIZE, 0, (struct sockaddr *)&client_addr, &adr_sz);
while (1)
{
file2 = fopen(filename, "rb");
printf("received checksum = %s\n", message);
unsigned short checksum = calculateChecksum(file2);
printf("calcualte checksum = %d\n", checksum);
char checksumCheck[BUFSIZE];
sprintf(checksumCheck, "%d", checksum);
if (!strcmp(message, checksumCheck))
{
printf("checksum correct\n");
sprintf(message, "success");
printf("%s\n", message);
message[7] = '\0';
sendto(sock, message, strlen(message), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
fclose(file2);
break;
}
else
{
printf("checksum incorrect");
printf("\n");
char retry_message[BUFSIZE] = "retry";
retry_message[6] = '\0';
sendto(sock, retry_message, strlen(retry_message), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
fileRecive(sock, &serv_addr, adr_sz);
}
}
close(sock);
return 0;
}