socket applications

BCS BCS_member at pathlink.com
Tue Feb 21 16:35:30 PST 2006


here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

         InternetAddress toAdd =  new InternetAddress(Address, port);

         Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

         toSoc.connect(toAdd);

         SocketStream st = new SocketStream(toSoc);

		// what to send
         char[] req = "Show me the money!!";

         st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
         while(true)
                 writef(st.getc);

         st.close();
}

---------
and here's a quick sever side
------------
import std.socketstream;
import std.socket;
import std.stream;
import std.stdio;


void main()
{
	int Port = GetPort();

	InternetAddress fromAdd = new InternetAddress(
			InternetAddress.ADDR_ANY, Port);

	Socket fromSoc = new Socket(
			AddressFamily.INET,
			SocketType.STREAM,
			ProtocolType.IP);
	Socket to;

	fromSoc.bind(fromAdd);

	writef("waiting...\n");
	fromSoc.listen(1);

	to = fromSoc.accept();
	writef("\nconnecting to %s\n", to.remoteAddress.toString);

	t1 = new Thread(cast(int function(void*))&Connect, cast(void*)to);
	t1.start();

	SocketStream st = new SocketStream(to);

	char[] buf;

	st.readBlock(buf.ptr, buf.length);

	// echo the input
	writef(buf, buf, \n);

	// end it back out
	st.writeBlock(buf.ptr, buf.length);
}


I haven't tested them but they come from somthing that did work

lee wrote:
> I'm stuck. I haven't been able to find a tutorial that outlines the
> creation of client programs using the phobos library. The following 
> program is my best guess as to how such a program could be written. 
> I'm trying to send data to a program similar to the listner app that 
> came with the d compiler. I'm still not certain about what steps are 
> to be takken when creating a network connection, and I don't know what
> functions to implement from the library. The code that follows below
> doesn't work, and I would like to know why. What's wrong with it? How
> could it be fixed? And, How could I achieve my goal with a different
> design?
> 
> Thanks for all the help so far;
> Lee



More information about the Digitalmars-d mailing list