-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnection.cpp
76 lines (64 loc) · 1.75 KB
/
Connection.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
#include "Connection.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string>
Connection::Connection() {
int PORT;
std::cout << "What port number? ";
std::cin >> PORT;
int tempSockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
tempSockfd = socket(AF_INET, SOCK_STREAM, 0);
if (tempSockfd < 0)
std::cout << "ERROR opening socket";
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
if (bind(tempSockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
std::cout << "ERROR on binding";
listen(tempSockfd,5);
clilen = sizeof(cli_addr);
std::cout << "Waiting for connection\n";
socketFd = accept(tempSockfd,
(struct sockaddr *) &cli_addr,
&clilen);
std::cout << "Recieved connection\n";
if (socketFd < 0)
std::cout << "ERROR on accept";
}
Connection::~Connection() {
disconnect();
}
double Connection::getReward() {
std::string currentLine = getString();
std::cout << "lineL " << currentLine << "\n";
while(currentLine.compare("4") != 0) {
std::cout << "line: " << currentLine << "\n";
}
return atof(getString().c_str());
}
void Connection::disconnect() {
close(socketFd);
}
std::string Connection::getString() {
std::string newString = "";
char buffer;
// Keep reading char by char until a newline
while(true) {
read(socketFd, &buffer, 1);
std::cout << buffer;
if (buffer == '\n') break;
else newString += buffer;
}
return newString;
}