Public Member Functions | |
| mxSocket () | |
| ~mxSocket () | |
| mxSocket (mx_socket nsock) | |
| mxSocket (const mxSocket &nsock) | |
| mxSocket & | operator= (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 () |
Definition at line 51 of file mxsocket.h.
| mx::mxSocket::mxSocket | ( | ) |
| mx::mxSocket::~mxSocket | ( | ) |
| mx::mxSocket::mxSocket | ( | mx_socket | nsock | ) |
creates object with already initalized socket
| nsock | socket file descriptor |
Definition at line 35 of file mxsocket.cpp.
| mx::mxSocket::mxSocket | ( | const mxSocket & | nsock | ) |
copy constructor
| nsock | init's this object with nsock |
Definition at line 41 of file mxsocket.cpp.
References connected, and current_socket.
| mx_socket mx::mxSocket::acceptNewSocket | ( | std::string & | ip | ) |
acceptNewSocket accepts new socket when listening
| ip | ip of accepted incoming 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
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
| ip | ip address | |
| port | outgoing port |
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
Definition at line 55 of file mxsocket.cpp.
| const int mx::mxSocket::getsocket | ( | ) | const [inline] |
| bool mx::mxSocket::isConnected | ( | ) | [inline] |
| bool mx::mxSocket::listenAt | ( | int | port | ) |
listen for connections at Port
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 }
overloaded operator =
| nsock | init's this object with nsock |
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
| 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
| 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 }
1.5.8