-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.cpp
126 lines (95 loc) · 2.72 KB
/
socket.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
#include <netdb.h>
#include "socket.h"
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <errno.h>
Socket::Socket() {
}
Socket::~Socket() {
close(sock);
exit(1);
}
bool Socket::initializeSocket(char *_myAddr=NULL, int _myPort = 0) {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
std::cerr << "Socket failed\n";
return false;
}
myPort = _myPort;
myAddr.sin_family = AF_INET;
myAddr.sin_port = htons((int) myPort);
myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (const sockaddr *) &myAddr, sizeof(struct sockaddr_in)) != 0) {
std::cerr << "Bind failed\n";
return false;
}
return true;
}
bool initializeClient(char* _peerAddr,int _peerPort)
{
sock = socket(A1F_INET,SOCK_DGRAM,0);
if (sock < 0)
{
std::cerr << "Socket failed\n";
return false;
}
sockaddr_in *clientAddr
clientAddr->sin_family = AF_INET;
clientAddr->sin_port = htons(2000);
clientAddr-> sin_addr.s_addr = htonl("localhost");
if(bind(sock,(sockaddr*) &clientAddr,sizeof(struct sockaddr_in)) != 0)
{
std::cerr << "Bind failed\n";
return false;
}
// peer address to send to
peerAddr->sin_family = AF_INET;
peerAddr->sin_port = htons(_peerPort);
peerAddr-> sin_addr.s_addr = htonl(_peerAddr);
return true;
}
bool initializeServer(char* _myAddr,int _myPort)
{
sock = socket(AF_INET,SOCK_DGRAM,0);
if (sock < 0)
{
std::cerr << "Socket failed\n";
return false;
}
sockaddr_in *serverAddr
serverAddr->sin_family = AF_INET;
serverAddr->sin_port = htons(_myPort);
serverAddr-> sin_addr.s_addr = htonl("localhost");
if(bind(sock,(sockaddr*) &serverAddr,sizeof(sockaddr_in)) != 0)
{
std::cerr << "Bind failed\n";
return false;
}
return true;
}
int Socket::writeToSocket(char *message) {
// strlen(parameters) + 1 for the NULL character at the end
int bytes_sent = sendto(sock, message, strlen(message) + 1, 0, (sockaddr *) &peerAddr, sizeof(peerAddr));
if (bytes_sent < 0) {
std::cerr << "Sending failed\n";
std::cerr << strerror(errno) << std::endl;
}
return bytes_sent;
}
int Socket::readFromSocket(char *message) {
peerAddr.sin_family = AF_INET;
socklen_t slength = sizeof(struct sockaddr_in);
int bytes_read = recvfrom(sock, message, strlen(message) + 1, 0, (sockaddr *) &peerAddr, &slength);
if (bytes_read < 0) {
std::cerr << "Receiving failed\n";
std::cerr << strerror(errno) << std::endl;
}
return bytes_read;
}
int Socket::getMyPort() {
return myPort;
}
int Socket::getSocketHandler() {
return sock;
}