httpget.pp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. var
  47. keys: integer;
  48. begin
  49. consoleDemoInit(); //setup the sub screen for printing
  50. iprintf(#10#10#9'Simple Wifi Connection Demo'#10#10);
  51. iprintf('Connecting via WFC data ...'#10);
  52. if not Wifi_InitDefault(WFC_CONNECT) then
  53. iprintf('Failed to connect!')
  54. else
  55. begin
  56. iprintf('Connected'#10#10);
  57. getHttp('www.akkit.org');
  58. end;
  59. while true do
  60. begin
  61. swiWaitForVBlank();
  62. if( keys and KEY_START ) <> 0 then
  63. break;
  64. end;
  65. end.