fork download
  1. import std.stdio;
  2. import std.socket;
  3. import core.memory;
  4.  
  5. Socket tcpConnect(Address[] addresses)
  6. {
  7. auto sock = new TcpSocket(AddressFamily.INET);
  8.  
  9. foreach(addr; addresses)
  10. {
  11. try
  12. {
  13. sock.connect(addr);
  14. break;
  15. }
  16.  
  17. catch(SocketException e)
  18. {}
  19. }
  20.  
  21. return sock.isAlive ? sock : null;
  22. }
  23.  
  24. string buildHttpRequest(string host, string path)
  25. {
  26. return "GET " ~ path ~ "\r\n"
  27. "Host: " ~ host ~ "\r\n"
  28. "Connection: close\r\n"
  29. "HTTP/1.1\r\n"
  30. "\r\n";
  31. }
  32.  
  33. void sendFully(Socket sock, const(void)[] data)
  34. {
  35. ptrdiff_t sent;
  36. do data = data[(sent = sock.send(data)) .. $];
  37. while(sent > 0);
  38. }
  39.  
  40. void[] receiveFully(Socket sock)
  41. {
  42. auto result = new char[0];
  43. auto buf = new char[1024];
  44. ptrdiff_t recvd;
  45.  
  46. do result ~= buf[0 .. (recvd = sock.receive(buf))];
  47. while(recvd > 0);
  48.  
  49. return result;
  50. }
  51.  
  52. enum HOST = "www.c-plusplus.de";
  53. enum PATH = "/forum";
  54. enum PORT = 80;
  55.  
  56. void main()
  57. {
  58. try
  59. {
  60. auto sock = tcpConnect(getAddress(HOST, PORT));
  61.  
  62. if(!sock)
  63. {
  64. writeln("error connecting");
  65. return;
  66. }
  67.  
  68. sendFully(sock, buildHttpRequest(HOST, PATH));
  69. write(cast(char[])(receiveFully(sock)));
  70. }
  71.  
  72. catch(SocketException e)
  73. writeln(e);
  74. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.d(60): Error: undefined identifier getAddress
prog.d(60): Error: function expected before (), not getAddress of type int
prog.d(60): Error: function prog.tcpConnect (Address[] addresses) is not callable using argument types (int)
Error: cannot implicitly convert expression (__error) of type int to Address[]
Error: cannot cast int to Address[]
stdout
Standard output is empty