19 #include <netinet/in.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
28 #define close closesocket
30 #pragma comment(lib, "ws2_32.lib")
38 isockstream::isockstream(
int port)
42 if ( (portID = establish()) < 0)
43 mfem::out <<
"Server couldn't be established on port "
48 int isockstream::establish()
51 char myname[] =
"localhost";
53 struct sockaddr_in sa;
56 memset(&sa, 0,
sizeof(
struct sockaddr_in));
58 hp= gethostbyname(myname);
62 mfem::err <<
"isockstream::establish(): gethostbyname() failed!\n"
63 <<
"isockstream::establish(): gethostname() returned: '"
64 << myname <<
"'" << endl;
69 sa.sin_family= hp->h_addrtype;
70 sa.sin_port= htons(portnum);
72 if ((port = socket(AF_INET, SOCK_STREAM, 0)) < 0)
74 mfem::err <<
"isockstream::establish(): socket() failed!" << endl;
80 setsockopt(port, SOL_SOCKET, SO_REUSEADDR, (
char *)(&on),
sizeof(on));
82 if (bind(port,(
const sockaddr*)&sa,(
socklen_t)
sizeof(
struct sockaddr_in)) < 0)
84 mfem::err <<
"isockstream::establish(): bind() failed!" << endl;
95 int isockstream::read_data(
int s,
char *buf,
int n)
103 if ((br = recv(s, buf, n - bcount, 0)) > 0)
117 void isockstream::receive(std::istringstream **in)
124 delete (*in), *in = NULL;
132 if ((socketID = accept(portID, NULL, NULL)) < 0)
134 mfem::out <<
"Server failed to accept connection." << endl;
139 if (recv(socketID, length, 32, 0) < 0)
150 Buf =
new char[size+1];
151 if (size != read_data(socketID, Buf, size))
153 mfem::out <<
"Not all the data has been read" << endl;
158 mfem::out <<
"Reading " << size <<
" bytes is successful" << endl;
164 (*in) =
new istringstream(Buf);
167 isockstream::~isockstream()
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...