/Haptics/20111027 Connect Two Haptics/WinThread_Client.cpp

http://sandblastplanning.googlecode.com/ · C++ · 89 lines · 53 code · 17 blank · 19 comment · 10 complexity · 8ed361a47318840b10745822735be256 MD5 · raw file

  1. // This is for XP
  2. #define WINVER 0x0501
  3. #include <afxwin.h>
  4. #include <afxsock.h>
  5. #include <string.h>
  6. #include <windows.h>
  7. #include <math.h>
  8. #include "WinThread_Client.h"
  9. //#include "haptics.h"
  10. HANDLE t_WinThread_C = NULL ;
  11. HANDLE t_hEndEvent_C = NULL ;
  12. DWORD WINAPI ThreadFuncC( LPVOID ) ;
  13. CAsyncSocket CliSock;
  14. bool tConnected=false;
  15. char RecvBuffCli[1024];
  16. char SendBuffCli[1024];
  17. //class CHaptic:public HapticsClass{};
  18. //CHaptic jHaptic;
  19. /******************************************************************************/
  20. BOOL InitWinThread_C( void )
  21. {
  22. DWORD dwID ;
  23. // Create an event //
  24. t_hEndEvent_C = CreateEvent( NULL, TRUE, FALSE, NULL );
  25. if( !t_hEndEvent_C )return FALSE;
  26. // Create an thread //
  27. t_WinThread_C = CreateThread( NULL, 0, ThreadFuncC, NULL, 0, &dwID);
  28. // initial AfxSock and Create Sock //
  29. if(!AfxSocketInit()) AfxMessageBox("SOCKETS INIT FAILED");
  30. if(CliSock.Create()==0) AfxMessageBox("Socket fault to create");
  31. return t_WinThread_C != NULL ;
  32. }
  33. void UnInitWinThread_C( void )
  34. {
  35. SetEvent(t_hEndEvent_C);
  36. WaitForSingleObject( t_WinThread_C, INFINITE );
  37. CloseHandle( t_hEndEvent_C );
  38. CloseHandle( t_WinThread_C );
  39. }
  40. /******************************************************************************/
  41. DWORD WINAPI ThreadFuncC( LPVOID )
  42. {
  43. // jHaptic.init(20);
  44. int i;
  45. // Loop 32 times a second until the g_hEndEvent becomes signalled.
  46. while( WaitForSingleObject(t_hEndEvent_C,10) != WAIT_OBJECT_0 )
  47. {
  48. if (tConnected==false){
  49. // BOOL er=CliSock.Connect("127.0.0.1",3108);
  50. BOOL er=CliSock.Connect("169.254.220.193",4000);
  51. // BOOL er=CliSock.Connect("138.25.50.123",3108);
  52. int error_code=CliSock.GetLastError();
  53. if (error_code==10056) tConnected=true;
  54. }
  55. if (tConnected==true){
  56. char RecvBuff[50];
  57. while (strlen(RecvBuff)<29)
  58. {
  59. CliSock.Receive(&RecvBuff,30,MSG_PEEK);
  60. }
  61. CliSock.Receive(&RecvBuffCli,30);
  62. CliSock.Send(&RecvBuffCli,30);
  63. // if(RecvBuffCli[0]=='@')
  64. // {
  65. // for (i=0;i<30;i++) SendBuffCli[i]=RecvBuffCli[i];
  66. // CliSock.Send(&SendBuffCli,30);
  67. // }
  68. }
  69. } //while
  70. return 0 ;
  71. }
  72. /******************************************************************************/