CMSocket Class Reference

Comunication through sockets. More...

Public Member Functions

 CMSocket (unsigned int port, const char *ip, unsigned int buffer)
 ~CMSocket ()
void init ()
int serverTcp ()
int serverRead (char *data)
int serverWrite (char *data)
int clientTcp ()
int clientWrite (char *data)
int clientRead (char *data)

Private Attributes

int id
char * ip
unsigned int port
unsigned int buffer_size
socklen_t address_size
struct sockaddr_in struct_socket

Detailed Description

Header for easy comunication through sockets.

Author:
Miguel Ángel Vílchez López 2008 <sacra@openmatica.es>

Codigo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Codigo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Examples:

TestSocket.cpp.


Constructor & Destructor Documentation

CMSocket::CMSocket ( unsigned int  port,
const char *  ip = "localhost",
unsigned int  buffer = 1024 
)

Constructors.

Parameters:
[in] port The port of socket tcp.
[in] ip The ip of socket tcp.
[in] buffer The buffer size.

00051 {
00052   this->ip=(char*)malloc((strlen(ip)+1)*sizeof(char));
00053   strcpy(this->ip, ip);
00054   this->port=port;
00055   this->buffer_size=buffer;
00056   this->init();
00057 }

CMSocket::~CMSocket (  ) 

Deconstructor.

00060 {
00061 }


Member Function Documentation

int CMSocket::clientRead ( char *  data  ) 

Accept and read from a server.

Parameters:
data The variable to fill with the data received.

00155 {
00156   int id_accept;
00157   char* buffer=(char*)malloc((this->buffer_size+1)*sizeof(char));
00158 
00159   //this->debug.debug("Trying to accept new connections...");
00160   if((id_accept=accept(this->id,(struct sockaddr *)&this->struct_socket,&this->address_size))<0)
00161   {
00162   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00163     return -1;
00164   }
00165 
00166   //this->debug.debug("Trying to receive data from socket...");
00167   if (recv(id_accept, buffer, this->buffer_size, 0) == -1)
00168   {
00169   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00170     return -1;
00171   }
00172 
00173   strcpy(data,buffer);
00174 
00175   return 0;
00176 }

int CMSocket::clientTcp (  ) 

Connect to a server.

Returns:
Returns 0 if succesfull, -1 if not.
Examples:
TestSocket.cpp.

00133 {
00134   //this->debug.debug("Trying to connect to server through sockets...");
00135   if((connect(this->id,(struct sockaddr *)&struct_socket,this->address_size))==-1)
00136   {
00137   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00138     return -1;
00139   }
00140   return 0;
00141 }

int CMSocket::clientWrite ( char *  data  ) 

Send data to server.

Returns:
Returns 0 if succesfull, -1 if not.
Examples:
TestSocket.cpp.

00144 {
00145   //this->debug.debug("Trying to send data through socket...");
00146   if (send(this->id, data, strlen(data),0) == -1) 
00147   {
00148   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00149     return 1;
00150   }
00151   return 0;
00152 }

void CMSocket::init (  ) 

Methods commons to server and client.

00064 {
00065   //this->debug.debug("Trying to create the socket...");
00066   if((this->id = socket(PF_INET,SOCK_STREAM,0))<0)
00067   {
00068   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00069     exit(EXIT_FAILURE);
00070   }
00071   //Initialize and config the server data structure
00072   memset(&this->struct_socket,0,sizeof(this->struct_socket));
00073   this->struct_socket.sin_family=AF_INET;//type of protocol
00074   this->struct_socket.sin_port=htons(this->port);//port
00075   //ligar el socket a una direccion de red
00076   this->address_size=sizeof(this->struct_socket);
00077 }

int CMSocket::serverRead ( char *  data  ) 

Accept and read from a client.

Parameters:
data The variable to fill with the data received.
Examples:
TestSocket.cpp.

00098 {
00099   int id_accept;
00100   char* buffer=(char*)malloc((this->buffer_size+1)*sizeof(char));
00101   
00102   //this->debug.debug("Trying to accept new connections...");
00103   if((id_accept=accept(this->id,(struct sockaddr *)&this->struct_socket,&this->address_size))<0)
00104   {
00105   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00106     return -1;
00107   }
00108   
00109   //this->debug.debug("Trying to receive data from socket...");
00110   if (recv(id_accept, buffer, this->buffer_size, 0) == -1)
00111   {
00112   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00113     return -1;
00114   }
00115 
00116   strcpy(data,buffer);
00117   
00118   return 0;
00119 }

int CMSocket::serverTcp (  ) 

Create a new socket.

Returns:
Returns 0 if succesfull, -1 if not.
Examples:
TestSocket.cpp.

00080 {
00081   //this->debug.debug("Trying to bind the socket...");
00082   if((bind(this->id,(struct sockaddr *)&this->struct_socket,this->address_size))<0)
00083   {
00084   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00085     return -1;
00086   }
00087   
00088   //this->debug.debug("Trying to waiting for new connections...");
00089   if((listen(this->id,0))<0)
00090   {
00091   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00092     return -1;
00093   }
00094   return 0;
00095 }

int CMSocket::serverWrite ( char *  data  ) 

Send data to client.

Returns:
Returns 0 if succesfull, -1 if not.

00122 {
00123   //this->debug.debug("Trying to send data through socket...");
00124   if (send(this->id, data, strlen(data),0) == -1)
00125   {
00126   //  this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00127     return 1;
00128   }
00129   return 0;
00130 }


Field Documentation

socklen_t CMSocket::address_size [private]

The socket address size.

unsigned int CMSocket::buffer_size [private]

The buffer size.

int CMSocket::id [private]

The identifier of socket.

char* CMSocket::ip [private]

The ip of socket.

unsigned int CMSocket::port [private]

The TCP port.

struct sockaddr_in CMSocket::struct_socket [private]

The struct of the socket.

 All Data Structures Functions Variables
Generated on Mon Sep 12 07:59:09 2011 for API MokArdu by  doxygen 1.6.3