38 #include <visp3/core/vpServer.h>
40 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
41 # include <TargetConditionals.h>
50 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
51 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
52 if (
emitter.socketFileDescriptorEmitter < 0)
54 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
59 emitter.emitterAddress.sin_family = AF_INET;
60 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
61 emitter.emitterAddress.sin_port = 0;
63 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
64 port =
emitter.emitterAddress.sin_port;
75 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
76 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
77 if (
emitter.socketFileDescriptorEmitter < 0)
79 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
82 vpERROR_TRACE(
"vpServer::vpServer(const int &port_serv), cannot open socket." );
84 emitter.emitterAddress.sin_family = AF_INET;
85 emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
86 emitter.emitterAddress.sin_port = htons( (
unsigned short)port_serv );
88 adress = inet_ntoa(
emitter.emitterAddress.sin_addr);
99 : adress(), port(0), started(false), max_clients(10)
102 emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
103 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
104 if (
emitter.socketFileDescriptorEmitter < 0)
106 if (
emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
109 vpERROR_TRACE(
"vpServer::vpServer(const std::string &adress_serv,const int &port_serv), cannot open socket." );
111 emitter.emitterAddress.sin_family = AF_INET;
112 emitter.emitterAddress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
113 emitter.emitterAddress.sin_port = htons( (
unsigned short)port_serv );
115 adress = adress_serv;
124 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
125 close(
emitter.socketFileDescriptorEmitter );
127 closesocket( (
unsigned)
emitter.socketFileDescriptorEmitter );
131 #
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
134 closesocket( (
unsigned)
receptor_list[i].socketFileDescriptorReceptor );
146 int serverStructLength =
sizeof(
emitter.emitterAddress);
147 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
148 int bindResult = bind(
emitter.socketFileDescriptorEmitter, (
struct sockaddr *) &
emitter.emitterAddress, (
unsigned)serverStructLength );
150 int bindResult = bind( (
unsigned)
emitter.socketFileDescriptorEmitter, (
struct sockaddr *) &
emitter.emitterAddress, serverStructLength );
156 std::cout <<
"Error id : " << bindResult << std::endl;
157 std::string errorMessage(
"vpServer::vpServer(), cannot bind to port" );
158 char posrtNumberString[32];
159 sprintf( posrtNumberString,
"%d", port );
161 errorMessage += posrtNumberString;
162 errorMessage +=
" The port may be already used.";
170 if (
emitter.socketFileDescriptorEmitter > 0) {
172 if (0 == setsockopt(
emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option,
sizeof(set_option))) {
174 std::cout <<
"Failed to set socket signal option" << std::endl;
177 #endif // SO_NOSIGPIPE
179 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
180 listen(
emitter.socketFileDescriptorEmitter, (
int)max_clients );
182 listen( (
unsigned)
emitter.socketFileDescriptorEmitter, (
int)max_clients );
185 std::cout <<
"Server ready" << std::endl;
235 vpNetwork::vpReceptor client;
236 client.receptorAddressSize =
sizeof(client.receptorAddress);
237 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
238 client.socketFileDescriptorReceptor = accept(
emitter.socketFileDescriptorEmitter,(
struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
240 client.socketFileDescriptorReceptor = accept((
unsigned int)
emitter.socketFileDescriptorEmitter,(
struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
243 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
244 if((client.socketFileDescriptorReceptor) == -1)
246 if((client.socketFileDescriptorReceptor) == INVALID_SOCKET)
250 client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
251 printf(
"New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
260 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
261 ssize_t numbytes = recv(
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
263 int numbytes = recv((
unsigned int)
receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
268 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