mx::mxSocket Class Reference

List of all members.

Public Member Functions

 mxSocket ()
 ~mxSocket ()
 mxSocket (mx_socket nsock)
 mxSocket (const mxSocket &nsock)
mxSocketoperator= (const mxSocket &nsock)
bool createSocket ()
bool isConnected ()
bool connectTo (std::string ip, unsigned int port)
bool listenAt (int port)
mx_socket acceptNewSocket (std::string &ip)
mx_socket acceptsocket ()
ssize_t Read (void *data, size_t size)
ssize_t Write (void *data, size_t size)
const int getsocket () const
int closeSocket ()


Detailed Description

socket class

Definition at line 51 of file mxsocket.h.


Constructor & Destructor Documentation

mx::mxSocket::mxSocket (  ) 

default constructor creates object

Definition at line 24 of file mxsocket.cpp.

00025         {
00026                 current_socket = 0;
00027                 connected = false;
00028         }

mx::mxSocket::~mxSocket (  ) 

destructor

Definition at line 30 of file mxsocket.cpp.

00031         {
00032 
00033         }

mx::mxSocket::mxSocket ( mx_socket  nsock  ) 

creates object with already initalized socket

Parameters:
nsock socket file descriptor

Definition at line 35 of file mxsocket.cpp.

00036         {
00037                 current_socket = nsock;
00038                 connected = true;
00039         }

mx::mxSocket::mxSocket ( const mxSocket nsock  ) 

copy constructor

Parameters:
nsock init's this object with nsock

Definition at line 41 of file mxsocket.cpp.

References connected, and current_socket.

00042         {
00043                 current_socket = nsock.current_socket;
00044                 connected = nsock.connected;
00045         }


Member Function Documentation

mx_socket mx::mxSocket::acceptNewSocket ( std::string &  ip  ) 

acceptNewSocket accepts new socket when listening

Parameters:
ip ip of accepted incoming connection
Returns:
returns file descriptor of established connection

Definition at line 81 of file mxsocket.cpp.

00082         {
00083 #ifdef _WIN32
00084                 int caddy_len;
00085 #else
00086                 socklen_t caddy_len;
00087 #endif
00088                 static struct sockaddr_in caddy;
00089                 int s;
00090                 s = accept(current_socket, (struct sockaddr *)&caddy, &caddy_len);
00091                 ip = std::string(inet_ntoa(caddy.sin_addr));
00092                 connected = true;
00093                 return s;
00094         }

mx_socket mx::mxSocket::acceptsocket (  ) 

acceptsocket accepts a incoming connection

Returns:
returns file descriptor for incoming connection

Definition at line 96 of file mxsocket.cpp.

00097         {
00098 #ifdef _WIN32
00099                 int caddy_len;
00100 #else
00101                 socklen_t caddy_len;
00102 #endif
00103 
00104                 mx_socket SOCK;
00105                 struct sockaddr_in  caddy;
00106                 SOCK = accept(current_socket, (struct sockaddr *)&caddy, &caddy_len);
00107                 connected = true;
00108                 return SOCK;
00109         }

int mx::mxSocket::closeSocket (  ) 

close the socket

Definition at line 184 of file mxsocket.cpp.

00185         {
00186 
00187 #ifdef _WIN32
00188                 return closesocket(current_socket);
00189 #else
00190                 return close(current_socket);
00191 #endif
00192                 return 1;
00193 
00194         }

bool mx::mxSocket::connectTo ( std::string  ip,
unsigned int  port 
)

connectTo connects to ip address and port

Parameters:
ip ip address
port outgoing port
Returns:
true on success

Definition at line 112 of file mxsocket.cpp.

00113         {
00114                 struct sockaddr_in saddy;
00115                 saddy.sin_port = htons(port);
00116                 saddy.sin_addr.s_addr = inet_addr(where.c_str());
00117                 saddy.sin_family = AF_INET;
00118 
00119                 if(connect(current_socket, (const sockaddr*)&saddy, sizeof(sockaddr_in)) == -1) {
00120 
00121                         std::cout << "error: could not connect: \n";
00122                         return false;
00123                 }
00124 
00125                 connected = true;
00126                 return true;
00127         }

