@@ -327,60 +327,6 @@ static void TessFreeAddrInfo(struct addrinfo* addr_info) {
327
327
}
328
328
329
329
330
- // Non-linux version of getaddrinfo()
331
- #if !defined(__linux__)
332
- static int GetAddrInfoNonLinux (const char * hostname, int port,
333
- struct addrinfo ** addr_info) {
334
- // Get the host data depending on the OS.
335
- struct sockaddr_in * address;
336
- *addr_info = new struct addrinfo ;
337
- memset (*addr_info, 0 , sizeof (struct addrinfo ));
338
- address = new struct sockaddr_in ;
339
- memset (address, 0 , sizeof (struct sockaddr_in ));
340
-
341
- (*addr_info)->ai_addr = (struct sockaddr *) address;
342
- (*addr_info)->ai_addrlen = sizeof (struct sockaddr );
343
- (*addr_info)->ai_family = AF_INET;
344
- (*addr_info)->ai_socktype = SOCK_STREAM;
345
-
346
- struct hostent *name;
347
- #ifdef _WIN32
348
- WSADATA wsaData;
349
- WSAStartup (MAKEWORD (1 , 1 ), &wsaData);
350
- name = gethostbyname (hostname);
351
- #else
352
- name = gethostbyname (hostname);
353
- #endif
354
-
355
- if (name == nullptr ) {
356
- TessFreeAddrInfo (*addr_info);
357
- *addr_info = nullptr ;
358
- return -1 ;
359
- }
360
-
361
- // Fill in the appropriate variables to be able to connect to the server.
362
- address->sin_family = name->h_addrtype ;
363
- memcpy (&address->sin_addr .s_addr , name->h_addr_list [0 ], name->h_length );
364
- address->sin_port = htons (port);
365
- return 0 ;
366
- }
367
- #endif
368
-
369
-
370
- // Platform independent version of getaddrinfo()
371
- // Given a hostname:port, produce an addrinfo struct
372
- static int GetAddrInfo (const char * hostname, int port,
373
- struct addrinfo ** address) {
374
- #if defined(__linux__)
375
- char port_str[40 ];
376
- snprintf (port_str, 40 , " %d" , port);
377
- return getaddrinfo (hostname, port_str, nullptr , address);
378
- #else
379
- return GetAddrInfoNonLinux (hostname, port, address);
380
- #endif
381
- }
382
-
383
-
384
330
// Set up a connection to a ScrollView on hostname:port.
385
331
SVNetwork::SVNetwork (const char * hostname, int port) {
386
332
msg_buffer_in_ = new char [kMaxMsgSize + 1 ];
@@ -390,10 +336,23 @@ SVNetwork::SVNetwork(const char* hostname, int port) {
390
336
buffer_ptr_ = nullptr ;
391
337
392
338
struct addrinfo *addr_info = nullptr ;
339
+ char port_str[40 ];
340
+ snprintf (port_str, 40 , " %d" , port);
341
+ #ifdef _WIN32
342
+ // Initialize Winsock
343
+ WSADATA wsaData;
344
+ int iResult = WSAStartup (MAKEWORD (2 , 2 ), &wsaData);
345
+ if (iResult != 0 ) {
346
+ std::cerr << " WSAStartup failed: " << iResult << std::endl;
347
+ }
348
+ #endif // _WIN32
393
349
394
- if (GetAddrInfo (hostname, port , &addr_info) != 0 ) {
350
+ if (getaddrinfo (hostname, port_str, nullptr , &addr_info) != 0 ) {
395
351
std::cerr << " Error resolving name for ScrollView host "
396
352
<< std::string (hostname) << " :" << port << std::endl;
353
+ #ifdef _WIN32
354
+ WSACleanup ();
355
+ #endif // _WIN32
397
356
}
398
357
399
358
stream_ = socket (addr_info->ai_family , addr_info->ai_socktype ,
@@ -442,7 +401,10 @@ SVNetwork::SVNetwork(const char* hostname, int port) {
442
401
}
443
402
}
444
403
}
445
- TessFreeAddrInfo (addr_info);
404
+ #ifdef _WIN32
405
+ // WSACleanup(); // This cause ScrollView windows is not displayed
406
+ #endif // _WIN32
407
+ freeaddrinfo (addr_info);
446
408
}
447
409
448
410
SVNetwork::~SVNetwork () {
0 commit comments