@@ -29,7 +29,6 @@ function getIpAdress(addressInfo as addrInfo ptr) as string
2929 IP = space( 16 )
3030 inet_ntop( addressInfo->ai_family, @(cast(sockaddr_in ptr,addressInfo->ai_addr)->sin_addr), strptr(IP), len(IP) )
3131 end if
32-
3332 return trim(IP)
3433end function
3534
@@ -124,26 +123,38 @@ function fbNetworkClient.open(address as string, _port as uinteger, timeoutValue
124123 return false
125124 end if
126125
127- dim socketFlags as integer = fcntl(this._socket, F_GETFL, 0 )
128- fcntl(this._socket, F_SETFL, socketFlags or O_NONBLOCK)
126+ #ifdef __FB_WIN32__
127+ dim as integer blocking = 1
128+ ioctlsocket(this._socket, FIONBIO, @blocking)
129+ # else
130+ dim socketFlags as integer = fcntl(this._socket, F_GETFL, 0 )
131+ fcntl(this._socket, F_SETFL, socketFlags or O_NONBLOCK)
132+ # endif
129133 if (connect( this._socket, this._addressInfo->ai_addr, this._addressInfo->ai_addrlen) <> 0 ) then
130134 dim timeout as timeval
131135 dim as fd_set fdset
132136 timeout.tv_sec = timeoutValue
133137 timeout.tv_usec = 0
134138 FD_ZERO( @fdset)
135139 FD_SET_(this._socket, @fdset)
136- dim foo as integer = select_(this._socket+ 1 , 0 , @fdset, 0 , @timeout)
137- if (foo = 1 ) then
140+ if ( select_(this._socket+ 1 , 0 , @fdset, 0 , @timeout) = 1 ) then
138141 dim as integer socketError
139- dim as socklen_t length = sizeof(so_error)
140-
141- getsockopt(this._socket, SOL_SOCKET, SO_ERROR, @socketError, @length)
142+
143+ #ifdef __FB_WIN32__
144+ socketError = WSAGetLastError()
145+ # else
146+ dim as socklen_t length = sizeof(so_error)
147+ getsockopt(this._socket, SOL_SOCKET, SO_ERROR, @socketError, @length)
148+ # endif
142149 if (socketError) then
143150 this.errorHandler(net_undefined)
144151 return false
145152 end if
146- fcntl(this._socket, F_SETFL, socketFlags AND NOT O_NONBLOCK)
153+ #ifdef __FB_WIN32__
154+ blocking = 0 : ioctlsocket(this._socket, FIONBIO, @blocking)
155+ # else
156+ fcntl(this._socket, F_SETFL, socketFlags or O_NONBLOCK)
157+ # endif
147158 else
148159 this.errorHandler(net_timeout)
149160 return false
@@ -160,12 +171,17 @@ function fbNetworkClient.open(address as string, _port as uinteger, timeoutValue
160171 do
161172 messageLength = recv( this._socket, recvBuffer, fbNetwork.RECVBUFFLEN, 0 )
162173 if ( messageLength <= 0 ) then
174+ closesocket(this._socket)
175+ freeaddrinfo(this._addressInfo)
176+ this._socket = 0
177+ this._addressInfo = 0
163178 this.close()
164179 return true
165180 end if
166181 recvbuffer[messageLength] = 0
167182 this.onMessage(recvbuffer)
168183 loop
184+
169185
170186 return true
171187end function
0 commit comments