34 #include <visp3/core/vpConfig.h>
38 #if defined(WINRT) || defined(_WIN32)
39 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
40 #define _WINSOCK_DEPRECATED_NO_WARNINGS
45 #ifdef VISP_HAVE_FUNC_INET_NTOP
49 #include <visp3/core/vpServer.h>
50 #include <visp3/core/vpDebug.h>
52 #if defined(__APPLE__) && defined(__MACH__)
53 #include <TargetConditionals.h>
63 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
64 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
65 if (
emitter.socketFileDescriptorEmitter < 0)
67 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
70 vpERROR_TRACE(
"vpServer::vpServer(), cannot open socket.");
72 emitter.emitterAddress.sin_family = AF_INET;
73 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
74 emitter.emitterAddress.sin_port = 0;
76 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
77 port =
emitter.emitterAddress.sin_port;
88 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
89 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
90 if (
emitter.socketFileDescriptorEmitter < 0)
92 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
95 vpERROR_TRACE(
"vpServer::vpServer(const int &port_serv), cannot open socket.");
97 emitter.emitterAddress.sin_family = AF_INET;
98 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
99 emitter.emitterAddress.sin_port = htons((
unsigned short)port_serv);
101 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
112 : adress(), port(0), started(false), max_clients(10)
115 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
116 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
117 if (
emitter.socketFileDescriptorEmitter < 0)
119 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
122 vpERROR_TRACE(
"vpServer::vpServer(const std::string &adress_serv,const "
123 "int &port_serv), cannot open socket.");
125 emitter.emitterAddress.sin_family = AF_INET;
126 emitter.emitterAddress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
127 emitter.emitterAddress.sin_port = htons((
unsigned short)port_serv);
129 adress = adress_serv;
138 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
139 close(
emitter.socketFileDescriptorEmitter);
141 closesocket((
unsigned)
emitter.socketFileDescriptorEmitter);
145 #
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
148 closesocket((
unsigned)
receptor_list[i].socketFileDescriptorReceptor);
159 int serverStructLength =
sizeof(
emitter.emitterAddress);
160 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
161 int bindResult = bind(
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&
emitter.emitterAddress,
162 (
unsigned)serverStructLength);
164 int bindResult = bind((
unsigned)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&
emitter.emitterAddress,
168 if (bindResult < 0) {
169 std::cout <<
"Error id : " << bindResult << std::endl;
170 std::stringstream errorMessage;
171 errorMessage <<
"vpServer::vpServer(), cannot bind to port";
173 errorMessage << port;
174 errorMessage <<
" The port may be already used.";
176 vpERROR_TRACE(errorMessage.str().c_str());
183 if (
emitter.socketFileDescriptorEmitter > 0) {
186 setsockopt(
emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
189 std::cout <<
"Failed to set socket signal option" << std::endl;
194 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
195 listen(
emitter.socketFileDescriptorEmitter, (
int)max_clients);
197 listen((
unsigned)
emitter.socketFileDescriptorEmitter, (
int)max_clients);
200 std::cout <<
"Server ready" << std::endl;
221 #ifdef TARGET_OS_IPHONE
247 else if (value == 0) {
252 vpNetwork::vpReceptor client;
253 client.receptorAddressSize =
sizeof(client.receptorAddress);
254 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
255 client.socketFileDescriptorReceptor = accept(
256 emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&client.receptorAddress, &client.receptorAddressSize);
258 client.socketFileDescriptorReceptor =
259 accept((
unsigned int)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&client.receptorAddress,
260 &client.receptorAddressSize);
263 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
264 if ((client.socketFileDescriptorReceptor) == -1)
266 if ((client.socketFileDescriptorReceptor) == INVALID_SOCKET)
268 vpERROR_TRACE(
"vpServer::run(), accept()");
270 client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
271 printf(
"New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
280 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
281 ssize_t numbytes = recv(
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
283 int numbytes = recv((
unsigned int)
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
287 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
304 #elif !defined(VISP_BUILD_SHARED_LIBS)
306 void dummy_vpServer() { };
fd_set readFileDescriptor
std::vector< vpReceptor > receptor_list
void print(const char *id="")
virtual ~vpServer() VP_OVERRIDE
bool checkForConnections()