34 SOCKET data_socket = INVALID_SOCKET,
35 data_client_socket = INVALID_SOCKET;
46 #ifndef _WIN32 //Init server UNIX 47 signal(SIGPIPE, SIG_IGN);
49 #else //Init server Winsock (Windows) 50 WORD wVersionRequested;
54 wVersionRequested = MAKEWORD(2, 2);
56 err = WSAStartup(wVersionRequested, &wsaData);
58 printf(
"WSAStartup failed with error: %d\n", err);
71 #ifdef _WIN32 //Listen for data (Winsock) 72 n = recv(data_client_socket, buff, *len, 0);
73 #else //Listen for data (UNIX) 82 int m_len = 1 + 1 +
sizeof(int);
83 unsigned char *msg = (
unsigned char*) malloc(m_len + len);
84 memcpy(msg, &first, 1);
85 memcpy(msg + 1, &second, 1);
86 memcpy(msg + 2, &len,
sizeof(
int));
87 memcpy(msg + m_len, data, len);
89 n = send(data_client_socket, (
char*)msg, m_len + len, 0);
101 struct sockaddr_in childaddr;
103 childlen =
sizeof(childaddr);
105 printf(
"### Wait client\n");
106 #ifdef _WIN32 //Wait for connection (Winsock) 107 data_client_socket = accept(data_socket, NULL, NULL);
108 if (data_client_socket == INVALID_SOCKET) {
109 if(!
closing) printf(
"Error on accept() for data, exit data thread. %d\n", WSAGetLastError());
110 closesocket(data_socket);
113 #else //Wait for connection (UNIX) 114 data_child = accept(
s_data, (
struct sockaddr *)&childaddr, (
unsigned int *)&childlen);
117 if(!
closing) fprintf(stderr,
"Error on accept() for data, exit data thread.\n");
130 if ( data_socket != INVALID_SOCKET )
131 closesocket(data_socket);
140 struct addrinfo *result = NULL;
142 ZeroMemory(&si_type,
sizeof (si_type));
144 si_type.ai_family = AF_INET;
145 si_type.ai_socktype = SOCK_STREAM;
146 si_type.ai_protocol = IPPROTO_TCP;
147 si_type.ai_flags = AI_PASSIVE;
149 iResult = getaddrinfo(NULL, conf_port, &si_type, &result);
151 printf(
"Socket: getaddrinfo failed: %d\n", iResult);
156 *the_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
158 if (*the_socket == INVALID_SOCKET) {
159 printf(
"Socket: socket failed [%ld]\n", WSAGetLastError());
160 freeaddrinfo(result);
165 iResult = bind(*the_socket, result->ai_addr, (
int)result->ai_addrlen);
166 if (iResult == SOCKET_ERROR) {
167 printf(
"Socket: bind failed: %d\n", WSAGetLastError());
168 freeaddrinfo(result);
169 closesocket(*the_socket);
174 freeaddrinfo(result);
176 if ( listen(*the_socket, SOMAXCONN ) == SOCKET_ERROR ) {
177 printf(
"Socket: listen failed [%ld]\n", WSAGetLastError() );
178 closesocket(*the_socket);
188 if ( (*the_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
190 fprintf(stderr,
"Unable to create depth socket\n");
194 setsockopt(*the_socket, SOL_SOCKET, SO_REUSEADDR, (
const void *)&optval,
sizeof(optval));
196 memset((
char *) &si_type, 0,
sizeof(si_type));
198 si_type.sin_family = AF_INET;
199 si_type.sin_port = htons(conf_port);
200 si_type.sin_addr.s_addr = inet_addr(
conf_ip);
202 if ( bind(*the_socket, (
struct sockaddr *)&si_type,
203 sizeof(si_type)) < 0 )
205 fprintf(stderr,
"Error at bind() for depth\n");
208 if ( listen(*the_socket, 1) < 0 )
210 fprintf(stderr,
"Error on listen() for depth\n");
void freenect_network_wait()
callback freenect_network_connected
void freenect_network_read(char *buff, int *len)
void freenect_network_close()
struct sockaddr_in si_data
int freenect_network_initSocket(struct sockaddr_in si_type, int conf_port, int *the_socket)
int freenect_network_init(callback cb)
int freenect_network_sendMessage(int first, int second, unsigned char *data, int len)