-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
186 lines (158 loc) · 6.41 KB
/
Copy pathclient.cpp
File metadata and controls
186 lines (158 loc) · 6.41 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <vector>
#include <filesystem>
#include <cstdlib>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include "protocol.h"
namespace fs = std::filesystem;
#define SERVER_IP "127.0.0.1"
#define PORT 5000
bool send_image(int sock, const std::string& filepath) {
if (!fs::exists(filepath)) {
std::cerr << "[Hata] Dosya bulunamadi: " << filepath << std::endl;
return false;
}
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "[Hata] Dosya acilamadi!" << std::endl;
return false;
}
std::streamsize file_size = file.tellg();
if (file_size < 0 || file_size > MAX_IMAGE_SIZE) {
std::cerr << "[Hata] Dosya boyutu 50 MB'dan buyuk olamaz!" << std::endl;
return false;
}
file.seekg(0, std::ios::beg);
std::string filename = fs::path(filepath).filename().string();
std::string extension = fs::path(filename).extension().string();
if (extension != ".jpg" && extension != ".JPG" &&
extension != ".png" && extension != ".PNG") {
std::cerr << "[Hata] Sadece JPG ve PNG dosyalari gonderilebilir!" << std::endl;
return false;
}
if (filename.empty() || filename.size() > UINT16_MAX) {
std::cerr << "[Hata] Dosya adi cok uzun!" << std::endl;
return false;
}
uint16_t name_len = static_cast<uint16_t>(filename.size());
MessageHeader header;
header.type = MSG_IMAGE;
header.filename_len = htons(name_len);
header.data_size = htonl(static_cast<uint32_t>(file_size));
if (!send_exact(sock, &header, sizeof(header))) return false;
if (!send_exact(sock, filename.data(), name_len)) return false;
std::vector<char> buffer(4096);
while (file_size > 0) {
std::streamsize to_read = std::min(file_size, static_cast<std::streamsize>(buffer.size()));
file.read(buffer.data(), to_read);
if (!send_exact(sock, buffer.data(), to_read)) return false;
file_size -= to_read;
}
std::cout << "[Bilgi] Gorsel gonderildi: " << filename << std::endl;
return true;
}
void receive_thread_func(int sock) {
while (true) {
MessageHeader header;
if (!read_exact(sock, &header, sizeof(header))) {
std::cout << "\n[Bilgi] Baglanti koptu." << std::endl;
break;
}
uint16_t filename_len = ntohs(header.filename_len);
uint32_t data_size = ntohl(header.data_size);
if (header.type != MSG_TEXT && header.type != MSG_IMAGE) {
std::cerr << "\n[Hata] Gecersiz mesaj turu." << std::endl;
break;
}
if ((header.type == MSG_TEXT && (filename_len != 0 || data_size > MAX_TEXT_SIZE)) ||
(header.type == MSG_IMAGE && (filename_len == 0 || data_size > MAX_IMAGE_SIZE))) {
std::cerr << "\n[Hata] Mesaj boyutu gecersiz." << std::endl;
break;
}
if (header.type == MSG_TEXT) {
std::vector<char> buffer(data_size + 1, 0);
if (!read_exact(sock, buffer.data(), data_size)) break;
std::cout << "\n[Gelen Mesaj]: " << buffer.data() << std::endl;
std::cout << "Sen: " << std::flush;
}
else if (header.type == MSG_IMAGE) {
std::string filename(filename_len, '\0');
if (!read_exact(sock, &filename[0], filename_len)) break;
fs::create_directories("alinanlar");
std::string save_path = "alinanlar/" + filename;
if (fs::exists(save_path)) {
std::cerr << "\n[Hata] Dosya zaten var, uzerine yazilmadi: " << save_path << std::endl;
std::vector<char> ignored(data_size);
if (!read_exact(sock, ignored.data(), data_size)) break;
continue;
}
std::ofstream out_file(save_path, std::ios::binary | std::ios::out);
if (!out_file.is_open()) {
std::cerr << "\n[Hata] Alinan dosya olusturulamadi." << std::endl;
break;
}
uint32_t remaining = data_size;
std::vector<char> buffer(4096);
bool success = true;
while (remaining > 0) {
uint32_t to_read = std::min(remaining, static_cast<uint32_t>(buffer.size()));
if (!read_exact(sock, buffer.data(), to_read)) {
success = false;
break;
}
out_file.write(buffer.data(), to_read);
remaining -= to_read;
}
if (success) {
std::cout << "\n[Gelen Gorsel]: " << save_path << " dosyasina kaydedildi." << std::endl;
} else {
std::cerr << "\n[Hata] Gorsel eksik alindi!" << std::endl;
}
std::cout << "Sen: " << std::flush;
}
}
}
int main() {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_addr{};
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
std::cout << "Sunucuya baglaniliyor..." << std::endl;
if (connect(sock_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Baglanti kurulamadi!" << std::endl;
return 1;
}
std::cout << "-> Baglandi! Metin yazabilir veya '!resim dosya.png' iletebilirsiniz." << std::endl;
std::thread receiver(receive_thread_func, sock_fd);
std::string line;
while (true) {
std::cout << "Sen: " << std::flush;
if (!std::getline(std::cin, line) || line == "cikis") break;
if (line.empty()) continue;
if (line.rfind("!resim ", 0) == 0) {
std::string path = line.substr(7);
send_image(sock_fd, path);
} else {
MessageHeader header;
header.type = MSG_TEXT;
header.filename_len = htons(0);
if (line.size() > MAX_TEXT_SIZE) {
std::cerr << "[Hata] Metin 1 MB'dan buyuk olamaz!" << std::endl;
continue;
}
header.data_size = htonl(static_cast<uint32_t>(line.size()));
if (!send_exact(sock_fd, &header, sizeof(header)) ||
!send_exact(sock_fd, line.data(), line.size())) break;
}
}
close(sock_fd);
if (receiver.joinable()) receiver.join();
return 0;
}