sockettest.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. program sockettest;
  2. {$J+}
  3. {$macro on}
  4. {$mode objfpc}
  5. uses
  6. cmem, ctypes, network, gccore;
  7. function strncmp(__s1:Pchar; __s2:Pchar; __n:cardinal):longint;cdecl;external;
  8. var
  9. xfb: pcuint32 = nil;
  10. rmode: PGXRModeObj = nil;
  11. httd_handle: lwp_t;
  12. hits: cint = 0;
  13. var
  14. http_200: pchar = 'HTTP/1.1 200 OK\r\n';
  15. indexdata: pchar = '<html> \' +
  16. ' <head><title>A test page</title></head> \' +
  17. ' <body> \' +
  18. ' This small test page has had %d hits. \' +
  19. ' </body> \' +
  20. '</html>';
  21. http_html_hdr: pchar = 'Content-type: text/html\r\n\r\n';
  22. http_get_index: pchar = 'GET / HTTP/1.1\r\n';
  23. function httpd(arg: pointer): pointer; cdecl;
  24. var
  25. sock, csock: cint32;
  26. ret: cint;
  27. clientlen: cuint32;
  28. client: sockaddr_in;
  29. server: sockaddr_in;
  30. temp: array [0..1025] of cchar;
  31. begin
  32. clientlen := sizeof(client);
  33. sock := net_socket (AF_INET, SOCK_STREAM, IPPROTO_IP);
  34. if (sock = INVALID_SOCKET) then
  35. printf ('Cannot create a socket!'#10)
  36. else
  37. begin
  38. memset(@server, 0, sizeof(server));
  39. memset(@client, 0, sizeof(client));
  40. server.sin_family := AF_INET;
  41. server.sin_port := {htons}(80);
  42. server.sin_addr.s_addr := INADDR_ANY;
  43. ret := net_bind(sock, psockaddr(@server), sizeof(server));
  44. if ret <> 0 then
  45. printf('Error %d binding socket!'#10, ret)
  46. else
  47. begin
  48. if ret = net_listen(sock, 5) then
  49. printf('Error %d listening!'#10, ret)
  50. else
  51. begin
  52. while true do
  53. begin
  54. csock := net_accept (sock, psockaddr(@client), @clientlen);
  55. if csock < 0 then
  56. begin
  57. printf('Error connecting socket %d!'#10, csock);
  58. while true do;
  59. end;
  60. printf('Connecting port %d from %s'#10, client.sin_port, inet_ntoa(client.sin_addr));
  61. memset(@temp, 0, 1026);
  62. ret := net_recv(csock, @temp, 1024, 0);
  63. printf('Received %d bytes'#10, ret);
  64. if strncmp(@temp, http_get_index, strlen(http_get_index)) = 0 then
  65. begin
  66. inc(hits);
  67. net_send(csock, http_200, strlen(http_200), 0);
  68. net_send(csock, http_html_hdr, strlen(http_html_hdr), 0);
  69. sprintf(@temp, indexdata, hits);
  70. net_send(csock, @temp, strlen(@temp), 0);
  71. end;
  72. net_close(csock);
  73. end;
  74. end;
  75. end;
  76. end;
  77. result := nil;
  78. end;
  79. function initialise(): pointer;
  80. var
  81. framebuffer: pcuint32;
  82. begin
  83. VIDEO_Init();
  84. WPAD_Init();
  85. rmode := VIDEO_GetPreferredMode(nil);
  86. framebuffer := (SYS_AllocateFramebuffer(rmode));
  87. console_init(framebuffer,20,20,rmode^.fbWidth,rmode^.xfbHeight,rmode^.fbWidth*VI_DISPLAY_PIX_SZ);
  88. VIDEO_Configure(rmode);
  89. VIDEO_SetNextFramebuffer(framebuffer);
  90. VIDEO_SetBlack(FALSE);
  91. VIDEO_Flush();
  92. VIDEO_WaitVSync();
  93. if(rmode^.viTVMode and VI_NON_INTERLACE) <> 0 then VIDEO_WaitVSync();
  94. result := framebuffer;
  95. end;
  96. var
  97. ret: cint32;
  98. localip: array [0..15] of cchar;
  99. gateway: array [0..15] of cchar;
  100. netmask: array [0..15] of cchar;
  101. begin
  102. xfb := initialise();
  103. printf(#10'libogc network demo'#10);
  104. printf('Configuring network ...'#10);
  105. // Configure the network interface
  106. ret := if_config(localip, netmask, gateway, TRUE);
  107. if (ret >= 0) then
  108. begin
  109. printf('network configured, ip: %s, gw: %s, mask %s'#10, @localip, @gateway, @netmask);
  110. LWP_CreateThread(@httd_handle, // thread handle
  111. @httpd, // code
  112. @localip, // arg pointer for thread
  113. nil, // stack base
  114. 16*1024, // stack size
  115. 50 // thread priority
  116. );
  117. end else
  118. printf ('network configuration failed!'#10);
  119. while true do
  120. begin
  121. VIDEO_WaitVSync();
  122. WPAD_ScanPads();
  123. if (WPAD_ButtonsDown(0) and WPAD_BUTTON_HOME) <> 0 then
  124. exit;
  125. end;
  126. end.