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

A class that encapsulates a socket client to be used for network communication. More...

#include <SocketClient.h>

Public Member Functions

 SocketClient (std::string serverName, int portNumber)
 
virtual int connectToServer ()
 
virtual int disconnectFromServer ()
 
virtual int send (std::string message)
 
virtual std::string receive (int size)
 
bool isClientConnected ()
 
virtual ~SocketClient ()
 

Detailed Description

A class that encapsulates a socket client to be used for network communication.

Constructor & Destructor Documentation

exploringBB::SocketClient::SocketClient ( std::string  serverName,
int  portNumber 
)
33  {
34  this->socketfd = -1;
35  this->server = NULL;
36  this->serverName = serverName;
37  this->portNumber = portNumber;
38  this->isConnected = false;
39 }
exploringBB::SocketClient::~SocketClient ( )
virtual
111  {
112  if (this->isConnected == true){
114  }
115 }
virtual int disconnectFromServer()
Definition: SocketClient.cpp:105

Member Function Documentation

int exploringBB::SocketClient::connectToServer ( )
virtual
41  {
42 
43  socketfd = socket(AF_INET, SOCK_STREAM, 0);
44  if (socketfd < 0){
45  perror("Socket Client: error opening socket.\n");
46  return 1;
47  }
48  server = gethostbyname(serverName.data());
49  if (server == NULL) {
50  perror("Socket Client: error - no such host.\n");
51  return 1;
52  }
53  bzero((char *) &serverAddress, sizeof(serverAddress));
54  serverAddress.sin_family = AF_INET;
55  bcopy((char *)server->h_addr,(char *)&serverAddress.sin_addr.s_addr, server->h_length);
56  serverAddress.sin_port = htons(portNumber);
57 
58  if (connect(socketfd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0){
59  perror("Socket Client: error connecting to the server.\n");
60  return 1;
61  }
62  this->isConnected = true;
63  return 0;
64 }
int exploringBB::SocketClient::disconnectFromServer ( )
virtual
105  {
106  this->isConnected = false;
107  close(this->socketfd);
108  return 0;
109 }
bool exploringBB::SocketClient::isClientConnected ( )
inline
57 { return this->isConnected; }
string exploringBB::SocketClient::receive ( int  size = 1024)
virtual
77  {
78  char readBuffer[size];
79  int n = read(this->socketfd, readBuffer, sizeof(readBuffer));
80  if (n < 0){
81  perror("Socket Client: error reading from socket");
82  }
83  return string(readBuffer);
84 }
string read(string path, string filename)
Definition: util.cpp:57
int exploringBB::SocketClient::send ( std::string  message)
virtual
66  {
67  const char *writeBuffer = message.data();
68  int length = message.length();
69  int n = write(this->socketfd, writeBuffer, length);
70  if (n < 0){
71  perror("Socket Client: error writing to socket");
72  return 1;
73  }
74  return 0;
75 }
int write(string path, string filename, string value)
Definition: util.cpp:40

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