21#include <sys/socket.h>
29#pragma comment(lib, "ws2_32.lib")
32#define close closesocket
44 if ( (portID = establish()) < 0)
45 mfem::out <<
"Server couldn't be established on port "
50int isockstream::establish()
53 char myname[] =
"localhost";
55 struct addrinfo hints, *res, *rp;
57 memset(&hints, 0,
sizeof(hints));
58 hints.ai_family = AF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
60 hints.ai_protocol = 0;
62 int s = getaddrinfo(myname, NULL, &hints, &res);
65 mfem::err <<
"isockstream::establish(): getaddrinfo() failed!\n"
66 <<
"isockstream::establish(): getaddrinfo() returned: '"
67 << myname <<
"'" << endl;
73 for (rp = res; rp != NULL; rp = rp->ai_next)
75 if ((sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0)
77 mfem::err <<
"isockstream::establish(): socket() failed!" << endl;
83 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (
char *)&on,
sizeof(on)) < 0)
85 mfem::err <<
"isockstream::establish(): setsockopt() failed!" << endl;
90 if (bind(sfd, (
const struct sockaddr *)rp->ai_addr, rp->ai_addrlen) < 0)
92 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) < 0)
95 mfem::err <<
"isockstream::establish(): bind() failed!" << endl;
116int isockstream::read_data(
int s,
char *buf,
int n)
124 if ((br = recv(
s, buf, n - bcount, 0)) > 0)
145 delete (*in), *in = NULL;
153 if ((socketID = accept(portID, NULL, NULL)) < 0)
155 mfem::out <<
"Server failed to accept connection." << endl;
160 if (recv(socketID, length, 32, 0) < 0)
171 Buf =
new char[size+1];
172 if (size != read_data(socketID, Buf, size))
174 mfem::out <<
"Not all the data has been read" << endl;
179 mfem::out <<
"Reading " << size <<
" bytes is successful" << endl;
185 (*in) =
new 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...