39 #include <visp3/core/vpServer.h> 41 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 42 #include <TargetConditionals.h> 51 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
52 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 53 if (
emitter.socketFileDescriptorEmitter < 0)
55 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
60 emitter.emitterAddress.sin_family = AF_INET;
61 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
62 emitter.emitterAddress.sin_port = 0;
64 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
65 port =
emitter.emitterAddress.sin_port;
76 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
77 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 78 if (
emitter.socketFileDescriptorEmitter < 0)
80 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
83 vpERROR_TRACE(
"vpServer::vpServer(const int &port_serv), cannot open socket.");
85 emitter.emitterAddress.sin_family = AF_INET;
86 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
87 emitter.emitterAddress.sin_port = htons((
unsigned short)port_serv);
89 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
100 : adress(), port(0), started(false), max_clients(10)
103 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
104 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 105 if (
emitter.socketFileDescriptorEmitter < 0)
107 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
110 vpERROR_TRACE(
"vpServer::vpServer(const std::string &adress_serv,const " 111 "int &port_serv), cannot open socket.");
113 emitter.emitterAddress.sin_family = AF_INET;
114 emitter.emitterAddress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
115 emitter.emitterAddress.sin_port = htons((
unsigned short)port_serv);
117 adress = adress_serv;
126 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 127 close(
emitter.socketFileDescriptorEmitter);
129 closesocket((
unsigned)
emitter.socketFileDescriptorEmitter);
133 #
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
136 closesocket((
unsigned)
receptor_list[i].socketFileDescriptorReceptor);
147 int serverStructLength =
sizeof(
emitter.emitterAddress);
148 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 149 int bindResult = bind(
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&
emitter.emitterAddress,
150 (
unsigned)serverStructLength);
152 int bindResult = bind((
unsigned)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&
emitter.emitterAddress,
156 if (bindResult < 0) {
157 std::cout <<
"Error id : " << bindResult << std::endl;
158 std::string errorMessage(
"vpServer::vpServer(), cannot bind to port");
159 char posrtNumberString[32];
160 sprintf(posrtNumberString,
"%d", port);
162 errorMessage += posrtNumberString;
163 errorMessage +=
" The port may be already used.";
171 if (
emitter.socketFileDescriptorEmitter > 0) {
174 setsockopt(
emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
176 std::cout <<
"Failed to set socket signal option" << std::endl;
179 #endif // SO_NOSIGPIPE 181 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 182 listen(
emitter.socketFileDescriptorEmitter, (
int)max_clients);
184 listen((
unsigned)
emitter.socketFileDescriptorEmitter, (
int)max_clients);
187 std::cout <<
"Server ready" << std::endl;
233 }
else if (value == 0) {
237 vpNetwork::vpReceptor client;
238 client.receptorAddressSize =
sizeof(client.receptorAddress);
239 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 240 client.socketFileDescriptorReceptor = accept(
241 emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&client.receptorAddress, &client.receptorAddressSize);
243 client.socketFileDescriptorReceptor =
244 accept((
unsigned int)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *)&client.receptorAddress,
245 &client.receptorAddressSize);
248 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 249 if ((client.socketFileDescriptorReceptor) == -1)
251 if ((client.socketFileDescriptorReceptor) == INVALID_SOCKET)
255 client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
256 printf(
"New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
264 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 265 ssize_t numbytes = recv(
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
267 int numbytes = recv((
unsigned int)
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
271 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
fd_set readFileDescriptor
void print(const char *id="")
bool checkForConnections()
std::vector< vpReceptor > receptor_list