Socket sample (and others) in DMD/samples

Sam Hu samhudotsamhu at gmail.com
Mon Sep 28 18:51:12 PDT 2009


I was wondering why some of the samples accompany with DMD2032 can not get compiled?If they are not updated together with DMD,why bother include them?It may confuses ppl who are new to D  the first time to give D a try but found that it does not work.

Given below sample from DMD/samples/d/htmlget.d,it does not compiled ,one of the error source is from char[][] vs string.After a couple of modification,now it looks like this:


/*
	HTMLget written by Christopher E. Miller
	This code is public domain.
	You may use it for any purpose.
	This code has no warranties and is provided 'as-is'.
*/


//debug = HTMLGET;


module htmlget;
import std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import std.stdio;

int main(string[] args)
{
	uint addr;
	if(args.length < 2)
	{
		printf("Usage:\n   htmlget <web-page>\n");
		return 0;
	}
	string url = args[1];
	int i;
	
	//i = std.string.find(url, "://");
	i=std.string.indexOf(url,"://");
	if(i != -1)
	{
		if(icmp(url[0 .. i], "http"))
			throw new Exception("http:// expected");
	}
	
	//i = std.string.find(url, '#');
	i=std.string.indexOf(url,'#');
	if(i != -1) // Remove anchor ref.
		url = url[0 .. i];
	
	//i = std.string.find(url, '/');
	i=std.string.indexOf(url,'/');
	string domain;
	if(i == -1)
	{
		domain = url;
		url = "/";
	}
	else
	{
		domain = url[0 .. i];
		url = url[i .. url.length];
	}
	
	uint port;
	//i = std.string.find(domain, ':');
	i=std.string.indexOf(domain,':');
	
	if(i == -1)
	{
		port = 80; // Default HTTP port.
	}
	else
	{
		//port = std.conv.toUshort(domain[i + 1 .. domain.length]);
		port=std.conv.to!(ushort)(domain[i+1..domain.length]);
		domain = domain[0 .. i];
	}
	addr=InternetAddress.parse(domain);
	debug(HTMLGET)
		printf("Connecting to " ~ domain ~ " on port " ~ std.string.to!string(port) ~ "...\n");
	
	auto Socket sock = new TcpSocket(new std.socket.InternetAddress(addr/*domain*/, port));
	Stream ss = new SocketStream(sock);
	
	debug(HTMLGET)
		printf("Connected!\nRequesting URL \"" ~ url ~ "\"...\n");
	
	if(port != 80)
	{
		char[] temp=cast(char[]) domain;
		temp=temp~cast(char[])":" ~cast(char[]) std.string.to!string(port);
		domain=cast(string)temp;
		//domain = domain ~ ":" ~ std.string.to!string(port);
	}
	ss.writeString("GET " ~ url ~ " HTTP/1.1\r\n"
		"Host: " ~ domain ~ "\r\n"
		"\r\n");
	
	// Skip HTTP header.
	string line;
	for(;;)
	{
		line = cast(string)ss.readLine();
		if(!line.length)
			break;
		
		const string CONTENT_TYPE_NAME = "Content-Type: ";
		if(line.length > CONTENT_TYPE_NAME.length &&
			!icmp(CONTENT_TYPE_NAME, line[0 .. CONTENT_TYPE_NAME.length]))
		{
			string type;
			type = line[CONTENT_TYPE_NAME.length .. line.length];
			if(type.length <= 5 || icmp("text/", type[0 .. 5]))
				throw new Exception("URL is not text");
		}
	}
	
	print_lines:
	while(!ss.eof())
	{
		line = cast(string)ss.readLine();
		printf("%.*s\n", line);
		
		//if(std.string.ifind(line, "</html>") != -1)
		//	break;
		size_t iw;
		for(iw = 0; iw != line.length; iw++)
		{
			if(!icmp("</html>", line[iw .. line.length]))
				break print_lines;
		}
	}
	
	return 0;
}

Error msg:

F:\DLang\DTwo\dmd\samples\d>bud -O -release -clean htmlget.d
htmlget.d(74): Error: constructor std.socket.InternetAddress.this () is not call
able using argument types (uint,uint)
htmlget.d(74): Error: expected 1 arguments, not 2 for non-variadic function type
 InternetAddress(ushort port)
htmlget.d(74): Error: cannot implicitly convert expression (addr) of type uint t
o ushort
htmlget.d(83): Error: function expected before (), not __error of type int

F:\DLang\DTwo\dmd\samples\d>

Read from this I learnt that class InternetAddress have no ctr this(string,ushort),nor this(uint,ushort),but it does when I read the source of std.socket.So what is the problem?

Thank you so much for your help in advance!!

Regards,
Sam 


More information about the Digitalmars-d-learn mailing list