19 #include <netinet/in.h> 21 #include <sys/types.h> 22 #include <sys/socket.h> 29 #pragma comment(lib, "ws2_32.lib") 32 #define close closesocket 40 isockstream::isockstream(
int port)
44 if ( (portID = establish()) < 0)
45 mfem::out <<
"Server couldn't be established on port " 50 int isockstream::establish()
53 char myname[] =
"localhost";
55 struct sockaddr_in sa;
58 memset(&sa, 0,
sizeof(
struct sockaddr_in));
60 hp= gethostbyname(myname);
64 mfem::err <<
"isockstream::establish(): gethostbyname() failed!\n" 65 <<
"isockstream::establish(): gethostname() returned: '" 66 << myname <<
"'" << endl;
71 sa.sin_family= hp->h_addrtype;
72 sa.sin_port= htons(portnum);
74 if ((port = socket(AF_INET, SOCK_STREAM, 0)) < 0)
76 mfem::err <<
"isockstream::establish(): socket() failed!" << endl;
82 setsockopt(port, SOL_SOCKET, SO_REUSEADDR, (
char *)(&on),
sizeof(on));
84 if (bind(port,(
const sockaddr*)&sa,(
socklen_t)
sizeof(
struct sockaddr_in)) < 0)
86 mfem::err <<
"isockstream::establish(): bind() failed!" << endl;
97 int isockstream::read_data(
int s,
char *buf,
int n)
105 if ((br = recv(
s, buf, n - bcount, 0)) > 0)
119 void isockstream::receive(std::istringstream **in)
126 delete (*in), *in = NULL;
134 if ((socketID = accept(portID, NULL, NULL)) < 0)
136 mfem::out <<
"Server failed to accept connection." << endl;
141 if (recv(socketID, length, 32, 0) < 0)
152 Buf =
new char[size+1];
153 if (size != read_data(socketID, Buf, size))
155 mfem::out <<
"Not all the data has been read" << endl;
160 mfem::out <<
"Reading " << size <<
" bytes is successful" << endl;
166 (*in) =
new istringstream(Buf);
169 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...