A class that encapsulates a server socket for network communication.
More...
#include <SocketServer.h>
A class that encapsulates a server socket for network communication.
exploringBB::SocketServer::SocketServer |
( |
int |
portNumber | ) |
|
36 this->clientSocketfd = -1;
37 this->portNumber = portNumber;
38 this->clientConnected =
false;
exploringBB::SocketServer::~SocketServer |
( |
| ) |
|
|
virtual |
88 close(this->socketfd);
89 close(this->clientSocketfd);
int exploringBB::SocketServer::listen |
( |
| ) |
|
|
virtual |
42 this->socketfd = socket(AF_INET, SOCK_STREAM, 0);
43 if (this->socketfd < 0){
44 perror(
"Socket Server: error opening socket.\n");
47 bzero((
char *) &serverAddress,
sizeof(serverAddress));
48 serverAddress.sin_family = AF_INET;
49 serverAddress.sin_addr.s_addr = INADDR_ANY;
50 serverAddress.sin_port = htons(this->portNumber);
51 if (bind(socketfd, (
struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0){
52 perror(
"Socket Server: error on binding the socket.\n");
56 socklen_t clientLength =
sizeof(this->clientAddress);
57 this->clientSocketfd = accept(this->socketfd,
58 (
struct sockaddr *) &this->clientAddress,
60 if (this->clientSocketfd < 0){
61 perror(
"Socket Server: Failed to bind the client socket properly.\n");
virtual int listen()
Definition: SocketServer.cpp:41
string exploringBB::SocketServer::receive |
( |
int |
size = 1024 | ) |
|
|
virtual |
79 char readBuffer[size];
80 int n =
read(this->clientSocketfd, readBuffer,
sizeof(readBuffer));
82 perror(
"Socket Server: error reading from server socket.");
84 return string(readBuffer);
string read(string path, string filename)
Definition: util.cpp:57
int exploringBB::SocketServer::send |
( |
std::string |
message | ) |
|
|
virtual |
68 const char *writeBuffer = message.data();
69 int length = message.length();
70 int n =
write(this->clientSocketfd, writeBuffer, length);
72 perror(
"Socket Server: error writing to server socket.");
int write(string path, string filename, string value)
Definition: util.cpp:40
The documentation for this class was generated from the following files: