-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNetwork.cpp
More file actions
162 lines (141 loc) · 3.71 KB
/
Network.cpp
File metadata and controls
162 lines (141 loc) · 3.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
#include <iostream>
#include <string>
#include "Network.h"
// Initialize winsock
bool Client::Initialize_winsock()
{
int iResult = WSAStartup(version, &wsaData);
if (iResult != 0)
{
std::cout << "WSAStartup failed: " << iResult << "\n";
return false;
}
return true;
}
// Create a socket
bool Client::CreateSocket()
{
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
std::cout << "Can't create socket, Error #" << WSAGetLastError() << "\n";
WSACleanup();
return false;
}
this->sock = sock;
return true;
}
// Bind the ip address and port to a socket
bool Client::Connect(std::string ipAddress, int port)
{
this->hint.sin_family = AF_INET;
this->hint.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &this->hint.sin_addr);
// Connect to server
while (connect(this->sock, (sockaddr*)&this->hint, sizeof(this->hint)) == SOCKET_ERROR)
{
std::cout << "Can't conect to server, Error #" << WSAGetLastError() << "\nTry Again in 5 seconds..\n";
Sleep(5000);
}
return true;
}
// Start The Connection
bool Client::Start(std::string ip_address, int port)
{
if (!Initialize_winsock())
return false;
std::cout << "Initialize Winsock Success!\n";
if (!CreateSocket())
return false;
std::cout << "Create Socket Success!\n";
if (!Connect(ip_address, port))
return false;
std::cout << "Connect Success!\n\n";
return true;
}
// Simple way to send Message
bool Client::Send(std::string msg)
{
std::cout << "Send\n\n";
int iSendResult = send(this->sock, msg.c_str(), msg.size() + 1, 0);
if (iSendResult == SOCKET_ERROR)
{
std::cout << "send failed with error: " << WSAGetLastError() << "\n";
return false;
}
return true;
}
// Simple way to recieve Message
bool Client::Recv(std::string& result)
{
std::cout << "Get\n\n";
char buf[4096];
ZeroMemory(buf, 4096);
int bytesReeceived = recv(this->sock, buf, 4096, 0);
if (bytesReeceived == SOCKET_ERROR)
{
std::cout << "Error in recv(). Error code #" << WSAGetLastError() << "\n";
return false;
}
result = std::string(buf, 0, bytesReeceived);
return true;
}
// Send File Function
bool Client::SendFile(std::string filePath)
{
int fileSize = 0;
char buf[256];
DWORD bytesRead = sizeof(DWORD);
HANDLE hFile = CreateFileA(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
fileSize = GetFileSize(hFile, NULL);
std::cout << "Size:\n";
std::cout << fileSize << "\n\n";
Send(std::to_string(fileSize));
if (fileSize == 0)
{
CloseHandle(hFile);
return false;
}
while (bytesRead != 0)
{
ReadFile(hFile, buf, sizeof(buf), &bytesRead, NULL);
send(this->sock, buf, bytesRead, 0);
}
CloseHandle(hFile);
return true;
}
// Recieve File Function
bool Client::RecvFile(std::string save_path)
{
int fileSize = 0;
std::string result;
HANDLE hFile = CreateFileA(save_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (!Recv(result))
return false;
fileSize = atoi(result.c_str());
}
std::cout << "File Size: " << fileSize << "\n";
if (fileSize == 0)
{
CloseHandle(hFile);
DeleteFileA(save_path.c_str());
return false;
}
int total_recv = 0;
char buf[256];
Sleep(50);
while (total_recv <= fileSize)
{
recv(this->sock, buf, sizeof(buf), 0);
WriteFile(hFile, buf, sizeof(buf), NULL, NULL);
total_recv += sizeof(buf);
}
Sleep(50);
std::cout << "Total Recv: " << total_recv << "\n\n";
std::cout << "Done\n";
CloseHandle(hFile);
return true;
}