[comp.lang.c++] sockets

aw1r+@andrew.cmu.edu (Alfred Benjamin Woodard) (10/25/90)

Excuse me if there is something obvious I missed like this already being
built into stream.h . I am running cfont 2.0 on a unix machine and I
would like to use sockets for interprocess communication. Now from the
docs that came with the at&t compiler there seems to be no built in
support for sockets in the stream library and I was wondering if anyone
has gone to the trouble to implement one and if so where can I find it.
If no one has would it be advisable to build it up myself or should I
just use sockets in the same way that I do when I program in c? Anyone
have any tips on how to build it up if it does not already exist?

-ben


-ben

schemers@vela.acs.oakland.edu (Roland Schemers III) (10/25/90)

>support for sockets in the stream library and I was wondering if anyone
>has gone to the trouble to implement one and if so where can I find it.
>If no one has would it be advisable to build it up myself or should I

I am currently working on one and I have almost finished it. It allows
you to declare socket type objects. For example:

The server code is:

main()
{
InetAddress port(8000);		// open a local port
InetStream  server,client;	// server and client Internet Stream Sockets

server.socket_server(port);	// create a socket for server use (passive)

server.accept(client);		// wait for a client
client.write("hello!",7);	// write hello to client
				// destructors close up sockets
}


The client code would look like:

main() 
{
	char buffer[10];
	InetAddress hello_address("vela.acs.oakland.edu",8000);
	InetStream hello_server;

	hello_server.socket_client(hello_address);   // create the socket
	hello_server.read(buffer,10);		     // read from server
	cout << buffer << endl;

}

The InetAddress can also be a service, like:

InetAddress daytime("daytime","tcp");

The whole inheritance tree looks like this:

class Transport;
class Socket : public virtual Transport;
class UnixSocket : public virtual Socket;
class InetSocket : public virtual Socket;
class DECnetSocket : public virtual Socket;
class TransportStream : public virtual Transport;
class SocketStream : public virtual Socket ,public virtual TransportStream;
class UnixStream : public virtual UnixSocket , public virtual SocketStream;
class InetStream :public virtual InetSocket  , public virtual SocketStream;
class DECnetStream : public virtual DECnetSocket, public virtual SocketStream;
class TransportDatagram : public virtual Transport;
class SocketDatagram :   public virtual Socket,public virtual TransportDatagram;
class InetDatagram :public virtual InetSocket, public virtual SocketDatagram;

Where Unix is AF_UNIX, Inet is AF_INET, and DECnet is AF_DECnet.
The Stream suffix is for is for SOCK_STREAM, and Datagram for sockets 
of type SOCK_DGRAM. By deriving everything from Transport, I simply
open up any type of connection I want, DECnet, Internet, and pass
the object to a function that takes a Transport Object.

I have also written some classes that handle sending data transparently
using a scheme similiar to Apollos NDR format. So you can say:

// InetSocket sock;
// NDR_send ndr;

ndr << double(1.3) << char('a') << "hello" << int(2);
sock << ndr;

And then unpack it at the remote side with:

// InetSocket sock;
// NDR_receive ndr;
// double d; char c; char buff[32]; int i;

sock >> ndr;
ndr  >> d >> c >> buff >> i;

And it handles all the marshalling of the data. Converting between big
endian/little endian, and IEEE/VAX floating types.

After I little more testing I'll probably put it up for FTP on
vela.acs.oakland.edu. 

Roland

ps. Sorry, I rambled a little :-)
-- 
Roland J. Schemers III                              Systems Programmer 
schemers@vela.acs.oakland.edu (Ultrix)              Oakland University 
schemers@argo.acs.oakland.edu (VMS)                 Rochester, MI 48309-4401
"So what's your pointer?"                            (313)-370-4323