network.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <sys/confnet.h>
  8. #include <sys/socket.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <net/route.h>
  13. #include <dev/nicrtl.h>
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <arpa/inet.h>
  17. #include <pro/dhcp.h>
  18. #include <pro/sntp.h>
  19. #include "network.h"
  20. #include "ntp.h"
  21. void NetworkInit() {
  22. /* Register de internet controller. */
  23. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  24. printf("Registering failed.");
  25. }/* Netwerk configureren op dhcp */
  26. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  27. /* Done. */
  28. }else {
  29. printf("Ik heb een internet connectie. Ip is: %s", inet_ntoa(confnet.cdn_ip_addr));
  30. }
  31. }
  32. void httpGet(){
  33. printf("komt in httpget()");
  34. NutDelay(1000);
  35. TCPSOCKET* sock = NutTcpCreateSocket();
  36. char http[] = "GET /Projecten/projectgroepa6/internetradio/gettimezone.php HTTP/1.1\r\nHost: jancokock.me \r\n\r\n";
  37. char buffer[200];
  38. int len = sizeof(http);
  39. if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  40. printf("Can't connect to sever\n");
  41. }else{
  42. FILE *stream;
  43. stream = _fdopen((int) sock, "r b");
  44. if(NutTcpSend(sock, http, len) != len){
  45. printf("Writing headers failed.");
  46. NutDelay(1000);
  47. }else{
  48. printf("Headers writed. Now reading.");
  49. NutDelay(1000);
  50. NutTcpReceive(sock, buffer, sizeof(buffer));
  51. //fread(buffer, 1, sizeof(buffer), stream);
  52. NutDelay(1000);
  53. printf(buffer);
  54. };
  55. fclose(stream);
  56. }
  57. NutTcpCloseSocket(sock);
  58. int i;
  59. int enters = 0;
  60. int t = 0;
  61. char content[50];
  62. for(i = 0; i < strlen(buffer); i++)
  63. {
  64. if(enters > 3) {
  65. enters = 10;
  66. content[t] = buffer[i];
  67. t++;
  68. }
  69. if(buffer[i] == '\n' || buffer[i] == '\r')
  70. {
  71. enters++;
  72. }
  73. else if(enters < 10)
  74. {
  75. enters = 0;
  76. }
  77. }
  78. content[t] = '\0';
  79. printf("Contnt size %d\n", t);
  80. printf("content: ");
  81. for(i = 0; i < 10; i++)
  82. printf("%d -> %d\n", i, content[i]);
  83. // printf("%s\n", content);
  84. }