14 #if defined(URG_WINDOWS_OS)
43 const char* data,
int size)
57 #if defined(URG_WINDOWS_OS)
59 ioctlsocket(cli->
sock_desc, FIONBIO, &flag);
69 enum { Connect_timeout_second = 2 };
71 struct timeval tv = { Connect_timeout_second, 0 };
72 #if defined(URG_WINDOWS_OS)
77 int sock_optval_size =
sizeof(sock_optval);
84 #if defined(URG_WINDOWS_OS)
86 static int is_initialized = 0;
87 WORD wVersionRequested = 0x0202;
90 if (!is_initialized) {
91 err = WSAStartup(wVersionRequested, &WSAData);
103 if ((cli->
sock_desc = (
int)socket(AF_INET, SOCK_STREAM, 0)) < 0) {
111 if (!strcmp(ip_str,
"localhost")) {
112 ip_str =
"127.0.0.1";
116 if ((cli->
server_addr.sin_addr.s_addr = inet_addr(ip_str)) == INADDR_NONE) {
120 #if defined(URG_WINDOWS_OS)
123 ioctlsocket(cli->
sock_desc, FIONBIO, &flag);
127 int error_number = WSAGetLastError();
128 if (error_number != WSAEWOULDBLOCK) {
137 ret = select((
int)cli->
sock_desc + 1, &rmask, &wmask, NULL, &tv);
149 flag = fcntl(cli->
sock_desc, F_GETFL, 0);
150 fcntl(cli->
sock_desc, F_SETFL, flag | O_NONBLOCK);
154 if (errno != EINPROGRESS) {
164 ret = select(cli->
sock_desc + 1, &rmask, &wmask, NULL, &tv);
171 if (getsockopt(cli->
sock_desc, SOL_SOCKET, SO_ERROR, (
int*)&sock_optval,
172 (socklen_t*)&sock_optval_size) != 0) {
178 if (sock_optval != 0) {
195 #if defined(URG_WINDOWS_OS)
207 char* userbuf,
int req_size,
int timeout)
212 int rem_size = req_size;
216 if (num_in_buf > 0) {
218 rem_size = req_size - n;
231 #if defined(URG_WINDOWS_OS)
233 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (
const char *)&no_timeout,
sizeof(
struct timeval));
234 n = recv(sock, tmpbuf,
BUFSIZE - num_in_buf, 0);
236 n = recv(sock, tmpbuf,
BUFSIZE - num_in_buf, MSG_DONTWAIT);
252 #if defined(URG_WINDOWS_OS)
253 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
254 (
const char *)&timeout,
sizeof(
struct timeval));
257 tv.tv_sec = timeout / 1000;
258 tv.tv_usec = (timeout % 1000) * 1000;
259 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(
struct timeval));
262 n = recv(sock, &userbuf[req_size-rem_size], rem_size, 0);
269 return (req_size - rem_size);
276 return send(cli->
sock_desc, buf, size, 0);
293 char* userbuf,
int buf_size,
int timeout)
303 for (; i < buf_size; ++i) {
309 if (ch ==
'\n' || ch ==
'\r') {
318 userbuf[buf_size - 1] =
'\0';
322 if (i == 0 && n <= 0) {