-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.h
More file actions
100 lines (79 loc) · 1.91 KB
/
Copy pathSocketClient.h
File metadata and controls
100 lines (79 loc) · 1.91 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
/*
* File: SocketClient.h
* Author: khoai
*
* Created on November 26, 2013, 3:18 PM
*/
#ifndef SOCKETCLIENT_H
#define SOCKETCLIENT_H
#include <iostream>
#include <string.h>
#include <fstream>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "HttpHeader.h"
#define MSG_LEN 512
#define MAX_HEADER_SIZE 4096
#define MAX_ADDR_LEN 512
#define MINI_LEN 128
#define MAX_PACKET_LEN 65536
#define MAX_FILE_PATH_LEN 260
using namespace std;
class SocketClient {
struct hostent *remoteHost;
struct sockaddr_in serverAddress;
int sock;
public:
bool isConnect;
SocketClient() {
isConnect = false;
}
SocketClient(string sHostAddress, int port) {
isConnect = InitConnection(sHostAddress, port);
}
int Send(char* msg) {
return send(sock, msg, strlen(msg), 0);
}
int Send(string msg) {
return Send(msg.c_str());
}
int Receive(char* buffer, int maxLen) {
int received = recv(sock, buffer, maxLen, 0);
if (received <= 0) {
perror("Fail, error code: received <= 0");
cout << endl;
}
return received;
}
void CloseSocket() {
shutdown(sock, SHUT_RDWR);
close(sock);
}
private:
bool InitConnection(string sHostAddress, int port) {
sock = socket(AF_INET, SOCK_STREAM, 0);
if (this->sock < 0)
perror("ERROR opening socket");
remoteHost = gethostbyname(sHostAddress.c_str());
if (this->remoteHost == NULL) {
fprintf(stderr, "ERROR, no such host\n");
return false;
}
bzero((char *) &serverAddress, sizeof (serverAddress));
serverAddress.sin_family = AF_INET;
bcopy((char *) remoteHost->h_addr,
(char *) &serverAddress.sin_addr.s_addr,
remoteHost->h_length);
serverAddress.sin_port = htons(port);
if (connect(sock, (struct sockaddr *) &serverAddress, sizeof (serverAddress)) < 0) {
perror("ERROR connecting");
return false;
}
return true;
}
};
#endif /* SOCKETCLIENT_H */