14 #define _CRT_SECURE_NO_WARNINGS
23 #include <arpa/inet.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #define closesocket (::close)
33 #pragma comment(lib, "ws2_32.lib")
41 int old_sd = socket_descriptor;
43 socket_descriptor = sd;
44 setg(NULL, NULL, NULL);
45 setp(obuf, obuf + buflen);
51 struct sockaddr_in sa;
55 setg(NULL, NULL, NULL);
56 setp(obuf, obuf + buflen);
58 hp = gethostbyname(hostname);
61 socket_descriptor = -3;
64 memset(&sa, 0,
sizeof(sa));
65 memcpy((
char *)&sa.sin_addr, hp->h_addr, hp->h_length);
66 sa.sin_family = hp->h_addrtype;
67 sa.sin_port = htons(port);
68 socket_descriptor = socket(hp->h_addrtype, SOCK_STREAM, 0);
69 if (socket_descriptor < 0)
78 if (setsockopt(socket_descriptor, SOL_SOCKET, SO_NOSIGPIPE,
79 (
char *)(&on),
sizeof(on)) < 0)
81 closesocket(socket_descriptor);
82 socket_descriptor = -2;
87 if (connect(socket_descriptor,
88 (
const struct sockaddr *)&sa,
sizeof(sa)) < 0)
90 closesocket(socket_descriptor);
91 socket_descriptor = -2;
102 int err = closesocket(socket_descriptor);
103 socket_descriptor = -1;
111 ssize_t bw, n = pptr() - pbase();
116 bw = send(socket_descriptor, pptr() - n, n, MSG_NOSIGNAL);
118 bw = send(socket_descriptor, pptr() - n, n, 0);
123 std::cout <<
"Error in send(): " << strerror(errno) << std::endl;
125 setp(pptr() - n, obuf + buflen);
131 setp(obuf, obuf + buflen);
138 ssize_t br = recv(socket_descriptor, ibuf, buflen, 0);
146 std::cout <<
"Error in recv(): " << strerror(errno) << std::endl;
149 setg(NULL, NULL, NULL);
150 return traits_type::eof();
152 setg(ibuf, ibuf, ibuf + br);
153 return traits_type::to_int_type(*ibuf);
160 return traits_type::eof();
162 if (traits_type::eq_int_type(c, traits_type::eof()))
164 return traits_type::not_eof(c);
166 *pptr() = traits_type::to_char_type(c);
175 const std::streamsize bn = egptr() - gptr();
178 traits_type::copy(__s, gptr(), __n);
182 traits_type::copy(__s, gptr(), bn);
183 setg(NULL, NULL, NULL);
184 std::streamsize remain = __n - bn;
185 char_type *end = __s + __n;
189 br = recv(socket_descriptor, end - remain, remain, 0);
195 std::cout <<
"Error in recv(): " << strerror(errno) << std::endl;
198 return (__n - remain);
209 if (pptr() + __n <= epptr())
211 traits_type::copy(pptr(), __s, __n);
220 std::streamsize remain = __n;
221 const char_type *end = __s + __n;
222 while (remain > buflen)
225 bw = send(socket_descriptor, end - remain, remain, MSG_NOSIGNAL);
227 bw = send(socket_descriptor, end - remain, remain, 0);
232 std::cout <<
"Error in send(): " << strerror(errno) << std::endl;
234 return (__n - remain);
240 traits_type::copy(pptr(), end - remain, remain);
249 listen_socket = socket(PF_INET, SOCK_STREAM, 0);
250 if (listen_socket < 0)
255 if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,
256 (
char *)(&on),
sizeof(on)) < 0)
258 closesocket(listen_socket);
262 struct sockaddr_in sa;
263 memset(&sa, 0,
sizeof(sa));
264 sa.sin_family = AF_INET;
265 sa.sin_port = htons(port);
266 sa.sin_addr.s_addr = INADDR_ANY;
267 if (bind(listen_socket, (
const struct sockaddr *)&sa,
sizeof(sa)))
269 closesocket(listen_socket);
273 const int backlog = 4;
274 if (listen(listen_socket, backlog) < 0)
276 closesocket(listen_socket);
288 int err = closesocket(listen_socket);
299 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)