-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
216 lines (168 loc) · 4.71 KB
/
client.cpp
File metadata and controls
216 lines (168 loc) · 4.71 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
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
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sstream>
#include <thread>
#include <signal.h>
#include <algorithm>
#include "utils.hpp"
int client;
bool isExit = false, stop_receiver = false;
// Print usage info and exit
void printUsage()
{
std::cout << "Usage: ./client <server_ip> <port> <username>" << std::endl;
exit(1);
}
// This runs in a seperate thread
// Keeps receiving information and prints data to stdout unless it's a file
void receiver(bool *exit, int *sclient)
{
int buffer_size = 1024;
char buffer[buffer_size];
while (!isExit)
{
// Clear buffer so that previous received chunk is not displayed
memset(buffer, 0, sizeof buffer);
recv(*sclient, buffer, buffer_size, 0);
// Logic for file transfers
if ( strncmp(buffer, "FILE", 4) == 0 )
{
char *tmp = buffer + 5;
std::string filename(tmp);
long file_size;
std::stringstream tmps;
// Need to do it in a better way
// Receive file size from server
do
{
memset(buffer, 0, sizeof buffer);
recv(client, buffer, 1, 0);
if (*buffer == '!')
break;
tmps << buffer;
} while (true);
file_size = std::stoi(tmps.str().c_str());
FILE *fd = fopen(filename.c_str(), "wb");
// Write to file
if (fd != NULL)
{
while (file_size > 0)
{
recv(*sclient, buffer, std::min(file_size, (long)buffer_size), 0);
int offset = 0;
do
{
size_t written_size = fwrite(&buffer[offset], 1, std::min(file_size, (long)buffer_size)-offset, fd);
offset += written_size;
} while (offset < std::min(file_size, (long)buffer_size));
file_size -= std::min(file_size, (long)buffer_size);
}
}
fclose(fd);
memset(buffer, 0, sizeof buffer);
std::cout << ">> ";
}
else
std::cout << buffer;
}
}
// Handle SIGINT
void closeClient(int s)
{
std::cout << "Exiting client..." << std::endl;
isExit = true;
std::string msg = "exit";
send(client, msg.c_str(), strlen(msg.c_str()), 0);
// Don't close the socket from client. Let the server kill the connection.
}
int main(int argc, char const *argv[])
{
signal(SIGINT, closeClient);
// struct sigaction sigIntHandler;
// sigIntHandler.sa_handler = closeServer;
// sigemptyset(&sigIntHandler.sa_mask);
// sigIntHandler.sa_flags = 0;
// sigaction(SIGINT, &sigIntHandler, NULL);
if (argc != 4)
{
printUsage();
}
int server_port = std::stoi(argv[2]);;
int bufsize = 1024;
char buffer[bufsize];
std::string server_ip(argv[1]);
std::string username(argv[3]);
struct sockaddr_in server_addr;
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
std::cout << "\nError establishing socket..." << std::endl;
exit(1);
}
std::cout << "\n=> Socket client has been created..." << std::endl;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
inet_pton(AF_INET, server_ip.c_str(), &server_addr.sin_addr);
if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
std::cout << "=> Connection to the server port number: " << server_port << std::endl;
std::cout << "=> Awaiting confirmation from the server..." << std::endl;
recv(client, buffer, bufsize, 0);
std::cout << "=> Connection confirmed, you are good to go..." << std::endl;
std::cout << std::endl << ">> ";
std::stringstream userss;
userss << username << std::endl;
send(client, userss.str().c_str(), strlen(userss.str().c_str()), 0);
std::thread receiver_thread(receiver, &stop_receiver, &client);
do {
// Read input
std::string msg;
getline(std::cin, msg);
std::stringstream s;
s << msg;
// Logic to send files
if (strncmp(msg.c_str(), "reply tcp", 9) == 0)
{
const char *tmp = msg.substr(10,msg.length()).c_str();
FILE *fd = fopen(tmp, "rb");
fseek(fd, 0, SEEK_END);
long file_size = ftell(fd);
rewind(fd);
std::stringstream ss;
ss << file_size << "!";
send(client, s.str().c_str(), strlen(s.str().c_str()), 0);
send(client, ss.str().c_str(), strlen(ss.str().c_str()), 0);
char buffer[1024];
do
{
size_t curr = std::min(file_size, (long)1024);
curr = fread(buffer, 1, curr, fd);
send(client, buffer, curr, 0);
file_size -= curr;
}
while (file_size > 0);
fclose(fd);
std::cout >> std::endl >> ">> ";
}
// If input is not empty send command
else if (!msg.empty())
{
s << std::endl;
send(client, s.str().c_str(), strlen(s.str().c_str()), 0);
}
if (strcmp(msg.c_str(), "exit") == 0)
{
isExit = true;
recv(client, buffer, bufsize, 0);
}
} while (!isExit);
std::cout << "\n=> Connection terminated.\nGoodbye...\n";
exit(0);
return 0;
}