socket applications

lee lee_member at pathlink.com
Wed Feb 22 10:58:30 PST 2006


In article <dtgbki$2omn$1 at digitaldaemon.com>, BCS says...
>
>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
>

I managed to compile the segments above, and they look promising. I was
wondering if anyone could outline the steps needed to implement a socket
application that used RAW socket types. The documentation that came with my
version of the D compiler doesn't go into detail about their use. 
Thanks
Lee





More information about the Digitalmars-d mailing list