(Phobos - SocketStream) Am I doing something wrong or is this a

Zane zane.sims at gmail.com
Wed Nov 4 03:02:03 PST 2009


Jesse Phillips Wrote:

> On Tue, 03 Nov 2009 20:05:17 -0500, Zane wrote:
> 
> > If I am to receive
> > these in arbitrarily sized chunks for concatenation, I don't see a
> > sensible way of constructing a loop.  Example?
> > 
> > Zane
> 
> You can use the number of bytes read to determine and use string slicing 
> to concatenation into the final array.

Thanks Jesse,

Can you (or someone) confirm that this program should work.  I added a loop with array slicing, but it does not seem to work for me.  The final output of "num" is 17593, and the file of that size is created, but it is not a valid gif image.  The code is below (note that this is assuming google still has their 'big-bird logo up :-P)

import std.stream;
import std.stdio;
import std.socket;
import std.socketstream;

import std.c.time;

int main()
{
	char[] line;
	ubyte[] data = new ubyte[17593];
	uint num = 0;

	TcpSocket socket = new TcpSocket(new InternetAddress("www.google.com", 80));

	socket.send("GET /logos/bigbird-hp.gif HTTP/1.0\r\n\r\n");

	SocketStream socketStream = new SocketStream(socket);
	
	while(!socketStream.eof)
	{
		line = socketStream.readLine();

		if (line=="")
			break;

		writef("%s\n", line);
	}
	
	num = socketStream.readBlock(data.ptr, 17593);
	writef("\n\nNum: %d\n", num);

	while(num < 17593)
	{
		num += socketStream.readBlock(data[(num-1)..length].ptr, data.length-num);
		writef("\n\nNum: %d\n", num);
	}

	socketStream.close;
	socket.close;

	File file = new File("logo.gif", FileMode.Out);
	file.write(data);
	file.close;

	return 0;
}

Thanks for everyone's help so far!



More information about the Digitalmars-d mailing list