-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsock.cpp
More file actions
112 lines (90 loc) · 2.39 KB
/
Copy pathsock.cpp
File metadata and controls
112 lines (90 loc) · 2.39 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
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "sock.h"
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
Sock::Sock(sock_type _type, std::string address, int port){
this->sock_addr.sin_family = AF_INET;
this->sock_addr.sin_port = htons(port);
inet_pton(AF_INET, address.c_str(), &this->sock_addr.sin_addr.s_addr);
this->mode = 1;
this->type = _type;
if(this->type == udp){
this->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
}
else{
if(this->type == tcp){
this->sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
}
int iSetOption = 1;
setsockopt(this->sockfd, SOL_SOCKET, SO_REUSEADDR,(char*) &iSetOption, sizeof(iSetOption));
}
Sock::Sock(sock_type _type, int port){
this->sock_addr.sin_family = AF_INET;
this->sock_addr.sin_port = htons(port);
this->sock_addr.sin_addr.s_addr = INADDR_ANY;
this->mode = 0;
this->type = _type;
if(this->type == udp){
this->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
}
else{
if(this->type == tcp){
this->sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
}
int iSetOption = 1;
setsockopt(this->sockfd, SOL_SOCKET, SO_REUSEADDR,(char*) &iSetOption, sizeof(iSetOption));
}
Sock::Sock(sock_type _type){
this->type = _type;
}
void Sock::Bind(){
if(bind(this->sockfd, (struct sockaddr *)&this->sock_addr, sizeof(this->sock_addr)) < 0)
{
std::cout << "Error on bind" << std::endl;
this->lasterror = errno;
}
}
void Sock::Listen(int backlog){
listen(this->sockfd, backlog);
}
void Sock::Connect(){
if(connect(this->sockfd, (struct sockaddr *)&this->sock_addr, sizeof(this->sock_addr))< 0){
std::cout << "Error on connect" << std::endl;
this->lasterror = errno;
}
}
Sock Sock::Accept(){
Sock tmp = Sock(this->type);
socklen_t socklength = sizeof(tmp.sock_addr);
tmp.sockfd = accept(this->sockfd, (struct sockaddr *) &tmp.sock_addr, &socklength);
if(tmp.sockfd < 0){
std::cout << "Error on accept" << std::endl;
std::cout << errno << std::endl;
this->lasterror = errno;
}
return tmp;
}
std::string Sock::Read(){
char buff[256];
for(int i = 0; i < 255; ++i)buff[i] = 0;
if(read(this->sockfd, buff, 255) < 0){
this->lasterror = errno;
return "";
}
std::string tmp = std::string(buff);
return tmp;
}
void Sock::Write(std::string writedata){
if(write(this->sockfd, writedata.c_str(), writedata.length()) < 0){
this->lasterror = errno;
}
}
void Sock::Close(){
close(this->sockfd);
}