-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sock.cpp
75 lines (55 loc) · 1.35 KB
/
Sock.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
67
68
69
70
71
72
73
74
/*
* File: Sock.cpp
* Author: Administrator
*
* Created on 2014年1月27日, 下午2:47
*/
#include <fcntl.h>
#include <sys/socket.h>
#include "Sock.h"
namespace CommLib {
Sock::Sock()
{
}
Sock::Sock(int __domain, int __type, int __protocol) {
sock_ = socket(__domain, __type, __protocol);
}
int Sock::Bind(SockAddr& addr) {
localaddr_ = addr;
return bind(sock_, (struct sockaddr*) &addr.GetAddr_in(), sizeof (addr));
}
int Sock::Connect(SockAddr& addr) {
remoteaddr_ = addr;
return connect(sock_, (struct sockaddr*) &addr.GetAddr_in(), sizeof (addr));
}
int Sock::Listen(int nlistens) {
return listen(sock_, nlistens);
}
int Sock::Setnonblocking() {
int ctl = fcntl(sock_, F_GETFL);
return fcntl(sock_, F_SETFL, ctl | O_NONBLOCK);
}
Sock::~Sock() {
}
void Sock::Attach(int sock) {
sock_ = sock;
}
int Sock::SetLocalAddr(SockAddr& addr) {
localaddr_ = addr;
}
SockAddr& Sock::GetLocalAddr() {
return localaddr_;
}
int Sock::SetRemoteAddr(SockAddr& addr) {
remoteaddr_ = addr;
}
SockAddr& Sock::GetRemoteAddr() {
return remoteaddr_;
}
int Sock::Getsock() {
return sock_;
}
void Sock::Close() {
close(sock_);
}
}