17 #include <arpa/inet.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #define closesocket (::close)
27 #pragma comment(lib, "ws2_32.lib")
35 int old_sd = socket_descriptor;
37 socket_descriptor = sd;
38 setg(NULL, NULL, NULL);
39 setp(obuf, obuf + buflen);
45 struct sockaddr_in sa;
49 setg(NULL, NULL, NULL);
50 setp(obuf, obuf + buflen);
52 hp = gethostbyname(hostname);
55 socket_descriptor = -3;
58 memset(&sa, 0,
sizeof(sa));
59 memcpy((
char *)&sa.sin_addr, hp->h_addr, hp->h_length);
60 sa.sin_family = hp->h_addrtype;
61 sa.sin_port = htons(port);
62 socket_descriptor = socket(hp->h_addrtype, SOCK_STREAM, 0);
63 if (socket_descriptor < 0)
65 if (connect(socket_descriptor,
66 (
const struct sockaddr *)&sa,
sizeof(sa)) < 0)
68 closesocket(socket_descriptor);
69 socket_descriptor = -2;
80 int err = closesocket(socket_descriptor);
81 socket_descriptor = -1;
89 ssize_t bw, n = pptr() - pbase();
94 bw = send(socket_descriptor, pptr() - n, n, MSG_NOSIGNAL);
96 bw = send(socket_descriptor, pptr() - n, n, 0);
100 setp(pptr() - n, obuf + buflen);
106 setp(obuf, obuf + buflen);
113 ssize_t br = recv(socket_descriptor, ibuf, buflen, 0);
118 setg(NULL, NULL, NULL);
119 return traits_type::eof();
121 setg(ibuf, ibuf, ibuf + br);
122 return traits_type::to_int_type(*ibuf);
128 return traits_type::eof();
129 if (traits_type::eq_int_type(c, traits_type::eof()))
130 return traits_type::not_eof(c);
131 *pptr() = traits_type::to_char_type(c);
140 const std::streamsize bn = egptr() - gptr();
143 traits_type::copy(__s, gptr(), __n);
147 traits_type::copy(__s, gptr(), bn);
148 setg(NULL, NULL, NULL);
149 std::streamsize remain = __n - bn;
150 char_type *end = __s + __n;
154 br = recv(socket_descriptor, end - remain, remain, 0);
156 return (__n - remain);
166 if (pptr() + __n <= epptr())
168 traits_type::copy(pptr(), __s, __n);
175 std::streamsize remain = __n;
176 const char_type *end = __s + __n;
177 while (remain > buflen)
180 bw = send(socket_descriptor, end - remain, remain, MSG_NOSIGNAL);
182 bw = send(socket_descriptor, end - remain, remain, 0);
185 return (__n - remain);
190 traits_type::copy(pptr(), end - remain, remain);
199 listen_socket = socket(PF_INET, SOCK_STREAM, 0);
200 if (listen_socket < 0)
205 if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,
206 (
char *)(&on),
sizeof(on)) < 0)
208 closesocket(listen_socket);
212 struct sockaddr_in sa;
213 memset(&sa, 0,
sizeof(sa));
214 sa.sin_family = AF_INET;
215 sa.sin_port = htons(port);
216 sa.sin_addr.s_addr = INADDR_ANY;
217 if (bind(listen_socket, (
const struct sockaddr *)&sa,
sizeof(sa)))
219 closesocket(listen_socket);
223 const int backlog = 4;
224 if (listen(listen_socket, backlog) < 0)
226 closesocket(listen_socket);
236 int err = closesocket(listen_socket);
245 int socketd =
::accept(listen_socket, NULL, NULL);
virtual std::streamsize xsgetn(char_type *__s, std::streamsize __n)
int accept(socketstream &sockstr)
virtual int_type underflow()
virtual int_type overflow(int_type c=traits_type::eof())
virtual std::streamsize xsputn(const char_type *__s, std::streamsize __n)
int open(const char hostname[], int port)