39 #include <visp3/core/vpServer.h> 42 #ifdef VISP_HAVE_FUNC_INET_NTOP 44 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 45 #include <TargetConditionals.h> 54 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
55 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 56 if (
emitter.socketFileDescriptorEmitter < 0)
58 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
63 emitter.emitterAddress.sin_family = AF_INET;
64 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
65 emitter.emitterAddress.sin_port = 0;
67 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
68 port =
emitter.emitterAddress.sin_port;
79 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
80 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 81 if (
emitter.socketFileDescriptorEmitter < 0)
83 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
86 vpERROR_TRACE(
"vpServer::vpServer(const int &port_serv), cannot open socket.");
88 emitter.emitterAddress.sin_family = AF_INET;
89 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
90 emitter.emitterAddress.sin_port = htons((
unsigned short)port_serv);
92 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
103 : adress(), port(0), started(false), max_clients(10)
106 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
107 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 108 if (
emitter.socketFileDescriptorEmitter < 0)
110 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
113 vpERROR_TRACE(
"vpServer::vpServer(const std::string &adress_serv,const " 114 "int &port_serv), cannot open socket.");
116 emitter.emitterAddress.sin_family = AF_INET;
117 emitter.emitterAddress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
118 emitter.emitterAddress.sin_port = htons((
unsigned short)port_serv);
120 adress = adress_serv;
129 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 130 close(
emitter.socketFileDescriptorEmitter);
132 closesocket((
unsigned)
emitter.socketFileDescriptorEmitter);
136 #
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
139 closesocket((
unsigned)
receptor_list[i].socketFileDescriptorReceptor);
150 int serverStructLength =
sizeof(
emitter.emitterAddress);
151 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 152 int bindResult = bind(
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&
emitter.emitterAddress,
153 (
unsigned)serverStructLength);
155 int bindResult = bind((
unsigned)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&
emitter.emitterAddress,
159 if (bindResult < 0) {
160 std::cout <<
"Error id : " << bindResult << std::endl;
161 std::string errorMessage(
"vpServer::vpServer(), cannot bind to port");
162 char posrtNumberString[32];
163 sprintf(posrtNumberString,
"%d", port);
165 errorMessage += posrtNumberString;
166 errorMessage +=
" The port may be already used.";
174 if (
emitter.socketFileDescriptorEmitter > 0) {
177 setsockopt(
emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
179 std::cout <<
"Failed to set socket signal option" << std::endl;
182 #endif // SO_NOSIGPIPE 184 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 185 listen(
emitter.socketFileDescriptorEmitter, (
int)max_clients);
187 listen((
unsigned)
emitter.socketFileDescriptorEmitter, (
int)max_clients);
190 std::cout <<
"Server ready" << std::endl;
236 }
else if (value == 0) {
240 vpNetwork::vpReceptor client;
241 client.receptorAddressSize =
sizeof(client.receptorAddress);
242 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 243 client.socketFileDescriptorReceptor = accept(
244 emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&client.receptorAddress, &client.receptorAddressSize);
246 client.socketFileDescriptorReceptor =
247 accept((
unsigned int)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&client.receptorAddress,
248 &client.receptorAddressSize);
251 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 252 if ((client.socketFileDescriptorReceptor) == -1)
254 if ((client.socketFileDescriptorReceptor) == INVALID_SOCKET)
258 client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
259 printf(
"New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
267 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 268 ssize_t numbytes = recv(
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
270 int numbytes = recv((
unsigned int)
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
274 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
291 #elif !defined(VISP_BUILD_SHARED_LIBS) 293 void dummy_vpServer(){};
fd_set readFileDescriptor
void print(const char *id="")
bool checkForConnections()
std::vector< vpReceptor > receptor_list