21#include <sys/socket.h>
29#pragma comment(lib, "ws2_32.lib")
32#define close closesocket
42 if ( (portID = establish()) < 0)
43 mfem::out <<
"Server couldn't be established on port "
44 << portnum << std::endl;
48int isockstream::establish()
51 char myname[] =
"localhost";
53 struct addrinfo hints, *res, *rp;
55 memset(&hints, 0,
sizeof(hints));
56 hints.ai_family = AF_UNSPEC;
57 hints.ai_socktype = SOCK_STREAM;
58 hints.ai_protocol = 0;
60 int s = getaddrinfo(myname, NULL, &hints, &res);
63 mfem::err <<
"isockstream::establish(): getaddrinfo() failed!\n"
64 <<
"isockstream::establish(): getaddrinfo() returned: '"
65 << myname <<
"'" << std::endl;
71 for (rp = res; rp != NULL; rp = rp->ai_next)
73 if ((sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0)
75 mfem::err <<
"isockstream::establish(): socket() failed!" << std::endl;
81 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (
char *)&on,
sizeof(on)) < 0)
83 mfem::err <<
"isockstream::establish(): setsockopt() failed!" << std::endl;
88 if (bind(sfd, (
const struct sockaddr *)rp->ai_addr, rp->ai_addrlen) < 0)
90 if (bind(sfd, rp->ai_addr,
static_cast<socklen_t>(rp->ai_addrlen)) < 0)
93 mfem::err <<
"isockstream::establish(): bind() failed!" << std::endl;
114int isockstream::read_data(
int s,
char *buf,
int n)
122 if ((br = recv(s, buf, n - bcount, 0)) > 0)
143 delete (*in), *in = NULL;
151 if ((socketID = accept(portID, NULL, NULL)) < 0)
153 mfem::out <<
"Server failed to accept connection." << std::endl;
158 if (recv(socketID, length, 32, 0) < 0)
169 Buf =
new char[size+1];
170 if (size != read_data(socketID, Buf, size))
172 mfem::out <<
"Not all the data has been read" << std::endl;
177 mfem::out <<
"Reading " << size <<
" bytes is successful" << std::endl;
183 (*in) =
new std::istringstream(Buf);
void receive(std::istringstream **in)
Start waiting for data and return it in an input stream.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...