bool mx::mxSocket::createSocket (  ) 

createSocket, creates the socket

Returns:
true on success

Definition at line 55 of file mxsocket.cpp.

00056         {
00057 
00058                 current_socket = socket(AF_INET,SOCK_STREAM,0);
00059                 return true;
00060         }

const int mx::mxSocket::getsocket (  )  const [inline]

getsocket return current socket file descriptor

Definition at line 117 of file mxsocket.h.

00117 { return current_socket; }

bool mx::mxSocket::isConnected (  )  [inline]

isConnected

Returns:
true if connected

Definition at line 81 of file mxsocket.h.

00081 { return connected; }

bool mx::mxSocket::listenAt ( int  port  ) 

listen for connections at Port

Returns:
true if operations successful

Definition at line 62 of file mxsocket.cpp.

00063         {
00064 
00065                 static struct sockaddr_in addy;
00066                 memset((char*)&addy,0, sizeof(addy));
00067                 addy.sin_port = htons(port);
00068                 addy.sin_family = AF_INET;
00069                 addy.sin_addr.s_addr = INADDR_ANY;
00070                 int yup = 1;
00071 
00072                 setsockopt(current_socket, SOL_SOCKET,SO_REUSEADDR,(const char*)&yup,sizeof(yup));
00073 
00074                 bind(current_socket, (struct sockaddr *)&addy, sizeof(addy));
00075                 listen(current_socket,5);
00076 
00077                 return true;
00078         }

mxSocket & mx::mxSocket::operator= ( const mxSocket nsock  ) 

overloaded operator =

Parameters:
nsock init's this object with nsock
Returns:
this object

Definition at line 47 of file mxsocket.cpp.

References connected, and current_socket.

00048         {
00049 
00050                 current_socket = sock.current_socket;
00051                 connected = sock.connected;
00052                 return *this;
00053         }

ssize_t mx::mxSocket::Read ( void *  data,
size_t  size 
)

Read all reads bytes of size until all have been read

Parameters:
data pointer to location to hold bytes
size how many bytes to read

Definition at line 130 of file mxsocket.cpp.

00131         {
00132 
00133                 size_t len = size;
00134                 size_t total = 0;
00135                 int cur_;
00136                 char *cdata = (char*)data;
00137 
00138                 while ( (len != 0) && ( (cur_ = recv(current_socket, cdata, len, 0) ) != 0) )
00139                 {
00140 
00141                         if(cur_ == -1) {
00142 
00143 
00144                                 std::cerr << "mxsocket: error on read\n";
00145                                 return -1;
00146 
00147                         }
00148                         len -= cur_;
00149                         cdata += cur_;
00150                         total += cur_;
00151                 }
00152 
00153                 return total;
00154 
00155         }

ssize_t mx::mxSocket::Write ( void *  data,
size_t  size 
)

Write all reads bytes of size until all have been written

Parameters:
data pointer to location of bytes
size how many bytes to write

Definition at line 157 of file mxsocket.cpp.

00158         {
00159 
00160                 size_t len = size;
00161                 size_t total = 0;
00162                 int cur_;
00163                 char *cdata = (char*)data;
00164 
00165                 while ( (len != 0) && (( cur_ = send(current_socket, cdata, len, 0) ) != 0) )
00166                 {
00167 
00168                         if(cur_ == -1) {
00169 
00170 
00171                                 std::cerr << "mxsocket: error on write\n";
00172                                 return -1;
00173 
00174                         }
00175                         len -= cur_;
00176                         cdata += cur_;
00177                         total += cur_;
00178                 }
00179 
00180                 return total;
00181         }


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

Generated on Wed Jun 10 14:52:02 2009 for libmx by  doxygen 1.5.8