-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.h
More file actions
88 lines (78 loc) · 2.2 KB
/
Client.h
File metadata and controls
88 lines (78 loc) · 2.2 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
#pragma once
#include "Common.h"
#include "Message.h"
#include "string.h"
namespace BERTCP
{
class Client
{
public:
inline static Client& TheClient()
{
static Client s;
return s;
}
public:
void sendMessage(unsigned char* data, size_t len)
{
#ifdef OS_WINDOWS
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
throw Exception("WSAStartup failed with error");
}
#endif
// open
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
unsigned char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
throw Exception("Socket creation error");
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
throw Exception("Invalid address/ Address not supported");
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
throw Exception("Connection failed");
}
// send(sock , hello , strlen(hello) , 0 );
// printf("Hello message sent\n");
Message msg;
cout << "!" << endl;
msg.attachBlob(data, len);
while (true)
{
unsigned char buf[1024];
cout << "." << endl;
auto bytesSent = msg.send(1024, buf);
cout << "1 sending " << bytesSent << "..." << endl;
send(sock, (char *)buf, bytesSent, 0 );
cout << "2" << endl;
if (msg.transferCompleted())
break;
cout << "3" << endl;
}
cout << "!!" << endl;
//
Message reply;
valread = read(sock, buffer, 1024);
cout << "( " << valread << " )" << endl;
reply.startReceiving(buffer);
reply.receive(1024, buffer);
cout << string((const char*)reply.binary()) << endl;
close(sock);
#ifdef OS_WINDOWS
WSACleanup();
#endif
}
};
}