diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-01-14 20:10:59 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-01-14 20:10:59 +0000 |
commit | cad9cf61c1e1c594f69d7e3feb7413caad3bc047 (patch) | |
tree | 27eb2d35b38415c73fbd6952b61ddab1d2e9d664 | |
parent | 3ee5204eb254e914d695a37fede11c8599415fa9 (diff) | |
download | libbu++-cad9cf61c1e1c594f69d7e3feb7413caad3bc047.tar.gz libbu++-cad9cf61c1e1c594f69d7e3feb7413caad3bc047.tar.bz2 libbu++-cad9cf61c1e1c594f69d7e3feb7413caad3bc047.tar.xz libbu++-cad9cf61c1e1c594f69d7e3feb7413caad3bc047.zip |
Ok...this may work. We completely changed the way Bu::Socket resolves
addresses. Windows wouldn't work with the other way at all. But, fortunately,
this seems to work, it does more for us, and it looks pretty cute.
Diffstat (limited to '')
-rw-r--r-- | src/socket.cpp | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/src/socket.cpp b/src/socket.cpp index 77485aa..53b6522 100644 --- a/src/socket.cpp +++ b/src/socket.cpp | |||
@@ -40,7 +40,6 @@ Bu::Socket::Socket( int nSocket ) : | |||
40 | 40 | ||
41 | Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) | 41 | Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) |
42 | { | 42 | { |
43 | struct sockaddr_in xServerName; | ||
44 | bActive = false; | 43 | bActive = false; |
45 | 44 | ||
46 | /* Create the socket. */ | 45 | /* Create the socket. */ |
@@ -74,27 +73,33 @@ Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) | |||
74 | /* Connect to the server. */ | 73 | /* Connect to the server. */ |
75 | //printf("Resolving hostname (%s)...\n", sAddr ); | 74 | //printf("Resolving hostname (%s)...\n", sAddr ); |
76 | { | 75 | { |
77 | struct hostent *hostinfo; | 76 | struct addrinfo *pAddr = NULL; |
77 | struct addrinfo aiHints; | ||
78 | memset( &aiHints, 0, sizeof(addrinfo) ); | ||
79 | aiHints.ai_flags = AI_CANONNAME; | ||
80 | aiHints.ai_family = AF_INET; | ||
81 | aiHints.ai_socktype = SOCK_STREAM; | ||
82 | char ibuf[10]; | ||
83 | sprintf( ibuf, "%d", nPort ); | ||
78 | 84 | ||
79 | xServerName.sin_family = AF_INET; | 85 | if( DYNLOAD getaddrinfo( sAddr.getStr(), ibuf, &aiHints, &pAddr ) |
80 | xServerName.sin_port = DYNLOAD htons( nPort ); | 86 | != 0 ) |
81 | hostinfo = DYNLOAD gethostbyname( sAddr.getStr() ); | ||
82 | if (hostinfo == NULL) | ||
83 | { | 87 | { |
84 | throw Bu::SocketException("Couldn't resolve hostname.\n"); | 88 | throw Bu::SocketException("Couldn't resolve hostname %s (%s).\n", |
89 | sAddr.getStr(), strerror(errno)); | ||
85 | } | 90 | } |
86 | xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; | 91 | |
92 | DYNLOAD connect( | ||
93 | nSocket, | ||
94 | pAddr->ai_addr, | ||
95 | pAddr->ai_addrlen | ||
96 | ); | ||
97 | |||
98 | sAddress = pAddr->ai_canonname; | ||
99 | |||
100 | DYNLOAD freeaddrinfo( pAddr ); | ||
87 | } | 101 | } |
88 | 102 | ||
89 | //printf("Making actual connection..."); | ||
90 | //fflush( stdout ); | ||
91 | DYNLOAD connect( | ||
92 | nSocket, | ||
93 | (struct sockaddr *)&xServerName, | ||
94 | sizeof(xServerName) | ||
95 | ); | ||
96 | //printf("Connected.\n"); | ||
97 | |||
98 | bActive = true; | 103 | bActive = true; |
99 | 104 | ||
100 | if( nTimeout > 0 ) | 105 | if( nTimeout > 0 ) |