-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTcp.cpp
151 lines (141 loc) · 5.28 KB
/
Tcp.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/************************************************************
* File description: TCP implementation. *
* the class inherit from socket. *
* methods of tcp socket type *
************************************************************/
#include "Tcp.h"
/***********************************************************************
* function name: Tcp *
* The Input: Boolean, true - if server, false if client and port number*
* The output: none *
* The Function operation: creating new Tcp. initialize if server and *
* port_num by the input *
***********************************************************************/
Tcp::Tcp(bool isServers, int port_num, string ip) {
this->ip_address = ip;
this->descriptorCommunicateClient[0] = 0;
this->port_number = port_num;
this->isServer = isServers;
this->upto = 0;
}
/***********************************************************************
* function name: ~Tcp *
* The Input: none *
* The output: none *
* The Function operation: default destructor *
***********************************************************************/
Tcp::~Tcp() {
for (int i = 0; i < freeSock.size(); i++) {
delete freeSock[i];
}
for (int i = 0; i < freeAddr.size(); i++) {
delete freeAddr[i];
}
}
/***********************************************************************
* function name: initialize *
* The Input: none *
* The output: int number representing the return status *
* The Function operation: initialize the Socket object by getting *
* socket descriptor. bind and accept for servers or connect for clients*
***********************************************************************/
int Tcp::initialize() {
//getting a socket descriptor and check if legal
this->socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (this->socketDescriptor < 0) {
//return an error represent error at this method
return ERROR_SOCKET;
}
//if server
if (this->isServer) {
//initialize the struct
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(this->port_number);
//bind
if (bind(this->socketDescriptor,
(struct sockaddr *) &sin, sizeof(sin)) < 0) {
//return an error represent error at this method
return ERROR_BIND;
}
//listen
if (listen(this->socketDescriptor, this->backLog) < 0) {
//return an error represent error at this method
return ERROR_LISTEN;
}
//if client
} else {
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr((this->ip_address).c_str());
sin.sin_port = htons(this->port_number);
if (connect(this->socketDescriptor,
(struct sockaddr *) &sin, sizeof(sin)) < 0) {
//return an error represent error at this method
return ERROR_CONNECT;
}
}
//return correct if there were no problem
return CORRECT;
}
/***********************************************************************
* function name: sendData *
* The Input: string data to send *
* The output: int number representing the return status *
* The Function operation: sending the required data, using his length *
* and the socket descroptor *
***********************************************************************/
int Tcp::sendData(string data, int id) {
int data_len = data.length();
const char * datas = data.c_str();
int sent_bytes = send(this->isServer ? this->descriptorCommunicateClient[id]
: this->socketDescriptor, datas, data_len, 0);
if (sent_bytes < 0) {
//return an error represent error at this method
return ERROR_SEND;
}
//return correct if there were no problem
return CORRECT;
}
/***********************************************************************
* function name: recive ` *
* The Input: none *
* The output: int number representing the return status *
* The Function operation: getting data from the other socket to, *
* enter it to the buffer and print the data *
***********************************************************************/
int Tcp::reciveData(char* buffer, int size, int id) {
int read_bytes = recv(this->isServer ? this->descriptorCommunicateClient[id]
: this->socketDescriptor, buffer, size, 0);
//checking the errors
if (read_bytes == 0) {
return CONNECTION_CLOSED;
}
else if (read_bytes < 0) {
//return an error represent error at this method
return ERROR_RECIVE;
} else {
//prinrting the massege
// cout<<buffer<<endl;
}
//return correct if there were no problem
return read_bytes;
}
int Tcp::acceptSock() {
//acceptSock
struct sockaddr_in * client_sin = new struct sockaddr_in;
unsigned int * addr_len = new unsigned int;
freeSock.push_back(client_sin);
freeAddr.push_back(addr_len);
*addr_len = sizeof(*client_sin);
this->descriptorCommunicateClient[upto] = accept(this->socketDescriptor,
(struct sockaddr *) client_sin, addr_len);
if (this->descriptorCommunicateClient[upto] < 0) {
//return an error represent error at this method
return ERROR_ACCEPT;
}
upto++;
}