httpget.pp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. program httpget;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9, dswifi9;
  5. procedure getHttp(url: pchar);
  6. const
  7. // store the HTTP request for later
  8. request_text = 'GET /dswifi/example1.php HTTP/1.1\r\n' + 'Host: www.akkit.org\r\n' + 'User-Agent: Nintendo DS\r\n\r\n';
  9. var
  10. myhost: phostent;
  11. my_socket: cint;
  12. sain: sockaddr_in;
  13. recvd_len: cint;
  14. incoming_buffer: array [0..255] of char;
  15. begin
  16. // Let's send a simple HTTP request to a server and print the results!
  17. // Find the IP address of the server, with gethostbyname
  18. myhost := gethostbyname(url);
  19. iprintf('Found IP Address!'#10);
  20. // Create a TCP socket
  21. my_socket := socket(AF_INET, SOCK_STREAM, 0);
  22. iprintf('Created Socket!'#10);
  23. // Tell the socket to connect to the IP address we found, on port 80 (HTTP)
  24. sain.sin_family := AF_INET;
  25. sain.sin_port := htons(80);
  26. sain.sin_addr.s_addr := culong(Pointer(myhost^.h_addr_list^)^);
  27. connect(my_socket, psockaddr(@sain), sizeof(sain));
  28. iprintf('Connected to server!'#10);
  29. // send our request
  30. send(my_socket, pchar(request_text), strlen(request_text), 0);
  31. iprintf('Sent our request!'#10);
  32. // Print incoming data
  33. iprintf('Printing incoming data:'#10);
  34. repeat
  35. recvd_len := recv( my_socket, @incoming_buffer, 255, 0);
  36. if (recvd_len > 0) then // data was received!
  37. begin
  38. incoming_buffer[recvd_len] := #0; // null-terminate
  39. iprintf(incoming_buffer);
  40. end;
  41. until recvd_len <= 0;
  42. iprintf('Other side closed connection!' + #10);
  43. shutdown(my_socket, 0); // good practice to shutdown the socket.
  44. closesocket(my_socket); // remove the socket.
  45. end;
  46. begin
  47. consoleDemoInit(); //setup the sub screen for printing
  48. iprintf(#10#10#9'Simple Wifi Connection Demo'#10#10);
  49. iprintf('Connecting via WFC data ...'#10);
  50. if not Wifi_InitDefault(WFC_CONNECT) then
  51. iprintf('Failed to connect!')
  52. else
  53. begin
  54. iprintf('Connected'#10#10);
  55. getHttp('www.akkit.org');
  56. end;
  57. while true do
  58. swiWaitForVBlank();
  59. end.