-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
165 lines (129 loc) · 4.7 KB
/
client.cpp
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
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fstream>
using namespace std;
/*
change color in c++ terminal:
const std::string red("\033[0;31m");
const std::string green("\033[1;32m");
const std::string yellow("\033[1;33m");
const std::string cyan("\033[0;36m");
const std::string magenta("\033[0;35m");
const std::string reset("\033[0m");
*/
class Client {
public:
Client(const char* serverIP, int port) : clientSocket(-1),clientDataSocket(-1) {
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
clientDataSocket = socket(AF_INET,SOCK_STREAM, 0);
if (clientSocket == -1 || clientDataSocket == -1) {
std::cerr << "Client: Socket creation failed" << std::endl;
return;
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(12345);
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
std::cerr << "Client1: Connection failed" << std::endl;
cout << "I am Ok." << endl;
return;
}
dataAddress.sin_family = AF_INET;
dataAddress.sin_port = htons(12543);
dataAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
cout << "I am on second" << endl;
if (connect(clientDataSocket, (struct sockaddr*)&dataAddress, sizeof(dataAddress)) == -1) {
std::cerr << "Client2: Connection failed" << std::endl;
return;
}
}
~Client() {
close(clientSocket);
close(clientDataSocket);
}
void sendData(const std::string& data) {
if (clientSocket != -1) {
send(clientSocket, data.c_str(), data.length(), 0);
}
}
std::string receiveData() {
char buffer[1024];
ssize_t bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesRead <= 0) {
return "";
}
buffer[bytesRead] = '\0';
return std::string(buffer);
}
int getClientSocket(){
return this->clientSocket;
}
void receiveFileFromServer() {
std::streamsize fileSize;
recv(clientSocket, reinterpret_cast<char*>(&fileSize), sizeof(fileSize), 0);
cout << "Client: fileSize: " << fileSize << endl;
char fileNameBuffer[1024];
recv(clientSocket, fileNameBuffer, sizeof(fileNameBuffer), 0);
std::string fileName(fileNameBuffer);
std::ofstream outFile(fileName, std::ios::binary);
cout << "Client: fileName: " << fileName << endl;
// Receive and write the file in chunks
const std::size_t bufferSize = 1024;
char buffer[bufferSize];
std::streamsize bytesRead;
std::streamsize totalBytesRead = 0;
while (totalBytesRead < fileSize) {
bytesRead = recv(clientDataSocket, buffer, std::min(fileSize - totalBytesRead, static_cast<std::streamsize>(bufferSize)), 0);
if (bytesRead <= 0) {
break; // Exit the loop when there is nothing more to receive or an error occurs
}
outFile.write(buffer, bytesRead);
totalBytesRead += bytesRead;
}
outFile.close();
}
private:
int clientSocket;
int clientDataSocket;
sockaddr_in serverAddress;
sockaddr_in dataAddress;
};
int main() {
Client client("127.0.0.1", 12345);
const std::string cyan("\033[0;36m");
const std::string green("\033[1;32m");
const std::string red("\033[0;31m");
if (client.getClientSocket() != -1) {
bool valid = true;
string message;
while(valid){
std::string receivedData = client.receiveData();
if (!receivedData.empty()) {
std::cout << green << "Received from server: " << receivedData << std::endl;
if (receivedData == "quit"){
valid = false;
break;
}
}
if (message == "quit"){
valid = false;
break;
}
cout << cyan << "Enter Command: \t";
getline(cin, message);
client.sendData(message);
if (message.find("RETR") != std::string::npos){
//string receivedMessage = client.receiveData();
// if(!(receivedMessage.find("ERROR") != std::string::npos)){
// client.receiveFileFromServer();
// }
client.receiveFileFromServer();
}
}
}
return 0;
}