Advertisement
atm959

main.c

Mar 14th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #include <coreinit/thread.h>
  2. #include <coreinit/time.h>
  3.  
  4. #include <whb/proc.h>
  5. #include <whb/log.h>
  6. #include <whb/log_console.h>
  7.  
  8. #include <nsysnet/socket.h>
  9.  
  10. #include <string.h>
  11.  
  12. enum {
  13. PACKET_HEARTBEAT = 1,
  14. PACKET_TIME_MISMATCH
  15. };
  16.  
  17. int sockFD;
  18. struct sockaddr_in sa;
  19. char buffer[1024];
  20. uint32_t addr;
  21.  
  22. int main(int argc, char **argv) {
  23. int last_tm_sec = -1;
  24. OSCalendarTime tm;
  25.  
  26. WHBProcInit();
  27. WHBLogConsoleInit();
  28. WHBLogPrintf("Hello World!");
  29.  
  30. socket_lib_init();
  31.  
  32. sockFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  33.  
  34. memset(&sa, 0, sizeof(sa));
  35. sa.sin_family = AF_INET;
  36. sa.sin_port = htons(11000);
  37. inet_aton("192.168.0.20", &sa.sin_addr);
  38.  
  39. connect(sockFD, (struct sockaddr *)&sa, sizeof(sa));
  40.  
  41. while(WHBProcIsRunning()) {
  42. OSTicksToCalendarTime(OSGetTime(), &tm);
  43.  
  44. if (tm.tm_sec != last_tm_sec) {
  45. last_tm_sec = tm.tm_sec;
  46.  
  47. memset(buffer, 0, sizeof(buffer));
  48. buffer[0] = PACKET_HEARTBEAT;
  49. send(sockFD, buffer, 1024, 0);
  50. memset(buffer, 0, sizeof(buffer));
  51. while(buffer[0] != PACKET_HEARTBEAT){
  52. recv(sockFD, &buffer, 1024, 0);
  53. WHBLogPrintf("Waiting for heartbeat return...\n");
  54. }
  55. WHBLogPrintf("Heartbeat returned.\n");
  56. }
  57.  
  58. WHBLogConsoleDraw();
  59. OSSleepTicks(OSMillisecondsToTicks(100));
  60. }
  61.  
  62. WHBLogPrintf("Exiting... good bye.");
  63. WHBLogConsoleDraw();
  64. OSSleepTicks(OSMillisecondsToTicks(1000));
  65.  
  66. socketclose(sockFD);
  67. socket_lib_finish();
  68.  
  69. WHBLogConsoleFree();
  70. WHBProcShutdown();
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement