38 struct addrinfo hints = { 0 }, *ai, *cur_ai;
41 int listen_socket = 0;
46 int timeout = 100, listen_timeout = -1;
47 char hostname[1024],proto[1024],path[1024];
51 &port, path,
sizeof(path), uri);
52 if (strcmp(proto,
"tcp"))
54 if (port <= 0 || port >= 65536) {
63 timeout = strtol(buf,
NULL, 10);
66 listen_timeout = strtol(buf,
NULL, 10);
71 snprintf(portstr,
sizeof(portstr),
"%d", port);
80 "Failed to resolve hostname %s: %s\n",
89 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
96 struct pollfd lp = { fd, POLLIN, 0 };
97 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse));
98 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
108 ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
124 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
128 struct pollfd p = {fd, POLLOUT, 0};
137 if (ret !=
AVERROR(EINPROGRESS) &&
147 ret = poll(&p, 1, 100);
156 optlen =
sizeof(ret);
157 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
164 "TCP connection to %s:%d failed: %s\n",
165 hostname, port, errbuf);
175 if (cur_ai->ai_next) {
177 cur_ai = cur_ai->ai_next;
199 ret = recv(s->
fd, buf, size, 0);
213 ret = send(s->
fd, buf, size, 0);
230 return shutdown(s->
fd, how);