Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions
exploringBB::SocketServer Class Reference

A class that encapsulates a server socket for network communication. More...

#include <SocketServer.h>

Public Member Functions

 SocketServer (int portNumber)
 
virtual int listen ()
 
virtual int send (std::string message)
 
virtual std::string receive (int size)
 
virtual ~SocketServer ()
 

Detailed Description

A class that encapsulates a server socket for network communication.

Constructor & Destructor Documentation

exploringBB::SocketServer::SocketServer ( int  portNumber)
34  {
35  this->socketfd = -1;
36  this->clientSocketfd = -1;
37  this->portNumber = portNumber;
38  this->clientConnected = false;
39 }
exploringBB::SocketServer::~SocketServer ( )
virtual
87  {
88  close(this->socketfd);
89  close(this->clientSocketfd);
90 }

Member Function Documentation

int exploringBB::SocketServer::listen ( )
virtual
41  {
42  this->socketfd = socket(AF_INET, SOCK_STREAM, 0);
43  if (this->socketfd < 0){
44  perror("Socket Server: error opening socket.\n");
45  return 1;
46  }
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");
53  return 1;
54  }
55  ::listen(this->socketfd, 5);
56  socklen_t clientLength = sizeof(this->clientAddress);
57  this->clientSocketfd = accept(this->socketfd,
58  (struct sockaddr *) &this->clientAddress,
59  &clientLength);
60  if (this->clientSocketfd < 0){
61  perror("Socket Server: Failed to bind the client socket properly.\n");
62  return 1;
63  }
64  return 0;
65 }
virtual int listen()
Definition: SocketServer.cpp:41
string exploringBB::SocketServer::receive ( int  size = 1024)
virtual
78  {
79  char readBuffer[size];
80  int n = read(this->clientSocketfd, readBuffer, sizeof(readBuffer));
81  if (n < 0){
82  perror("Socket Server: error reading from server socket.");
83  }
84  return string(readBuffer);
85 }
string read(string path, string filename)
Definition: util.cpp:57
int exploringBB::SocketServer::send ( std::string  message)
virtual
67  {
68  const char *writeBuffer = message.data();
69  int length = message.length();
70  int n = write(this->clientSocketfd, writeBuffer, length);
71  if (n < 0){
72  perror("Socket Server: error writing to server socket.");
73  return 1;
74  }
75  return 0;
76 }
int write(string path, string filename, string value)
Definition: util.cpp:40

The documentation for this class was generated from the following files: