be-a9i_lllc4phpdf7pmc34h.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /******************************************************************************
  2. This tutorial presents a sample Client Connection.
  3. It can be used together with second tutorial "Connection/Server"
  4. /******************************************************************************/
  5. Connection connection; // connection for client/server connection
  6. Str data ; // received data
  7. /******************************************************************************/
  8. void InitPre()
  9. {
  10. EE_INIT();
  11. App.flag=APP_WORK_IN_BACKGROUND|APP_NO_PAUSE_ON_WINDOW_MOVE_SIZE; // specify work in background flag to work also when not focused
  12. App.x=1;
  13. D.mode(400, 300);
  14. D.scale(2);
  15. }
  16. bool Init()
  17. {
  18. Connection temp[16]; // try 16 connection attempts at the same time
  19. FREPA(temp)temp[i].clientConnectToServer(SockAddr().setLocal(0xFFFF-i)); // initiate connecting on all of them
  20. for(flt start=Time.curTime(); ; ) // wait until one connects
  21. {
  22. bool connecting=false;
  23. FREPA(temp)
  24. {
  25. Connection &conn=temp[i];
  26. if(conn.receive(0)) // if received any data
  27. if(conn.data.getStr()=="Hello from Server") // if this is hello from our test server
  28. {
  29. Swap(conn, connection); // keep this as the main connection
  30. goto found; // stop searching
  31. }
  32. if(conn.state()==CONNECT_CONNECTING || conn.state()==CONNECT_AWAIT_GREET || conn.state()==CONNECT_GREETED)connecting=true;
  33. }
  34. if(!connecting)Exit("Couldn't find server"); // if no connection is in progress then fail
  35. if(Time.curTime()-start>=2)Exit("Connection timeout"); // wait up to 2 seconds
  36. Time.wait(1); // wait a bit
  37. }
  38. found:
  39. return true;
  40. }
  41. /******************************************************************************/
  42. void Shut()
  43. {
  44. connection.del();
  45. }
  46. /******************************************************************************/
  47. bool Update()
  48. {
  49. if(Kb.bp(KB_ESC))return false;
  50. // move window on mouse button
  51. if(Ms.b(0))WindowMove(Ms.pixelDelta().x, Ms.pixelDelta().y);
  52. // check if received any data
  53. if(connection.receive(0))
  54. {
  55. connection.data.getStr(data); // read data as string
  56. }
  57. // send random text when space pressed
  58. if(Kb.bp(KB_SPACE))
  59. {
  60. File f; f.writeMem();
  61. f.putStr(S+"Random Text "+Random(1024));
  62. f.pos(0);
  63. connection.send(f);
  64. }
  65. return true;
  66. }
  67. /******************************************************************************/
  68. void Draw()
  69. {
  70. D.clear(TURQ);
  71. if(connection.state()!=CONNECT_GREETED)
  72. {
  73. D.text(0, 0, S+"Invalid connection");
  74. }else
  75. {
  76. D.text(0, 0.4, S+"Server Address: "+connection.address().asText());
  77. D.text(0, 0.3, S+"Press Space to send random text");
  78. D.text(0, 0.0, S+"Received Data: "+data);
  79. }
  80. }
  81. /******************************************************************************/