1 /*******************************************************************************
2 * File Name : sock_test.cpp
5 * Created Time : Tue 03 Nov 2009 04:30:29 PM CST
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 /******************************************************************************
51 ******************************************************************************/
52 void OnUdpReceive (void *pvNULL
,
53 void *pvBuf
, UINT32 unLen
, HLH_SockAddr
&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 /******************************************************************************
71 ******************************************************************************/
72 void * SendThread0 (HLH_Thread
&zhtThread
, void *pvNULL
)
74 HLH_DEBUG ( HLH_DEBUG_MAIN
, ("SendThread0 started") );
75 zhtThread
.ThreadStarted ();
78 HLH_Time::Wait ( HLH_Time(0, 500*1000) );
80 zhuSock0
.Send ( message
, sizeof(message
) );
81 } while ( !zhtThread
.IsStopping() );
86 /******************************************************************************
91 ******************************************************************************/
92 void * SendThread1 (HLH_Thread
&zhtThread
, void *pvNULL
)
94 HLH_DEBUG ( HLH_DEBUG_MAIN
, ("SendThread1 started") );
95 zhtThread
.ThreadStarted ();
98 HLH_Time::Wait ( HLH_Time(0, 700*1000) );
100 zhuSock0
.Send ( message
, sizeof(message
) );
101 } while ( !zhtThread
.IsStopping() );
107 /******************************************************************************
112 ******************************************************************************/
113 int main ( int argc
, char *argv
[] )
115 HLH_SockAddr zhsSockAddr
;
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 ();