After some work on that sockio class I did some more reading on OOP and decided it might be best if it could be treated as an iostream. So I'm working on its rebirth using that idea ;)
I'm starting with the output and it works fine by itself but once I try to use it as an ostream or iostream it segfaults. It appears to be using the ostream's operator << instead of the classes.
I tried to make the inheritance private but doing so provided me with the following error:sockio.hQuote:
fields of `iostream' are inaccessible in `SocketInputOutput::SocketInOut' due to private inheritance
sockio.cppCode:#ifndef SOCKIO_H_
#define SOCKIO_H_
#define SOCKIO_NIX_
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <list>
#include <cerrno>
#include <sstream>
namespace SocketInputOutput {
class SocketInOut : private iostream{
int sock;
void SockRecv ();
public:
SocketInOut(int x) {
sock = x;
}
inline int getsocket() { return sock; }
inline void changesocket(int x) { sock = x; }
inline SocketInOut& operator << ( SocketInOut & (*f)(SocketInOut &) ){
return f(*this);
}
// Ouput Functions
template <typename T>
SocketInOut& operator <<(T x) {
ostringstream sout;
sout<<x;
return (*this)<<(sout.str());
}
SocketInOut& operator <<(string x);
};
inline SocketInOut &endl (SocketInOut &x){
return x<<"\r\n";
}
};
#endif
sockio_test.cppCode:#include <string>
#include <cstring>
#include <cctype>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sched.h>
#include "sockio.h"
using SocketInputOutput::SocketInOut;
using SocketInputOutput::endl;
SocketInOut & SocketInOut::operator <<(string s)
{
send (sock, s.c_str(), s.size(), 0);
return (*this);
}
the sockconn.cpp and sockconn.h just provide getconn() which waits for a connect and returns the sock (or throws an exception).Code:#include "sockio.h"
#include "sockconn.h"
using SocketInputOutput::SocketInOut;
using SocketInputOutput::endl;
void output (iostream &);
int main()
{
SocketInOut io(getconn());
output (io);
}
void output (iostream &x) {
x<<"This is a test"<<endl;
}
This is something I would like to figure out on my own as much as possible so please only nudges :)