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) {
int tcpclient_open(urg_tcpclient_t *cli, const char *ip_str, int port_num)
constructor of tcp client module
void tcpclient_close(urg_tcpclient_t *cli)
destructor of tcp client module
int ring_write(ring_buffer_t *ring, const char *data, int size)
データの格納
static int tcpclient_buffer_write(urg_tcpclient_t *cli, const char *data, int size)
static int tcpclient_buffer_data_num(urg_tcpclient_t *cli)
int tcpclient_readline(urg_tcpclient_t *cli, char *userbuf, int buf_size, int timeout)
read one line from socket.
int ring_read(ring_buffer_t *ring, char *buffer, int size)
データの取り出し
int tcpclient_read(urg_tcpclient_t *cli, char *userbuf, int req_size, int timeout)
read from socket.
struct sockaddr_in server_addr
int tcpclient_write(urg_tcpclient_t *cli, const char *buf, int size)
write to socket.
static void tcpclient_buffer_init(urg_tcpclient_t *cli)
int tcpclient_error(urg_tcpclient_t *cli, char *error_message, int max_size)
void ring_initialize(ring_buffer_t *ring, char *buffer, const int shift_length)
初期化
int ring_size(const ring_buffer_t *ring)
格納データ数を返す
static void set_block_mode(urg_tcpclient_t *cli)
TCP/IP read/write functions.
static int tcpclient_buffer_read(urg_tcpclient_t *cli, char *data, int size)