-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySocket.cpp
More file actions
98 lines (77 loc) · 2.1 KB
/
MySocket.cpp
File metadata and controls
98 lines (77 loc) · 2.1 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
#include "MySocket.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string>
using namespace std;
MySocket::MySocket(const char *inetAddr, int port) {
struct sockaddr_in server;
struct addrinfo hints;
struct addrinfo *res;
// set up the new socket (TCP/IP)
sockFd = socket(AF_INET,SOCK_STREAM,0);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
int ret = getaddrinfo(inetAddr, NULL, &hints, &res);
if(ret != 0) {
string str;
str = string("Could not get host ") + string(inetAddr);
throw SocketError(str.c_str());
}
server.sin_addr = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
server.sin_port = htons((short) port);
server.sin_family = AF_INET;
freeaddrinfo(res);
// conenct to the server
if( connect(sockFd, (struct sockaddr *) &server,
sizeof(server)) == -1 ) {
throw SocketError("Did not connect to the server");
}
}
MySocket::MySocket(void) {
sockFd = -1;
}
MySocket::MySocket(int socketFileDesc) {
sockFd = socketFileDesc;
}
MySocket::~MySocket(void) {
close();
}
void MySocket::write(string buffer) {
write_bytes(buffer.c_str(), buffer.size());
}
void MySocket::write_bytes(const void *buffer, int len) {
const unsigned char *buf = (const unsigned char *) buffer;
int bytesWritten = 0;
if (sockFd<0) {
throw SocketNotConnected();
}
while(len > 0) {
bytesWritten = ::write(sockFd, buf, len);
if(bytesWritten <= 0) {
throw SocketWriteError();
}
buf += bytesWritten;
len -= bytesWritten;
}
}
string MySocket::read() {
char buffer[4096];
if(sockFd<0) {
throw SocketNotConnected();
}
int ret = ::read(sockFd, buffer, sizeof(buffer));
if(ret <= 0) {
throw SocketReadError();
}
return string(buffer, ret);
}
void MySocket::close(void) {
if(sockFd<0) return;
::close(sockFd);
sockFd = -1;
}