Create Project for repo.or.cz
[vp.git] / test / sock_test.cpp
blob87cca168ca864cefd4d901be3043b1625cecc978
1 /*******************************************************************************
2 * File Name : sock_test.cpp
3 *
4 * Author : Henry He
5 * Created Time : Tue 03 Nov 2009 04:30:29 PM CST
6 * Description :
7 ******************************************************************************/
10 /*******************************************************************************
11 * Desc : Includes Files
12 ******************************************************************************/
13 #include "utils/common.h"
16 /*******************************************************************************
17 * Desc : Macro Definations
18 ******************************************************************************/
21 /*******************************************************************************
22 * Desc : Type Definations
23 ******************************************************************************/
26 /*******************************************************************************
27 * Desc : Global Variables
28 ******************************************************************************/
31 /*******************************************************************************
32 * Desc : File Variables
33 ******************************************************************************/
36 static char message [] = "0 - abcdefgh";
39 static HLH_UDPSock zhuSock0;
40 static HLH_UDPSock zhuSock1;
42 static HLH_Thread zhtSendThread0;
43 static HLH_Thread zhtSendThread1;
46 /******************************************************************************
47 * Func :
48 * Desc :
49 * Args :
50 * Outs :
51 ******************************************************************************/
52 void OnUdpReceive (void *pvNULL,
53 void *pvBuf, UINT32 unLen, HLH_SockAddr &zhsSockAddr)
55 (void)pvNULL;
56 (void)zhsSockAddr;
58 Printf ("OnUdpReceive: receive %d bytes:\n", unLen);
60 unLen = unLen > sizeof(message) ? sizeof(message) : unLen;
61 ((char*)pvBuf) [unLen] = '\0';
63 Printf (" [ %s ]\n", pvBuf);
66 /******************************************************************************
67 * Func :
68 * Desc :
69 * Args :
70 * Outs :
71 ******************************************************************************/
72 void * SendThread0 (HLH_Thread &zhtThread, void *pvNULL)
74 HLH_DEBUG ( HLH_DEBUG_MAIN, ("SendThread0 started") );
75 zhtThread.ThreadStarted ();
76 (void)pvNULL;
77 do {
78 HLH_Time::Wait ( HLH_Time(0, 500*1000) );
79 message [0] = '0';
80 zhuSock0.Send ( message, sizeof(message) );
81 } while ( !zhtThread.IsStopping() );
83 return NULL;
86 /******************************************************************************
87 * Func :
88 * Desc :
89 * Args :
90 * Outs :
91 ******************************************************************************/
92 void * SendThread1 (HLH_Thread &zhtThread, void *pvNULL)
94 HLH_DEBUG ( HLH_DEBUG_MAIN, ("SendThread1 started") );
95 zhtThread.ThreadStarted ();
96 (void)pvNULL;
97 do {
98 HLH_Time::Wait ( HLH_Time(0, 700*1000) );
99 message [0] = '1';
100 zhuSock0.Send ( message, sizeof(message) );
101 } while ( !zhtThread.IsStopping() );
103 return NULL;
107 /******************************************************************************
108 * Func :
109 * Desc :
110 * Args :
111 * Outs :
112 ******************************************************************************/
113 int main ( int argc, char *argv[] )
115 HLH_SockAddr zhsSockAddr;
117 (void)argc;
118 (void)argv;
120 HLH_ENTER_FUNC (HLH_DEBUG_MAIN);
122 zhsSockAddr.SetAddr (INADDR_ANY, 8000);
123 zhuSock0.Create (zhsSockAddr);
124 zhsSockAddr.SetAddr ("127.0.0.1", 8002);
125 zhuSock0.Connect (zhsSockAddr);
126 zhuSock0.AddRecvHandler (OnUdpReceive, NULL);
128 zhsSockAddr.SetAddr (INADDR_ANY, 8002);
129 zhuSock1.Create (zhsSockAddr);
130 zhsSockAddr.SetAddr ("127.0.0.1", 8000);
131 zhuSock1.Connect (zhsSockAddr);
132 zhuSock1.AddRecvHandler (OnUdpReceive, NULL);
134 zhtSendThread0.Start (SendThread0, NULL);
135 zhtSendThread1.Start (SendThread1, NULL);
137 HLH_Time::Wait (HLH_Time(50, 0));
139 zhtSendThread0.Stop ();
140 zhtSendThread1.Stop ();
142 zhuSock0.Destroy ();
143 zhuSock1.Destroy ();
145 return 0;