-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyclientsocket.cpp
66 lines (56 loc) · 1.83 KB
/
myclientsocket.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
#include "myclientsocket.h"
#define BUFSIZE 256
MyClientSocket::MyClientSocket()
{
}
MyClientSocket::~MyClientSocket()
{
}
void MyClientSocket::mconnect(char ip[], int port) {
initSocket();
struct sockaddr_in addr;
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
#ifdef __linux__
if(inet_aton(ip, &addr.sin_addr) == 0)
#elif _WIN32
if(inet_pton(PF_INET,ip,&addr.sin_addr) == 0)
#endif
{
throw std::runtime_error(std::string("inet aton error"));
}
int rc = connect(sDescr, (struct sockaddr *)(&addr), sizeof(struct sockaddr_in));
if( rc == -1 ) {
throw std::runtime_error("connect error: " + std::string(strerror(errno)));
}
}
/*void MyClientSocket::mconnect(char ip[4], int port, unsigned long timeout) {
struct sockaddr_in addr;
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
set_non_blocked_impl(sDescr, true);
if(inet_aton(ip, &addr.sin_addr) == 0) {
std::runtime_error(std::string("inet aton error"));
}
int rc = connect(sDeskr, &addr, sizeof(struct sockaddr_in));
if( rc == -1 && errno != EINPROGRESS ) {
std::runtime_error(std::string("connect error"));
}
}*/
void MyClientSocket::msend(const std::vector<char> & msg) {
size_t size = msg.size();
char buf[BUFSIZE];
for(unsigned int i = 0; i < size; i+= BUFSIZE) {
memset(buf,0,BUFSIZE);
for(unsigned int j = 0; (j<BUFSIZE) && (j<size); j++) {
buf[j] = msg[i+j];
}
if(send(sDescr,buf,BUFSIZE, 0) == -1) { ///????
throw std::runtime_error(std::string("send_error"));
}
}
}