/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
- // This is for XP
- #define WINVER 0x0501
-
- #include <afxwin.h>
- #include <afxsock.h>
- #include <string.h>
- #include <windows.h>
- #include <math.h>
- #include "WinThread_Client.h"
- //#include "haptics.h"
-
- HANDLE t_WinThread_C = NULL ;
- HANDLE t_hEndEvent_C = NULL ;
- DWORD WINAPI ThreadFuncC( LPVOID ) ;
-
- CAsyncSocket CliSock;
- bool tConnected=false;
-
- char RecvBuffCli[1024];
- char SendBuffCli[1024];
-
- //class CHaptic:public HapticsClass{};
- //CHaptic jHaptic;
- /******************************************************************************/
- BOOL InitWinThread_C( void )
- {
- DWORD dwID ;
-
- // Create an event //
- t_hEndEvent_C = CreateEvent( NULL, TRUE, FALSE, NULL );
- if( !t_hEndEvent_C )return FALSE;
-
- // Create an thread //
- t_WinThread_C = CreateThread( NULL, 0, ThreadFuncC, NULL, 0, &dwID);
-
- // initial AfxSock and Create Sock //
- if(!AfxSocketInit()) AfxMessageBox("SOCKETS INIT FAILED");
- if(CliSock.Create()==0) AfxMessageBox("Socket fault to create");
-
-
- return t_WinThread_C != NULL ;
- }
- void UnInitWinThread_C( void )
- {
- SetEvent(t_hEndEvent_C);
- WaitForSingleObject( t_WinThread_C, INFINITE );
- CloseHandle( t_hEndEvent_C );
- CloseHandle( t_WinThread_C );
- }
-
- /******************************************************************************/
- DWORD WINAPI ThreadFuncC( LPVOID )
- {
- // jHaptic.init(20);
-
- int i;
-
- // Loop 32 times a second until the g_hEndEvent becomes signalled.
- while( WaitForSingleObject(t_hEndEvent_C,10) != WAIT_OBJECT_0 )
- {
- if (tConnected==false){
- // BOOL er=CliSock.Connect("127.0.0.1",3108);
- BOOL er=CliSock.Connect("169.254.220.193",4000);
- // BOOL er=CliSock.Connect("138.25.50.123",3108);
- int error_code=CliSock.GetLastError();
- if (error_code==10056) tConnected=true;
- }
- if (tConnected==true){
-
- char RecvBuff[50];
- while (strlen(RecvBuff)<29)
- {
- CliSock.Receive(&RecvBuff,30,MSG_PEEK);
- }
- CliSock.Receive(&RecvBuffCli,30);
- CliSock.Send(&RecvBuffCli,30);
-
- // if(RecvBuffCli[0]=='@')
- // {
- // for (i=0;i<30;i++) SendBuffCli[i]=RecvBuffCli[i];
- // CliSock.Send(&SendBuffCli,30);
- // }
- }
- } //while
- return 0 ;
- }
- /******************************************************************************/