-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUdp.cpp
118 lines (110 loc) · 4.14 KB
/
Udp.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
/************************************************************
* File description: UDP implementation. *
* the class inherit from socket. *
* methods of udp socket type *
************************************************************/
#include "Udp.h"
/***********************************************************************
* function name: Udp *
* The Input: Boolean, true - if server, false if client and port number*
* The output: none *
* The Function operation: creating new Udp socket *
***********************************************************************/
Udp::Udp(bool isServers, int port_num, string ip) {
this->port_number = port_num;
this->isServer = isServers;
this->ip_address = ip;
}
/***********************************************************************
* function name: ~Udp *
* The Input: none *
* The output: none *
* The Function operation: default destructor *
***********************************************************************/
Udp::~Udp() {
// TODO Auto-generated destructor stub
}
/***********************************************************************
* function name: initialize *
* The Input: none *
* The output: int number representing the return status *
* The Function operation: initialize the Socket object by using *
* socket(), and bind() if server *
***********************************************************************/
int Udp::initialize() {
//creating new socket and getting his descriptor
this->socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
if (this->socketDescriptor < 0) {
return ERROR_SOCKET;
}
//if server
if (this->isServer) {
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 ERROR_BIND;
}
}
//return correct if there were no problems
return CORRECT;
}
/***********************************************************************
* function name: sendData *
* The Input: string representing the data to send *
* The output: int number representing the return status *
* The Function operation: sending the input data to the socket *
* who connect to this socket. check if send successfully *
***********************************************************************/
int Udp::sendData(string data, int a) {
//initialize the struct
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);
const char * datas = data.c_str();
int data_len = data.length() + 1;
//send
int sent_bytes = sendto(this->socketDescriptor,
datas, data_len, 0, (struct sockaddr *) &sin, sizeof(sin));
// cout << sent_bytes << endl;
//check if send successfully
if (sent_bytes < 0) {
return ERROR_SEND;
}
//return correct if there were no problems
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 check if *
* there were no error reciving and print *
***********************************************************************/
int Udp::reciveData(char* buffer, int size, int a) {
struct sockaddr_in to;
unsigned int to_len = sizeof(struct sockaddr_in);
//receive
int bytes = recvfrom(this->socketDescriptor,
buffer, size, 0, (struct sockaddr *) &to, &to_len);
//set the port number to the new one which we get with the data
this->port_number = ntohs(to.sin_port);
//check if receive successfully
// cout << bytes << endl;
if (bytes < 0) {
return -1;
}
//print the data
// cout<<buffer<<endl;
//return correct if there were no error
return bytes;
}
int Udp::acceptSock() {
return 0;
}