xref: /AOO41X/main/sal/qa/osl/socket/osl_StreamSocket.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /**  test coder preface:
32*cdf0e10cSrcweir      1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
33*cdf0e10cSrcweir      if you are not including ws2_32.lib in makefile.mk,  the including format will be like this:
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir      .IF "$(GUI)" == "WNT"
36*cdf0e10cSrcweir      SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
37*cdf0e10cSrcweir      SHL1STDLIBS +=  ws2_32.lib
38*cdf0e10cSrcweir      .ENDIF
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir      likewise on Solaris platform.
41*cdf0e10cSrcweir      .IF "$(GUI)" == "UNX"
42*cdf0e10cSrcweir      SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
43*cdf0e10cSrcweir      SHL1STDLIBS += -lsocket -ldl -lnsl
44*cdf0e10cSrcweir      .ENDIF
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir      2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
47*cdf0e10cSrcweir      category.
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir      3. some fragment of Socket source implementation are lack of comment so it is hard for testers
50*cdf0e10cSrcweir      guess what the exact functionality or usage of a member.  Hope the Socket section's comment
51*cdf0e10cSrcweir      will be added.
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir      4. following functions are declared but not implemented:
54*cdf0e10cSrcweir      inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
55*cdf0e10cSrcweir */
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //------------------------------------------------------------------------
58*cdf0e10cSrcweir // include files
59*cdf0e10cSrcweir //------------------------------------------------------------------------
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir //#include "osl_Socket_Const.h"
64*cdf0e10cSrcweir #include "sockethelper.hxx"
65*cdf0e10cSrcweir #include <osl/conditn.hxx>
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir using namespace osl;
68*cdf0e10cSrcweir using namespace rtl;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir #define IP_PORT_MYPORT9  8897
71*cdf0e10cSrcweir #define IP_PORT_MYPORT10 18900
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir const char * pTestString1 = "test socket";
74*cdf0e10cSrcweir const char * pTestString2 = " Passed#OK";
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir //------------------------------------------------------------------------
77*cdf0e10cSrcweir // helper functions
78*cdf0e10cSrcweir //------------------------------------------------------------------------
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir // just used to test socket::close() when accepting
81*cdf0e10cSrcweir class AcceptorThread : public Thread
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     ::osl::AcceptorSocket asAcceptorSocket;
84*cdf0e10cSrcweir     ::rtl::OUString aHostIP;
85*cdf0e10cSrcweir     sal_Bool bOK;
86*cdf0e10cSrcweir protected:
87*cdf0e10cSrcweir     void SAL_CALL run( )
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir             ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
90*cdf0e10cSrcweir             ::osl::StreamSocket ssStreamConnection;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
93*cdf0e10cSrcweir             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
94*cdf0e10cSrcweir             if  ( sal_True != bOK1 )
95*cdf0e10cSrcweir             {
96*cdf0e10cSrcweir                 t_print("# AcceptorSocket bind address failed.\n" ) ;
97*cdf0e10cSrcweir                 return;
98*cdf0e10cSrcweir             }
99*cdf0e10cSrcweir             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
100*cdf0e10cSrcweir             if  ( sal_True != bOK2 )
101*cdf0e10cSrcweir             {
102*cdf0e10cSrcweir                 t_print("# AcceptorSocket listen address failed.\n" ) ;
103*cdf0e10cSrcweir                 return;
104*cdf0e10cSrcweir             }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             asAcceptorSocket.enableNonBlockingMode( sal_False );
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir             oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
109*cdf0e10cSrcweir             if (eResult != osl_Socket_Ok )
110*cdf0e10cSrcweir             {
111*cdf0e10cSrcweir                 bOK = sal_True;
112*cdf0e10cSrcweir                 t_print("AcceptorThread: acceptConnection failed! \n");
113*cdf0e10cSrcweir             }
114*cdf0e10cSrcweir         }
115*cdf0e10cSrcweir public:
116*cdf0e10cSrcweir     AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
117*cdf0e10cSrcweir             : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             bOK = sal_False;
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     sal_Bool isOK() { return bOK; }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     ~AcceptorThread( )
125*cdf0e10cSrcweir         {
126*cdf0e10cSrcweir             if ( isRunning( ) )
127*cdf0e10cSrcweir             {
128*cdf0e10cSrcweir                 asAcceptorSocket.shutdown();
129*cdf0e10cSrcweir                 t_print("# error: Acceptor thread not terminated.\n" );
130*cdf0e10cSrcweir             }
131*cdf0e10cSrcweir         }
132*cdf0e10cSrcweir };
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir /** Server Socket Thread, served as a temp little server to communicate with client.
135*cdf0e10cSrcweir  */
136*cdf0e10cSrcweir class ServerSocketThread : public Thread
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     osl::Condition    &m_aCondition;
139*cdf0e10cSrcweir protected:
140*cdf0e10cSrcweir     oslThreadIdentifier m_id;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     void SAL_CALL run( )
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
145*cdf0e10cSrcweir             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 );
146*cdf0e10cSrcweir             ::osl::StreamSocket ssStreamConnection;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
149*cdf0e10cSrcweir             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
150*cdf0e10cSrcweir             while ( schedule( ) == sal_True )
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir                 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
153*cdf0e10cSrcweir                 if  ( sal_True != bOK1 )
154*cdf0e10cSrcweir                 {
155*cdf0e10cSrcweir                     t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
156*cdf0e10cSrcweir                     break;
157*cdf0e10cSrcweir                 }
158*cdf0e10cSrcweir                 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
159*cdf0e10cSrcweir                 if  ( sal_True != bOK2 )
160*cdf0e10cSrcweir                 {
161*cdf0e10cSrcweir                     t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
162*cdf0e10cSrcweir                     break;
163*cdf0e10cSrcweir                 }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir                 asAcceptorSocket.enableNonBlockingMode( sal_False );
166*cdf0e10cSrcweir                 m_aCondition.set();
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir                 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
169*cdf0e10cSrcweir                 if (eResult != osl_Socket_Ok )
170*cdf0e10cSrcweir                 {
171*cdf0e10cSrcweir                     t_print("ServerSocketThread: acceptConnection failed! \n");
172*cdf0e10cSrcweir                     break;
173*cdf0e10cSrcweir                 }
174*cdf0e10cSrcweir                 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
175*cdf0e10cSrcweir                 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
176*cdf0e10cSrcweir                 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
177*cdf0e10cSrcweir                 //t_print("# read buffer content: %s\n", pReadBuffer );
178*cdf0e10cSrcweir                 break;
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir             ssStreamConnection.close();
181*cdf0e10cSrcweir             asAcceptorSocket.close();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     void SAL_CALL onTerminated( )
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             //t_print("# normally terminate this server thread %d!\n",  m_id );
188*cdf0e10cSrcweir         }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir public:
191*cdf0e10cSrcweir     // public to check if data transmition is OK
192*cdf0e10cSrcweir     sal_Char pReadBuffer[30];
193*cdf0e10cSrcweir     ServerSocketThread( osl::Condition &_aCond  ):m_aCondition(_aCond)
194*cdf0e10cSrcweir         {
195*cdf0e10cSrcweir             m_aCondition.reset();
196*cdf0e10cSrcweir             t_print("#init ServerSocketThread\n");
197*cdf0e10cSrcweir             m_id = getIdentifier( );
198*cdf0e10cSrcweir             //t_print("# successfully creat this ServerSocketThread %d!\n",  m_id );
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     ~ServerSocketThread( )
202*cdf0e10cSrcweir         {
203*cdf0e10cSrcweir             if ( isRunning( ) )
204*cdf0e10cSrcweir                 t_print("# error: ServerSocketThread has not terminated.\n" );
205*cdf0e10cSrcweir         }
206*cdf0e10cSrcweir };
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server.
209*cdf0e10cSrcweir  */
210*cdf0e10cSrcweir class ClientSocketThread : public Thread
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir protected:
213*cdf0e10cSrcweir     osl::Condition    &m_aCondition;
214*cdf0e10cSrcweir     oslThreadIdentifier m_id;
215*cdf0e10cSrcweir     ::osl::SocketAddr m_saTargetSocketAddr;
216*cdf0e10cSrcweir     ::osl::ConnectorSocket m_csConnectorSocket;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     void SAL_CALL run( )
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir             TimeValue *pTimeout;
221*cdf0e10cSrcweir             pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
222*cdf0e10cSrcweir             pTimeout->Seconds = 5;
223*cdf0e10cSrcweir             pTimeout->Nanosec = 0;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir             /// if the thread should terminate, schedule return false
226*cdf0e10cSrcweir             //while ( schedule( ) == sal_True )
227*cdf0e10cSrcweir             //{
228*cdf0e10cSrcweir             if ( osl::Condition::result_ok != m_aCondition.wait( pTimeout ) )
229*cdf0e10cSrcweir             {
230*cdf0e10cSrcweir                 free( pTimeout );
231*cdf0e10cSrcweir                 return;
232*cdf0e10cSrcweir             }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
235*cdf0e10cSrcweir             {
236*cdf0e10cSrcweir                 m_csConnectorSocket.send( pTestString1, 11 ); // "test socket"
237*cdf0e10cSrcweir                 m_csConnectorSocket.send( pTestString2, 10);
238*cdf0e10cSrcweir             }
239*cdf0e10cSrcweir             else
240*cdf0e10cSrcweir                 t_print("# ClientSocketThread: connect failed! \n");
241*cdf0e10cSrcweir             //  terminate();
242*cdf0e10cSrcweir             //}
243*cdf0e10cSrcweir             m_csConnectorSocket.close();
244*cdf0e10cSrcweir             free( pTimeout );
245*cdf0e10cSrcweir         }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     void SAL_CALL onTerminated( )
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir             //t_print("# normally terminate this thread %d!\n",  m_id );
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir public:
253*cdf0e10cSrcweir     ClientSocketThread( osl::Condition &_aCond  ):
254*cdf0e10cSrcweir             m_aCondition(_aCond),
255*cdf0e10cSrcweir             m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
256*cdf0e10cSrcweir             m_csConnectorSocket( )
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             m_id = getIdentifier( );
259*cdf0e10cSrcweir             //t_print("# successfully creat this client thread %d!\n",  m_id );
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir     ~ClientSocketThread( )
263*cdf0e10cSrcweir         {
264*cdf0e10cSrcweir             if ( isRunning( ) )
265*cdf0e10cSrcweir                 t_print("# error: client thread not terminated.\n" );
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir };
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir // -----------------------------------------------------------------------------
271*cdf0e10cSrcweir // Helper functions, to create buffers, check buffers
272*cdf0e10cSrcweir class ValueCheckProvider
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir     bool m_bFoundFailure;
275*cdf0e10cSrcweir     char *m_pBuffer;
276*cdf0e10cSrcweir     sal_Int32 m_nBufferSize;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir public:
279*cdf0e10cSrcweir     ValueCheckProvider()
280*cdf0e10cSrcweir             :m_bFoundFailure(false),
281*cdf0e10cSrcweir              m_pBuffer(NULL),
282*cdf0e10cSrcweir              m_nBufferSize(0)
283*cdf0e10cSrcweir         {
284*cdf0e10cSrcweir         }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     bool       isFailure() {return m_bFoundFailure;}
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     const char* getBuffer() {return m_pBuffer;}
289*cdf0e10cSrcweir     char*       getWriteBuffer() {return m_pBuffer;}
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir     sal_Int32   getBufferSize() {return m_nBufferSize;}
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir     bool checkValues(sal_Int32 _nLength, int _nValue)
294*cdf0e10cSrcweir         {
295*cdf0e10cSrcweir             m_bFoundFailure = false;
296*cdf0e10cSrcweir             for(sal_Int32 i=0;i<_nLength;i++)
297*cdf0e10cSrcweir             {
298*cdf0e10cSrcweir                 if (m_pBuffer[i] != _nValue)
299*cdf0e10cSrcweir                 {
300*cdf0e10cSrcweir                     m_bFoundFailure = true;
301*cdf0e10cSrcweir                 }
302*cdf0e10cSrcweir             }
303*cdf0e10cSrcweir             return m_bFoundFailure;
304*cdf0e10cSrcweir         }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     void createBuffer(sal_Int32 _nLength, int _nValue)
307*cdf0e10cSrcweir         {
308*cdf0e10cSrcweir             m_nBufferSize = _nLength;
309*cdf0e10cSrcweir             m_pBuffer = (char*) malloc(m_nBufferSize);
310*cdf0e10cSrcweir             if (m_pBuffer)
311*cdf0e10cSrcweir             {
312*cdf0e10cSrcweir                 memset(m_pBuffer, _nValue, m_nBufferSize);
313*cdf0e10cSrcweir             }
314*cdf0e10cSrcweir         }
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir     void freeBuffer()
317*cdf0e10cSrcweir         {
318*cdf0e10cSrcweir             if (m_pBuffer) free(m_pBuffer);
319*cdf0e10cSrcweir         }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir };
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir // -----------------------------------------------------------------------------
324*cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server.
325*cdf0e10cSrcweir  */
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir class ReadSocketThread : public Thread
328*cdf0e10cSrcweir {
329*cdf0e10cSrcweir     ValueCheckProvider m_aValues;
330*cdf0e10cSrcweir     int m_nValue;
331*cdf0e10cSrcweir     osl::Condition    &m_aCondition;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir protected:
334*cdf0e10cSrcweir     oslThreadIdentifier m_id;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir     void SAL_CALL run( )
337*cdf0e10cSrcweir         {
338*cdf0e10cSrcweir             ::osl::SocketAddr      m_aTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
339*cdf0e10cSrcweir             ::osl::ConnectorSocket m_aConnectorSocket;
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir             if (! m_aTargetSocketAddr.is())
342*cdf0e10cSrcweir             {
343*cdf0e10cSrcweir                 t_print("# SocketAddr was NOT created successfully!\n");
344*cdf0e10cSrcweir             }
345*cdf0e10cSrcweir             else
346*cdf0e10cSrcweir             {
347*cdf0e10cSrcweir                 t_print("start ReadSocketThread\n");
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir                 TimeValue *pTimeout;
350*cdf0e10cSrcweir                 pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
351*cdf0e10cSrcweir                 pTimeout->Seconds = 5;
352*cdf0e10cSrcweir                 pTimeout->Nanosec = 0;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir                 m_aCondition.wait();
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir                 t_print("connect()\n");
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir                 oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout );
359*cdf0e10cSrcweir                 if ( osl_Socket_Ok == eResult )
360*cdf0e10cSrcweir                 {
361*cdf0e10cSrcweir                     sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
362*cdf0e10cSrcweir                     m_aValues.checkValues(nReadCount, m_nValue);
363*cdf0e10cSrcweir                 }
364*cdf0e10cSrcweir                 else
365*cdf0e10cSrcweir                 {
366*cdf0e10cSrcweir                     t_print("# ReadSocketThread: connect failed! \n");
367*cdf0e10cSrcweir                     printSocketResult(eResult);
368*cdf0e10cSrcweir                 }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir                 //remove this line for deadlock on solaris( margritte.germany )
371*cdf0e10cSrcweir                 m_aConnectorSocket.close();
372*cdf0e10cSrcweir                 free( pTimeout );
373*cdf0e10cSrcweir             }
374*cdf0e10cSrcweir         }
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir     void SAL_CALL onTerminated( )
377*cdf0e10cSrcweir         {
378*cdf0e10cSrcweir             //t_print("# normally terminate this thread %d!\n",  m_id );
379*cdf0e10cSrcweir         }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir public:
382*cdf0e10cSrcweir     sal_Int32 getCount() {return m_aValues.getBufferSize();}
383*cdf0e10cSrcweir     bool       isOk() {return m_aValues.isFailure() == true ? false : true;}
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir     ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
386*cdf0e10cSrcweir             : m_nValue( _nValue ),
387*cdf0e10cSrcweir               m_aCondition(_aCond)
388*cdf0e10cSrcweir         {
389*cdf0e10cSrcweir             t_print("#init ReadSocketThread\n");
390*cdf0e10cSrcweir             m_id = getIdentifier( );
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir             //t_print("# successfully creat this client thread %d!\n",  m_id );
393*cdf0e10cSrcweir             m_aValues.createBuffer(_nBufferSize, 0);
394*cdf0e10cSrcweir         }
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir     ~ReadSocketThread( )
397*cdf0e10cSrcweir         {
398*cdf0e10cSrcweir             if ( isRunning( ) )
399*cdf0e10cSrcweir                 t_print("# error: client thread not terminated.\n" );
400*cdf0e10cSrcweir             m_aValues.freeBuffer();
401*cdf0e10cSrcweir         }
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir };
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir /** Server Socket Thread, write a file which is large
406*cdf0e10cSrcweir  */
407*cdf0e10cSrcweir class WriteSocketThread : public Thread
408*cdf0e10cSrcweir {
409*cdf0e10cSrcweir     ValueCheckProvider m_aValues;
410*cdf0e10cSrcweir     osl::Condition    &m_aCondition;
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir protected:
413*cdf0e10cSrcweir     oslThreadIdentifier m_id;
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir     void SAL_CALL run( )
416*cdf0e10cSrcweir         {
417*cdf0e10cSrcweir             t_print("start WriteSocketThread\n");
418*cdf0e10cSrcweir             ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
419*cdf0e10cSrcweir             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
420*cdf0e10cSrcweir             if (! saLocalSocketAddr.is())
421*cdf0e10cSrcweir             {
422*cdf0e10cSrcweir                 t_print("LocalSocketAddr was NOT created successfully!\n");
423*cdf0e10cSrcweir             }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir             ::osl::StreamSocket ssStreamConnection;
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir             //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
428*cdf0e10cSrcweir             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );    //sal_True);
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir             /// if the thread should terminate, schedule return false
431*cdf0e10cSrcweir             // while ( schedule( ) == sal_True )
432*cdf0e10cSrcweir             // {
433*cdf0e10cSrcweir             t_print("bind()\n");
434*cdf0e10cSrcweir             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
435*cdf0e10cSrcweir             if  ( sal_True != bOK1 )
436*cdf0e10cSrcweir             {
437*cdf0e10cSrcweir                 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
438*cdf0e10cSrcweir             }
439*cdf0e10cSrcweir             else
440*cdf0e10cSrcweir             {
441*cdf0e10cSrcweir                 t_print("listen()\n");
442*cdf0e10cSrcweir                 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
443*cdf0e10cSrcweir                 if  ( sal_True != bOK2 )
444*cdf0e10cSrcweir                 {
445*cdf0e10cSrcweir                     t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
446*cdf0e10cSrcweir                 }
447*cdf0e10cSrcweir                 else
448*cdf0e10cSrcweir                 {
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir                     // blocking mode, if read/recv failed, block until success
451*cdf0e10cSrcweir                     asAcceptorSocket.enableNonBlockingMode( sal_False);
452*cdf0e10cSrcweir                     t_print("acceptConnection()\n");
453*cdf0e10cSrcweir                     m_aCondition.set();
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir                     oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
456*cdf0e10cSrcweir                     if (eResult != osl_Socket_Ok )
457*cdf0e10cSrcweir                     {
458*cdf0e10cSrcweir                         t_print("WriteSocketThread: acceptConnection failed! \n");
459*cdf0e10cSrcweir                     }
460*cdf0e10cSrcweir                     else
461*cdf0e10cSrcweir                     {
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir // LLA: removed, due to the fact, this is to error prone
464*cdf0e10cSrcweir // LLA:             char * pSrcRoot = getenv("SOURCE_ROOT");
465*cdf0e10cSrcweir // LLA:             // LLA: This is absolute wrong!
466*cdf0e10cSrcweir // LLA:             // strcat( pSrcRoot, "/sal/inc/osl/file.hxx");
467*cdf0e10cSrcweir // LLA:             rtl::OString sSrcRoot(pSrcRoot);
468*cdf0e10cSrcweir // LLA:             sSrcRoot += "/sal/inc/osl/file.hxx";
469*cdf0e10cSrcweir // LLA:
470*cdf0e10cSrcweir // LLA:             ::rtl::OUString sFilePath = ::rtl::OUString::createFromAscii( sSrcRoot.getStr() );
471*cdf0e10cSrcweir // LLA: #ifdef WNT
472*cdf0e10cSrcweir // LLA:             while (sFilePath.lastIndexOf('/') != -1)
473*cdf0e10cSrcweir // LLA:                 sFilePath = sFilePath.replace('/',(sal_Unicode)'\\');
474*cdf0e10cSrcweir // LLA: #endif
475*cdf0e10cSrcweir // LLA:             FILE *stream;
476*cdf0e10cSrcweir // LLA:             sal_uInt64     nCount_read;
477*cdf0e10cSrcweir // LLA:             sal_Char       buffer_read[FILE_READ];
478*cdf0e10cSrcweir // LLA:
479*cdf0e10cSrcweir // LLA:             if( (stream = fopen( oustring2char( sFilePath ), "r+t" )) != NULL )
480*cdf0e10cSrcweir // LLA:             {
481*cdf0e10cSrcweir // LLA:                 /* Attempt to read in 25 characters */
482*cdf0e10cSrcweir // LLA:                 nCount_read = fread( buffer_read, sizeof( char ), FILE_READ, stream );
483*cdf0e10cSrcweir // LLA:                 fclose( stream );
484*cdf0e10cSrcweir // LLA:             }
485*cdf0e10cSrcweir // LLA:             else
486*cdf0e10cSrcweir // LLA:                 t_print("# File $SRC_ROOT/sal/inc/osl/file.hxx could not be opened\n" );
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir                         t_print("write()\n");
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir                         ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
491*cdf0e10cSrcweir                         t_print("done written.\n");
492*cdf0e10cSrcweir                     }
493*cdf0e10cSrcweir                 }
494*cdf0e10cSrcweir             }
495*cdf0e10cSrcweir             ssStreamConnection.close();
496*cdf0e10cSrcweir             asAcceptorSocket.close();
497*cdf0e10cSrcweir         }
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir     void SAL_CALL onTerminated( )
500*cdf0e10cSrcweir         {
501*cdf0e10cSrcweir             //t_print("# normally terminate this server thread %d!\n",  m_id );
502*cdf0e10cSrcweir         }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir public:
505*cdf0e10cSrcweir     // public to check if data transmition is OK
506*cdf0e10cSrcweir     WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
507*cdf0e10cSrcweir             : m_aCondition(_aCond)
508*cdf0e10cSrcweir         {
509*cdf0e10cSrcweir             m_aCondition.reset();
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir             t_print("#init WriteSocketThread\n");
512*cdf0e10cSrcweir             m_id = getIdentifier( );
513*cdf0e10cSrcweir             //t_print("# successfully creat this server thread %d!\n",  m_id );
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir             m_aValues.createBuffer(_nBufferSize, _nValue);
516*cdf0e10cSrcweir         }
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir     ~WriteSocketThread( )
519*cdf0e10cSrcweir         {
520*cdf0e10cSrcweir             if ( isRunning( ) )
521*cdf0e10cSrcweir                 t_print("# error: server thread not terminated.\n" );
522*cdf0e10cSrcweir             m_aValues.freeBuffer();
523*cdf0e10cSrcweir         }
524*cdf0e10cSrcweir };
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir // -----------------------------------------------------------------------------
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir namespace osl_StreamSocket
529*cdf0e10cSrcweir {
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir     /** testing the methods:
532*cdf0e10cSrcweir         inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
533*cdf0e10cSrcweir         oslProtocol Protocol = osl_Socket_ProtocolIp,
534*cdf0e10cSrcweir         oslSocketType   Type = osl_Socket_TypeStream);
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir         inline StreamSocket( const StreamSocket & );
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir         inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir         inline StreamSocket( oslSocket Socket );
541*cdf0e10cSrcweir     */
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir     class ctors : public CppUnit::TestFixture
544*cdf0e10cSrcweir     {
545*cdf0e10cSrcweir     public:
546*cdf0e10cSrcweir         oslSocket sHandle;
547*cdf0e10cSrcweir         // initialization
548*cdf0e10cSrcweir         void setUp( )
549*cdf0e10cSrcweir             {
550*cdf0e10cSrcweir                 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
551*cdf0e10cSrcweir             }
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir         void tearDown( )
554*cdf0e10cSrcweir             {
555*cdf0e10cSrcweir                 sHandle = NULL;
556*cdf0e10cSrcweir             }
557*cdf0e10cSrcweir 
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir         void ctors_none()
560*cdf0e10cSrcweir             {
561*cdf0e10cSrcweir                 /// Socket constructor.
562*cdf0e10cSrcweir                 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.",
565*cdf0e10cSrcweir                                         osl_Socket_TypeStream ==  ssSocket.getType( ) );
566*cdf0e10cSrcweir             }
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir         void ctors_acquire()
569*cdf0e10cSrcweir             {
570*cdf0e10cSrcweir                 /// Socket constructor.
571*cdf0e10cSrcweir                 ::osl::StreamSocket ssSocket( sHandle );
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
574*cdf0e10cSrcweir                                         osl_Socket_TypeStream == ssSocket.getType( ) );
575*cdf0e10cSrcweir             }
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir         void ctors_no_acquire()
578*cdf0e10cSrcweir             {
579*cdf0e10cSrcweir                 /// Socket constructor.
580*cdf0e10cSrcweir                 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
583*cdf0e10cSrcweir                                        osl_Socket_TypeStream == ssSocket.getType( ) );
584*cdf0e10cSrcweir             }
585*cdf0e10cSrcweir 
586*cdf0e10cSrcweir         void ctors_copy_ctor()
587*cdf0e10cSrcweir             {
588*cdf0e10cSrcweir                 /// Socket constructor.
589*cdf0e10cSrcweir                 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
590*cdf0e10cSrcweir                 /// Socket copy constructor.
591*cdf0e10cSrcweir                 ::osl::StreamSocket copySocket( ssSocket );
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
594*cdf0e10cSrcweir                                        osl_Socket_TypeStream == copySocket.getType( ) );
595*cdf0e10cSrcweir             }
596*cdf0e10cSrcweir 
597*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE( ctors );
598*cdf0e10cSrcweir         CPPUNIT_TEST( ctors_none );
599*cdf0e10cSrcweir         CPPUNIT_TEST( ctors_acquire );
600*cdf0e10cSrcweir         CPPUNIT_TEST( ctors_no_acquire );
601*cdf0e10cSrcweir         CPPUNIT_TEST( ctors_copy_ctor );
602*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE_END();
603*cdf0e10cSrcweir 
604*cdf0e10cSrcweir     }; // class ctors
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir     class send_recv: public CppUnit::TestFixture
607*cdf0e10cSrcweir     {
608*cdf0e10cSrcweir     public:
609*cdf0e10cSrcweir         // initialization
610*cdf0e10cSrcweir         void setUp( )
611*cdf0e10cSrcweir             {
612*cdf0e10cSrcweir             }
613*cdf0e10cSrcweir 
614*cdf0e10cSrcweir         void tearDown( )
615*cdf0e10cSrcweir             {
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir             }
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir         void send_recv1()
620*cdf0e10cSrcweir             {
621*cdf0e10cSrcweir                 osl::Condition aCondition;
622*cdf0e10cSrcweir                 //client sent two strings, and server received, check the order and value
623*cdf0e10cSrcweir                 ServerSocketThread myServerThread( aCondition );
624*cdf0e10cSrcweir                 ClientSocketThread myClientThread( aCondition );
625*cdf0e10cSrcweir                 myServerThread.create( );
626*cdf0e10cSrcweir                 myClientThread.create( );
627*cdf0e10cSrcweir 
628*cdf0e10cSrcweir                 //wait until the thread terminate
629*cdf0e10cSrcweir                 myClientThread.join( );
630*cdf0e10cSrcweir                 myServerThread.join( );
631*cdf0e10cSrcweir                 sal_Char myStr[30] = "";
632*cdf0e10cSrcweir                 strcat( myStr, pTestString1 );
633*cdf0e10cSrcweir                 strcat( myStr, pTestString2 );
634*cdf0e10cSrcweir                 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
635*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.",
636*cdf0e10cSrcweir                                        nRes == 0 );
637*cdf0e10cSrcweir             }
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir         // error when recv
640*cdf0e10cSrcweir         void send_recv2()
641*cdf0e10cSrcweir             {
642*cdf0e10cSrcweir                 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
643*cdf0e10cSrcweir                 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 );
644*cdf0e10cSrcweir                 ::osl::StreamSocket ssStreamConnection;
645*cdf0e10cSrcweir                 sal_Char pReadBuffer[30] = "";
646*cdf0e10cSrcweir 
647*cdf0e10cSrcweir                 osl::Condition aCondition;
648*cdf0e10cSrcweir                 aCondition.reset();
649*cdf0e10cSrcweir                 ClientSocketThread myClientThread( aCondition );
650*cdf0e10cSrcweir                 myClientThread.create( );
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir                 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir                 asAcceptorSocket.bind( saLocalSocketAddr );
655*cdf0e10cSrcweir                 asAcceptorSocket.listen( 1 );
656*cdf0e10cSrcweir                 asAcceptorSocket.enableNonBlockingMode( sal_True );
657*cdf0e10cSrcweir                 aCondition.set();
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir                 asAcceptorSocket.acceptConnection( ssStreamConnection );
660*cdf0e10cSrcweir                 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir                 myClientThread.join( ) ;
663*cdf0e10cSrcweir                 ssStreamConnection.close();
664*cdf0e10cSrcweir                 asAcceptorSocket.close();
665*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 );
666*cdf0e10cSrcweir             }
667*cdf0e10cSrcweir 
668*cdf0e10cSrcweir         // LLA: This is a helper function, which create 2 threads, a server and a client.
669*cdf0e10cSrcweir         // the server writes the buffersize to the client.
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir         void write_read(sal_Int32 _nBufferSize, int _nValue)
672*cdf0e10cSrcweir             {
673*cdf0e10cSrcweir                 //client sent two strings, and server received, check the order and value
674*cdf0e10cSrcweir                 osl::Condition aCondition;
675*cdf0e10cSrcweir                 WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition);
676*cdf0e10cSrcweir                 ReadSocketThread myClientThread(_nBufferSize, _nValue, aCondition);
677*cdf0e10cSrcweir                 myServerThread.create( );
678*cdf0e10cSrcweir //          thread_sleep( 1 );
679*cdf0e10cSrcweir                 myClientThread.create( );
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir                 //wait until the thread terminate
682*cdf0e10cSrcweir                 myClientThread.join( );
683*cdf0e10cSrcweir                 myServerThread.join( );
684*cdf0e10cSrcweir 
685*cdf0e10cSrcweir                 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
686*cdf0e10cSrcweir                 // Proteon PRONET  = 2046), so here test read 4000 bytes
687*cdf0e10cSrcweir                 sal_Int32 nLength = myClientThread.getCount();
688*cdf0e10cSrcweir                 bool       bIsOk   = myClientThread.isOk(); // check if the values are right.
689*cdf0e10cSrcweir 
690*cdf0e10cSrcweir                 t_print("Length:=%d\n", nLength);
691*cdf0e10cSrcweir                 t_print(" bIsOk:=%d\n", bIsOk);
692*cdf0e10cSrcweir 
693*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.",
694*cdf0e10cSrcweir                                        nLength == _nBufferSize && bIsOk == true);
695*cdf0e10cSrcweir             }
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir         // Tests with different values and sizes
698*cdf0e10cSrcweir         void write_read_001()
699*cdf0e10cSrcweir             {
700*cdf0e10cSrcweir                 write_read(50, 10);
701*cdf0e10cSrcweir             }
702*cdf0e10cSrcweir         void write_read_002()
703*cdf0e10cSrcweir             {
704*cdf0e10cSrcweir                 write_read(1024, 20);
705*cdf0e10cSrcweir             }
706*cdf0e10cSrcweir         void write_read_003()
707*cdf0e10cSrcweir             {
708*cdf0e10cSrcweir                 write_read(4000, 1);
709*cdf0e10cSrcweir             }
710*cdf0e10cSrcweir         void write_read_004()
711*cdf0e10cSrcweir             {
712*cdf0e10cSrcweir                 write_read(8192, 3);
713*cdf0e10cSrcweir             }
714*cdf0e10cSrcweir         void write_read_005()
715*cdf0e10cSrcweir             {
716*cdf0e10cSrcweir                 write_read(32768, 3);
717*cdf0e10cSrcweir             }
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE( send_recv );
720*cdf0e10cSrcweir         CPPUNIT_TEST( write_read_001 );
721*cdf0e10cSrcweir         CPPUNIT_TEST( write_read_002 );
722*cdf0e10cSrcweir         CPPUNIT_TEST( write_read_003 );
723*cdf0e10cSrcweir         CPPUNIT_TEST( write_read_004 );
724*cdf0e10cSrcweir         CPPUNIT_TEST( write_read_005 );
725*cdf0e10cSrcweir         CPPUNIT_TEST( send_recv1 );
726*cdf0e10cSrcweir         CPPUNIT_TEST( send_recv2 );
727*cdf0e10cSrcweir //      CPPUNIT_TEST( write_read );
728*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE_END();
729*cdf0e10cSrcweir     }; // class send_recv
730*cdf0e10cSrcweir 
731*cdf0e10cSrcweir // -----------------------------------------------------------------------------
732*cdf0e10cSrcweir 
733*cdf0e10cSrcweir     class SendClientThread : public Thread
734*cdf0e10cSrcweir     {
735*cdf0e10cSrcweir     protected:
736*cdf0e10cSrcweir         ::osl::SocketAddr m_saTargetSocketAddr;
737*cdf0e10cSrcweir         ::osl::ConnectorSocket m_csConnectorSocket;
738*cdf0e10cSrcweir         void SAL_CALL run( )
739*cdf0e10cSrcweir             {
740*cdf0e10cSrcweir                 TimeValue *pTimeout;
741*cdf0e10cSrcweir                 pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
742*cdf0e10cSrcweir                 pTimeout->Seconds = 5;
743*cdf0e10cSrcweir                 pTimeout->Nanosec = 0;
744*cdf0e10cSrcweir 
745*cdf0e10cSrcweir                 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
746*cdf0e10cSrcweir                 {
747*cdf0e10cSrcweir                     sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket"
748*cdf0e10cSrcweir 
749*cdf0e10cSrcweir                     sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
750*cdf0e10cSrcweir                     thread_sleep( 2 );
751*cdf0e10cSrcweir                     m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
752*cdf0e10cSrcweir                     t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
753*cdf0e10cSrcweir                     //thread_sleep( 1 );
754*cdf0e10cSrcweir                 }
755*cdf0e10cSrcweir                 else
756*cdf0e10cSrcweir                     t_print("# SendClientThread: connect failed! \n");
757*cdf0e10cSrcweir 
758*cdf0e10cSrcweir                 m_csConnectorSocket.close();
759*cdf0e10cSrcweir                 free( pTimeout );
760*cdf0e10cSrcweir             }
761*cdf0e10cSrcweir     public:
762*cdf0e10cSrcweir         SendClientThread(  ):
763*cdf0e10cSrcweir                 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
764*cdf0e10cSrcweir                 m_csConnectorSocket( )
765*cdf0e10cSrcweir             {
766*cdf0e10cSrcweir                 //t_print("# successfully creat this SendClientThread %d!\n",  m_id );
767*cdf0e10cSrcweir             }
768*cdf0e10cSrcweir 
769*cdf0e10cSrcweir         ~SendClientThread( )
770*cdf0e10cSrcweir             {
771*cdf0e10cSrcweir                 if ( isRunning( ) )
772*cdf0e10cSrcweir                     t_print("# error: SendClientThread has not terminated.\n" );
773*cdf0e10cSrcweir             }
774*cdf0e10cSrcweir 
775*cdf0e10cSrcweir     };
776*cdf0e10cSrcweir 
777*cdf0e10cSrcweir     class shutdown: public CppUnit::TestFixture
778*cdf0e10cSrcweir     {
779*cdf0e10cSrcweir     public:
780*cdf0e10cSrcweir         // initialization
781*cdf0e10cSrcweir         void setUp( )
782*cdf0e10cSrcweir             {
783*cdf0e10cSrcweir             }
784*cdf0e10cSrcweir 
785*cdf0e10cSrcweir         void tearDown( )
786*cdf0e10cSrcweir             {
787*cdf0e10cSrcweir 
788*cdf0e10cSrcweir             }
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir         // similar to close_002
791*cdf0e10cSrcweir         void shutdown_001()
792*cdf0e10cSrcweir             {
793*cdf0e10cSrcweir #if defined(LINUX)
794*cdf0e10cSrcweir                 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
795*cdf0e10cSrcweir                 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
796*cdf0e10cSrcweir                 myAcceptorThread.create();
797*cdf0e10cSrcweir 
798*cdf0e10cSrcweir                 thread_sleep( 1 );
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir                 //when accepting, shutdown the socket, the thread will not block for accepting
801*cdf0e10cSrcweir                 asSocket.shutdown();
802*cdf0e10cSrcweir                 myAcceptorThread.join();
803*cdf0e10cSrcweir 
804*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
805*cdf0e10cSrcweir                                         myAcceptorThread.isOK( ) == sal_True );
806*cdf0e10cSrcweir #endif
807*cdf0e10cSrcweir             }
808*cdf0e10cSrcweir 
809*cdf0e10cSrcweir         void shutdown_002()
810*cdf0e10cSrcweir             {
811*cdf0e10cSrcweir                 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
812*cdf0e10cSrcweir                 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
813*cdf0e10cSrcweir                 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
814*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
815*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
816*cdf0e10cSrcweir                 sal_Char pReadBuffer[40];
817*cdf0e10cSrcweir //          osl::Condition aCondition;
818*cdf0e10cSrcweir                 SendClientThread mySendThread;
819*cdf0e10cSrcweir                 mySendThread.create();
820*cdf0e10cSrcweir 
821*cdf0e10cSrcweir                 asSocket.enableNonBlockingMode( sal_False );
822*cdf0e10cSrcweir                 ::osl::StreamSocket ssConnectionSocket;
823*cdf0e10cSrcweir                 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
824*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
825*cdf0e10cSrcweir 
826*cdf0e10cSrcweir                 /* set socket option SO_LINGER 0, so close immediatly */
827*cdf0e10cSrcweir                 linger aLingerSet;
828*cdf0e10cSrcweir                 sal_Int32 nBufferLen = sizeof( struct linger );
829*cdf0e10cSrcweir                 aLingerSet.l_onoff = 0;
830*cdf0e10cSrcweir                 aLingerSet.l_linger = 0;
831*cdf0e10cSrcweir 
832*cdf0e10cSrcweir                 ssConnectionSocket.setOption( osl_Socket_OptionLinger,  &aLingerSet, nBufferLen );
833*cdf0e10cSrcweir                 thread_sleep( 1 );
834*cdf0e10cSrcweir                 //sal_uInt32 nRecv1 = 0;
835*cdf0e10cSrcweir                 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
836*cdf0e10cSrcweir 
837*cdf0e10cSrcweir                 //shutdown read after client the first send complete
838*cdf0e10cSrcweir                 ssConnectionSocket.shutdown( osl_Socket_DirRead );
839*cdf0e10cSrcweir 
840*cdf0e10cSrcweir                 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
841*cdf0e10cSrcweir                 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
842*cdf0e10cSrcweir                 t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
843*cdf0e10cSrcweir                 mySendThread.join();
844*cdf0e10cSrcweir 
845*cdf0e10cSrcweir                 ssConnectionSocket.close();
846*cdf0e10cSrcweir                 asSocket.close();
847*cdf0e10cSrcweir 
848*cdf0e10cSrcweir                 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
849*cdf0e10cSrcweir                    http://dbforums.com/arch/186/2002/12/586417
850*cdf0e10cSrcweir                    While on Solaris, after shutdown(DirRead), all read will return 0
851*cdf0e10cSrcweir                 */
852*cdf0e10cSrcweir #ifdef LINUX
853*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
854*cdf0e10cSrcweir                                         nRead1 > 0  && nRead3 == 0 );
855*cdf0e10cSrcweir #else
856*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
857*cdf0e10cSrcweir                                         nRead1 > 0  && nRead2 == 0 && nRead3 == 0 );
858*cdf0e10cSrcweir #endif
859*cdf0e10cSrcweir 
860*cdf0e10cSrcweir             }
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir         void shutdown_003()
863*cdf0e10cSrcweir             {
864*cdf0e10cSrcweir                 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
865*cdf0e10cSrcweir                 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
866*cdf0e10cSrcweir                 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
867*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
868*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
869*cdf0e10cSrcweir                 sal_Char pReadBuffer[40];
870*cdf0e10cSrcweir                 osl::Condition aCondition;
871*cdf0e10cSrcweir                 SendClientThread mySendThread;
872*cdf0e10cSrcweir                 mySendThread.create();
873*cdf0e10cSrcweir 
874*cdf0e10cSrcweir                 asSocket.enableNonBlockingMode( sal_False );
875*cdf0e10cSrcweir                 ::osl::StreamSocket ssConnectionSocket;
876*cdf0e10cSrcweir                 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
877*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
878*cdf0e10cSrcweir 
879*cdf0e10cSrcweir                 thread_sleep( 1 );
880*cdf0e10cSrcweir                 //shutdown write after client the first send complete
881*cdf0e10cSrcweir                 ssConnectionSocket.shutdown( osl_Socket_DirWrite );
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir                 // recv should not shutdown
884*cdf0e10cSrcweir                 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir                 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
887*cdf0e10cSrcweir                 // still can read
888*cdf0e10cSrcweir                 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
889*cdf0e10cSrcweir                 t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
890*cdf0e10cSrcweir                 mySendThread.join();
891*cdf0e10cSrcweir                 ssConnectionSocket.close();
892*cdf0e10cSrcweir                 asSocket.close();
893*cdf0e10cSrcweir 
894*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).",
895*cdf0e10cSrcweir                                         nRead1  > 0  && nWrite == 0 && nRead3 > 0);
896*cdf0e10cSrcweir 
897*cdf0e10cSrcweir             }
898*cdf0e10cSrcweir 
899*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE( shutdown );
900*cdf0e10cSrcweir         CPPUNIT_TEST( shutdown_001 );
901*cdf0e10cSrcweir         CPPUNIT_TEST( shutdown_002 );
902*cdf0e10cSrcweir         CPPUNIT_TEST( shutdown_003 );
903*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE_END();
904*cdf0e10cSrcweir     }; // class shutdown
905*cdf0e10cSrcweir 
906*cdf0e10cSrcweir     class isExceptionPending: public CppUnit::TestFixture
907*cdf0e10cSrcweir     {
908*cdf0e10cSrcweir     public:
909*cdf0e10cSrcweir         void isExPending_001()
910*cdf0e10cSrcweir             {
911*cdf0e10cSrcweir                 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
912*cdf0e10cSrcweir                 TimeValue *pTimeout;
913*cdf0e10cSrcweir                 pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
914*cdf0e10cSrcweir                 pTimeout->Seconds = 3;
915*cdf0e10cSrcweir                 pTimeout->Nanosec = 0;
916*cdf0e10cSrcweir                 sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
917*cdf0e10cSrcweir                 free( pTimeout );
918*cdf0e10cSrcweir 
919*cdf0e10cSrcweir                 CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.",
920*cdf0e10cSrcweir                                         bOk == sal_False );
921*cdf0e10cSrcweir             }
922*cdf0e10cSrcweir 
923*cdf0e10cSrcweir         /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
924*cdf0e10cSrcweir 
925*cdf0e10cSrcweir 
926*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE( isExceptionPending );
927*cdf0e10cSrcweir         CPPUNIT_TEST( isExPending_001 );
928*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE_END();
929*cdf0e10cSrcweir     }; // class isExceptionPending
930*cdf0e10cSrcweir 
931*cdf0e10cSrcweir // -----------------------------------------------------------------------------
932*cdf0e10cSrcweir /** Server Socket Thread, write a file which is large
933*cdf0e10cSrcweir  */
934*cdf0e10cSrcweir // LLA: class WriteSocketThread : public Thread
935*cdf0e10cSrcweir // LLA: {
936*cdf0e10cSrcweir // LLA:     ValueCheckProvider m_aValues;
937*cdf0e10cSrcweir // LLA:
938*cdf0e10cSrcweir // LLA: protected:
939*cdf0e10cSrcweir // LLA:     oslThreadIdentifier m_id;
940*cdf0e10cSrcweir // LLA:
941*cdf0e10cSrcweir // LLA:     void SAL_CALL run( )
942*cdf0e10cSrcweir // LLA:     {
943*cdf0e10cSrcweir // LLA:         ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
944*cdf0e10cSrcweir // LLA:         ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("10.16.66.252"), 8888 );
945*cdf0e10cSrcweir // LLA:         ::osl::StreamSocket ssStreamConnection;
946*cdf0e10cSrcweir // LLA:
947*cdf0e10cSrcweir // LLA:         //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
948*cdf0e10cSrcweir // LLA:         asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );    //sal_True);
949*cdf0e10cSrcweir // LLA:
950*cdf0e10cSrcweir // LLA:         /// if the thread should terminate, schedule return false
951*cdf0e10cSrcweir // LLA:         while ( schedule( ) == sal_True )
952*cdf0e10cSrcweir // LLA:         {
953*cdf0e10cSrcweir // LLA:             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
954*cdf0e10cSrcweir // LLA:             if  ( sal_True != bOK1 )
955*cdf0e10cSrcweir // LLA:             {
956*cdf0e10cSrcweir // LLA:                 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
957*cdf0e10cSrcweir // LLA:                 break;
958*cdf0e10cSrcweir // LLA:             }
959*cdf0e10cSrcweir // LLA:             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
960*cdf0e10cSrcweir // LLA:             if  ( sal_True != bOK2 )
961*cdf0e10cSrcweir // LLA:             {
962*cdf0e10cSrcweir // LLA:                 t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
963*cdf0e10cSrcweir // LLA:                 break;
964*cdf0e10cSrcweir // LLA:             }
965*cdf0e10cSrcweir // LLA:             // blocking mode, if read/recv failed, block until success
966*cdf0e10cSrcweir // LLA:             asAcceptorSocket.enableNonBlockingMode( sal_False);
967*cdf0e10cSrcweir // LLA:
968*cdf0e10cSrcweir // LLA:             oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
969*cdf0e10cSrcweir // LLA:             if (eResult != osl_Socket_Ok )
970*cdf0e10cSrcweir // LLA:             {
971*cdf0e10cSrcweir // LLA:                 t_print("WriteSocketThread: acceptConnection failed! \n");
972*cdf0e10cSrcweir // LLA:                 break;
973*cdf0e10cSrcweir // LLA:             }
974*cdf0e10cSrcweir // LLA:
975*cdf0e10cSrcweir // LLA:
976*cdf0e10cSrcweir // LLA:             sal_Int32 nReadNumber1 = ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
977*cdf0e10cSrcweir // LLA:             break;
978*cdf0e10cSrcweir // LLA:         }
979*cdf0e10cSrcweir // LLA:         ssStreamConnection.close();
980*cdf0e10cSrcweir // LLA:         asAcceptorSocket.close();
981*cdf0e10cSrcweir // LLA:     }
982*cdf0e10cSrcweir // LLA: }
983*cdf0e10cSrcweir // -----------------------------------------------------------------------------
984*cdf0e10cSrcweir // -----------------------------------------------------------------------------
985*cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server.
986*cdf0e10cSrcweir  */
987*cdf0e10cSrcweir 
988*cdf0e10cSrcweir #define IP_PORT_TEST 8900
989*cdf0e10cSrcweir 
990*cdf0e10cSrcweir     class ReadSocket2Thread : public Thread
991*cdf0e10cSrcweir     {
992*cdf0e10cSrcweir         osl::Condition &m_aCondition;
993*cdf0e10cSrcweir         char*        m_pBuffer;
994*cdf0e10cSrcweir         sal_Int32    m_nBufferSize;
995*cdf0e10cSrcweir         sal_Int32    m_nReadCount;
996*cdf0e10cSrcweir         rtl::OString m_sAddr;
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir         bool         m_bOk;
999*cdf0e10cSrcweir 
1000*cdf0e10cSrcweir         void setFailed()
1001*cdf0e10cSrcweir             {
1002*cdf0e10cSrcweir                 m_bOk = false;
1003*cdf0e10cSrcweir             }
1004*cdf0e10cSrcweir 
1005*cdf0e10cSrcweir     protected:
1006*cdf0e10cSrcweir         oslThreadIdentifier m_id;
1007*cdf0e10cSrcweir 
1008*cdf0e10cSrcweir         void read()
1009*cdf0e10cSrcweir             {
1010*cdf0e10cSrcweir                 if (m_sAddr.getLength() == 0)
1011*cdf0e10cSrcweir                 {
1012*cdf0e10cSrcweir                     setFailed();
1013*cdf0e10cSrcweir                     return;
1014*cdf0e10cSrcweir                 }
1015*cdf0e10cSrcweir 
1016*cdf0e10cSrcweir                 // 10.16.66.252
1017*cdf0e10cSrcweir                 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST );
1018*cdf0e10cSrcweir                 ::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1019*cdf0e10cSrcweir 
1020*cdf0e10cSrcweir                 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1021*cdf0e10cSrcweir 
1022*cdf0e10cSrcweir                 m_aCondition.wait();
1023*cdf0e10cSrcweir                 t_print("wait done\n");
1024*cdf0e10cSrcweir 
1025*cdf0e10cSrcweir                 TimeValue *pTimeout;
1026*cdf0e10cSrcweir                 pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
1027*cdf0e10cSrcweir                 pTimeout->Seconds = 20;
1028*cdf0e10cSrcweir                 pTimeout->Nanosec = 0;
1029*cdf0e10cSrcweir 
1030*cdf0e10cSrcweir 
1031*cdf0e10cSrcweir                 // blocking mode, if read/recv failed, block until success
1032*cdf0e10cSrcweir                 t_print("enableNonBlockingMode(false)\n");
1033*cdf0e10cSrcweir                 aSocket.enableNonBlockingMode( sal_False );
1034*cdf0e10cSrcweir 
1035*cdf0e10cSrcweir 
1036*cdf0e10cSrcweir                 t_print("connect()\n");
1037*cdf0e10cSrcweir                 oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout );
1038*cdf0e10cSrcweir                 if ( osl_Socket_Ok == eResult)
1039*cdf0e10cSrcweir                 {
1040*cdf0e10cSrcweir                     if (m_pBuffer)
1041*cdf0e10cSrcweir                     {
1042*cdf0e10cSrcweir                         t_print("read()\n");
1043*cdf0e10cSrcweir                         m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize );
1044*cdf0e10cSrcweir                         t_print("%d bytes recived.\n", m_nReadCount);
1045*cdf0e10cSrcweir                     }
1046*cdf0e10cSrcweir                 }
1047*cdf0e10cSrcweir                 else
1048*cdf0e10cSrcweir                 {
1049*cdf0e10cSrcweir                     t_print("# ReadSocket2Thread: connect failed! \n");
1050*cdf0e10cSrcweir                     printSocketResult(eResult);
1051*cdf0e10cSrcweir                     setFailed();
1052*cdf0e10cSrcweir                 }
1053*cdf0e10cSrcweir 
1054*cdf0e10cSrcweir                 //remove this line for deadlock on solaris( margritte.germany )
1055*cdf0e10cSrcweir                 aSocket.close();
1056*cdf0e10cSrcweir                 free( pTimeout );
1057*cdf0e10cSrcweir             }
1058*cdf0e10cSrcweir 
1059*cdf0e10cSrcweir         void SAL_CALL run( )
1060*cdf0e10cSrcweir             {
1061*cdf0e10cSrcweir                 read();
1062*cdf0e10cSrcweir             }
1063*cdf0e10cSrcweir 
1064*cdf0e10cSrcweir         void SAL_CALL onTerminated( )
1065*cdf0e10cSrcweir             {
1066*cdf0e10cSrcweir                 //t_print("# normally terminate this thread %d!\n",  m_id );
1067*cdf0e10cSrcweir             }
1068*cdf0e10cSrcweir 
1069*cdf0e10cSrcweir     public:
1070*cdf0e10cSrcweir         sal_Int32 getCount() {return m_nReadCount;}
1071*cdf0e10cSrcweir         bool       isOk() {return m_nReadCount == 0 ? false : true;}
1072*cdf0e10cSrcweir         bool       getFailed() {return m_bOk == false ? true : false;}
1073*cdf0e10cSrcweir 
1074*cdf0e10cSrcweir         ReadSocket2Thread(osl::Condition &_aCondition)
1075*cdf0e10cSrcweir                 :m_aCondition(_aCondition),
1076*cdf0e10cSrcweir                  m_nReadCount(0),
1077*cdf0e10cSrcweir                  m_bOk( true )
1078*cdf0e10cSrcweir             {
1079*cdf0e10cSrcweir                 m_aCondition.reset();
1080*cdf0e10cSrcweir                 m_pBuffer = (char*) malloc(1024);
1081*cdf0e10cSrcweir                 if (m_pBuffer)
1082*cdf0e10cSrcweir                 {
1083*cdf0e10cSrcweir                     m_nBufferSize = 1024;
1084*cdf0e10cSrcweir                 }
1085*cdf0e10cSrcweir 
1086*cdf0e10cSrcweir                 m_id = getIdentifier( );
1087*cdf0e10cSrcweir                 //t_print("# successfully creat this client thread %d!\n",  m_id );
1088*cdf0e10cSrcweir             }
1089*cdf0e10cSrcweir 
1090*cdf0e10cSrcweir         void setAddr(rtl::OString const& _sAddr)
1091*cdf0e10cSrcweir             {
1092*cdf0e10cSrcweir                 m_sAddr = _sAddr;
1093*cdf0e10cSrcweir             }
1094*cdf0e10cSrcweir 
1095*cdf0e10cSrcweir         ~ReadSocket2Thread( )
1096*cdf0e10cSrcweir             {
1097*cdf0e10cSrcweir                 if ( isRunning( ) )
1098*cdf0e10cSrcweir                     t_print("# error: client thread not terminated.\n" );
1099*cdf0e10cSrcweir                 free(m_pBuffer);
1100*cdf0e10cSrcweir             }
1101*cdf0e10cSrcweir 
1102*cdf0e10cSrcweir     };
1103*cdf0e10cSrcweir 
1104*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
1105*cdf0e10cSrcweir 
1106*cdf0e10cSrcweir     class justtest : public CppUnit::TestFixture
1107*cdf0e10cSrcweir     {
1108*cdf0e10cSrcweir         void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &)
1109*cdf0e10cSrcweir             {
1110*cdf0e10cSrcweir                 ::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1111*cdf0e10cSrcweir                 ::osl::SocketAddr aSocketAddr;
1112*cdf0e10cSrcweir 
1113*cdf0e10cSrcweir                 if (! aSocketAddr.setPort(IP_PORT_TEST))
1114*cdf0e10cSrcweir                 {
1115*cdf0e10cSrcweir                     t_print("# cant set port\n" );
1116*cdf0e10cSrcweir                 }
1117*cdf0e10cSrcweir 
1118*cdf0e10cSrcweir                 if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr())))
1119*cdf0e10cSrcweir                 {
1120*cdf0e10cSrcweir                     t_print("# cant set hostname/ip\n" );
1121*cdf0e10cSrcweir                 }
1122*cdf0e10cSrcweir 
1123*cdf0e10cSrcweir                 rtl::OUString aHostname = aSocketAddr.getHostname();
1124*cdf0e10cSrcweir                 aSocketAddr.getPort();
1125*cdf0e10cSrcweir 
1126*cdf0e10cSrcweir 
1127*cdf0e10cSrcweir                 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1128*cdf0e10cSrcweir                 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1129*cdf0e10cSrcweir 
1130*cdf0e10cSrcweir                 /// if the thread should terminate, schedule return false
1131*cdf0e10cSrcweir                 // while ( schedule( ) == sal_True )
1132*cdf0e10cSrcweir                 // {
1133*cdf0e10cSrcweir                 if (! aSocket.bind( aSocketAddr ))
1134*cdf0e10cSrcweir                 {
1135*cdf0e10cSrcweir                     t_print("# can't bind.\n" );
1136*cdf0e10cSrcweir                 }
1137*cdf0e10cSrcweir                 if (! aSocket.listen( ))
1138*cdf0e10cSrcweir                 {
1139*cdf0e10cSrcweir                     t_print("# can't listen. \n" );
1140*cdf0e10cSrcweir                 }
1141*cdf0e10cSrcweir 
1142*cdf0e10cSrcweir                 // blocking mode, if read/recv failed, block until success
1143*cdf0e10cSrcweir                 aSocket.enableNonBlockingMode( sal_False);
1144*cdf0e10cSrcweir                 ::osl::StreamSocket ssStreamConnection;
1145*cdf0e10cSrcweir 
1146*cdf0e10cSrcweir                 oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection );
1147*cdf0e10cSrcweir                 if (eResult != osl_Socket_Ok )
1148*cdf0e10cSrcweir                 {
1149*cdf0e10cSrcweir                     t_print("WriteSocketThread: acceptConnection failed! \n");
1150*cdf0e10cSrcweir                     // break;
1151*cdf0e10cSrcweir                 }
1152*cdf0e10cSrcweir                 char const * pBuffer = "Test String\n";
1153*cdf0e10cSrcweir                 sal_Int32 nBufferSize = strlen(pBuffer);
1154*cdf0e10cSrcweir                 ssStreamConnection.write( pBuffer, nBufferSize );
1155*cdf0e10cSrcweir                 // break;
1156*cdf0e10cSrcweir                 // }
1157*cdf0e10cSrcweir 
1158*cdf0e10cSrcweir                 // ssStreamConnection.close();
1159*cdf0e10cSrcweir                 aSocket.close();
1160*cdf0e10cSrcweir             }
1161*cdf0e10cSrcweir 
1162*cdf0e10cSrcweir         // -----------------------------------------------------------------------------
1163*cdf0e10cSrcweir 
1164*cdf0e10cSrcweir         void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ )
1165*cdf0e10cSrcweir             {
1166*cdf0e10cSrcweir                 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1167*cdf0e10cSrcweir                 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1168*cdf0e10cSrcweir 
1169*cdf0e10cSrcweir                 if (! aSocketAddr.is())
1170*cdf0e10cSrcweir                 {
1171*cdf0e10cSrcweir                     t_print("is failed.\n");
1172*cdf0e10cSrcweir                     return;
1173*cdf0e10cSrcweir                 }
1174*cdf0e10cSrcweir 
1175*cdf0e10cSrcweir                 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1176*cdf0e10cSrcweir                 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1177*cdf0e10cSrcweir 
1178*cdf0e10cSrcweir                 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1179*cdf0e10cSrcweir                 if  ( aResult != osl_Socket_Ok )
1180*cdf0e10cSrcweir                 {
1181*cdf0e10cSrcweir                     t_print("# send_Connector: connect failed. \n" );
1182*cdf0e10cSrcweir                 }
1183*cdf0e10cSrcweir                 else
1184*cdf0e10cSrcweir                 {
1185*cdf0e10cSrcweir                     // blocking mode, if read/recv failed, block until success
1186*cdf0e10cSrcweir //                    aSocket.enableNonBlockingMode( sal_False );
1187*cdf0e10cSrcweir 
1188*cdf0e10cSrcweir //                     _aCondition.set();
1189*cdf0e10cSrcweir 
1190*cdf0e10cSrcweir                     ::osl::StreamSocket ssStreamConnection(aSocket);
1191*cdf0e10cSrcweir 
1192*cdf0e10cSrcweir                     char const * pBuffer = "GET / HTTP/1.0\015\012\015\012";
1193*cdf0e10cSrcweir                     sal_Int32 nBufferSize = strlen(pBuffer);
1194*cdf0e10cSrcweir                     ssStreamConnection.write( pBuffer, nBufferSize );
1195*cdf0e10cSrcweir 
1196*cdf0e10cSrcweir                     char *pBufferPeek = (char*) malloc(1024);
1197*cdf0e10cSrcweir                     sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1198*cdf0e10cSrcweir                     free(pBufferPeek);
1199*cdf0e10cSrcweir 
1200*cdf0e10cSrcweir                     char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1201*cdf0e10cSrcweir                     sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1202*cdf0e10cSrcweir                     pBuffer2[nReadNumberReal] = '\0';
1203*cdf0e10cSrcweir 
1204*cdf0e10cSrcweir                     t_print("received: %s\n", pBuffer2);
1205*cdf0e10cSrcweir 
1206*cdf0e10cSrcweir                     // char * pBuffer3 = "quit\n";
1207*cdf0e10cSrcweir                     // nBufferSize = strlen(pBuffer3);
1208*cdf0e10cSrcweir                     // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1209*cdf0e10cSrcweir 
1210*cdf0e10cSrcweir                     rtl::OUString suError = ssStreamConnection.getErrorAsString();
1211*cdf0e10cSrcweir                     free(pBuffer2);
1212*cdf0e10cSrcweir                     // ssStreamConnection.close();
1213*cdf0e10cSrcweir 
1214*cdf0e10cSrcweir                     // ssStreamConnection.close();
1215*cdf0e10cSrcweir                 }
1216*cdf0e10cSrcweir                 aSocket.shutdown(osl_Socket_DirReadWrite);
1217*cdf0e10cSrcweir                 aSocket.close();
1218*cdf0e10cSrcweir             }
1219*cdf0e10cSrcweir 
1220*cdf0e10cSrcweir 
1221*cdf0e10cSrcweir     public:
1222*cdf0e10cSrcweir // LLA: orig        void send_recv()
1223*cdf0e10cSrcweir // LLA: orig             {
1224*cdf0e10cSrcweir // LLA: orig                if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1225*cdf0e10cSrcweir // LLA: orig                    t_print("margritte is alive ! \n");
1226*cdf0e10cSrcweir // LLA: orig                if ( ifAvailable(rtl::OUString::createFromAscii("10.16.66.252")) == sal_False )
1227*cdf0e10cSrcweir // LLA: orig                {
1228*cdf0e10cSrcweir // LLA: orig            t_print("ip 10.16.66.252 is not alive! \n");
1229*cdf0e10cSrcweir // LLA: orig            return;
1230*cdf0e10cSrcweir // LLA: orig        }
1231*cdf0e10cSrcweir // LLA: orig                 ReadSocket2Thread myReadThread;
1232*cdf0e10cSrcweir // LLA: orig                 myReadThread.create();
1233*cdf0e10cSrcweir // LLA: orig
1234*cdf0e10cSrcweir // LLA: orig                 thread_sleep( 2 );
1235*cdf0e10cSrcweir // LLA: orig                 // send_Acceptor();
1236*cdf0e10cSrcweir // LLA: orig                 send_Connector();
1237*cdf0e10cSrcweir // LLA: orig
1238*cdf0e10cSrcweir // LLA: orig                 myReadThread.join();
1239*cdf0e10cSrcweir // LLA: orig
1240*cdf0e10cSrcweir // LLA: orig                 // statistics
1241*cdf0e10cSrcweir // LLA: orig                 sal_uInt32 nLength = myReadThread.getCount();
1242*cdf0e10cSrcweir // LLA: orig                 bool       bIsOk   = myReadThread.isOk(); // check if the values are right.
1243*cdf0e10cSrcweir // LLA: orig
1244*cdf0e10cSrcweir // LLA: orig                 t_print("Length:=%d\n", nLength);
1245*cdf0e10cSrcweir // LLA: orig                 t_print(" bIsOk:=%d\n", bIsOk);
1246*cdf0e10cSrcweir // LLA: orig             }
1247*cdf0e10cSrcweir 
1248*cdf0e10cSrcweir         // -----------------------------------------------------------------------------
1249*cdf0e10cSrcweir 
1250*cdf0e10cSrcweir         // LLA: send_Connector_2_margritte works, it send strings to echo server on margritte
1251*cdf0e10cSrcweir         //      but can not receive anything
1252*cdf0e10cSrcweir 
1253*cdf0e10cSrcweir         void send_Connector_2_margritte(rtl::OString const& _sAddr)
1254*cdf0e10cSrcweir             {
1255*cdf0e10cSrcweir                 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1256*cdf0e10cSrcweir                 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1257*cdf0e10cSrcweir 
1258*cdf0e10cSrcweir                 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1259*cdf0e10cSrcweir                 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1260*cdf0e10cSrcweir 
1261*cdf0e10cSrcweir                 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1262*cdf0e10cSrcweir                 if  ( aResult != osl_Socket_Ok )
1263*cdf0e10cSrcweir                 {
1264*cdf0e10cSrcweir                     t_print("# connect failed. \n" );
1265*cdf0e10cSrcweir                 }
1266*cdf0e10cSrcweir                 else
1267*cdf0e10cSrcweir                 {
1268*cdf0e10cSrcweir                     // blocking mode, if read/recv failed, block until success
1269*cdf0e10cSrcweir                     aSocket.enableNonBlockingMode( sal_False );
1270*cdf0e10cSrcweir 
1271*cdf0e10cSrcweir                     ::osl::StreamSocket ssStreamConnection(aSocket);
1272*cdf0e10cSrcweir 
1273*cdf0e10cSrcweir                     char const * pBuffer = "Test String\n";
1274*cdf0e10cSrcweir                     sal_Int32 nBufferSize = strlen(pBuffer);
1275*cdf0e10cSrcweir                     sal_Int32 nWriteNumber = ssStreamConnection.write( pBuffer, nBufferSize );
1276*cdf0e10cSrcweir 
1277*cdf0e10cSrcweir                     // char * pBuffer2 = "                                                                                                 ";
1278*cdf0e10cSrcweir                     // sal_Int32 nReadNumber = ssStreamConnection.read( pBuffer2, strlen(pBuffer2) );
1279*cdf0e10cSrcweir 
1280*cdf0e10cSrcweir                     char const * pBuffer3 = "quit\n";
1281*cdf0e10cSrcweir                     nBufferSize = strlen(pBuffer3);
1282*cdf0e10cSrcweir                     nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1283*cdf0e10cSrcweir 
1284*cdf0e10cSrcweir                     ssStreamConnection.close();
1285*cdf0e10cSrcweir                 }
1286*cdf0e10cSrcweir                 aSocket.close();
1287*cdf0e10cSrcweir             }
1288*cdf0e10cSrcweir 
1289*cdf0e10cSrcweir         void send_recv_2_margritte()
1290*cdf0e10cSrcweir             {
1291*cdf0e10cSrcweir                 rtl::OString sAddr;
1292*cdf0e10cSrcweir                 sAddr = "margritte.germany.sun.com";
1293*cdf0e10cSrcweir             	if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1294*cdf0e10cSrcweir                 {
1295*cdf0e10cSrcweir                     t_print("found %s!\n", sAddr.getStr());
1296*cdf0e10cSrcweir                 }
1297*cdf0e10cSrcweir                 send_Connector_2_margritte(sAddr);
1298*cdf0e10cSrcweir             }
1299*cdf0e10cSrcweir 
1300*cdf0e10cSrcweir         // -----------------------------------------------------------------------------
1301*cdf0e10cSrcweir 
1302*cdf0e10cSrcweir         void send_recv()
1303*cdf0e10cSrcweir             {
1304*cdf0e10cSrcweir                 rtl::OString sAddr;
1305*cdf0e10cSrcweir                 // if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1306*cdf0e10cSrcweir                 // {
1307*cdf0e10cSrcweir                 //     t_print("margritte is alive ! \n");
1308*cdf0e10cSrcweir                 //     sAddr = "margritte.germany";
1309*cdf0e10cSrcweir                 // }
1310*cdf0e10cSrcweir 
1311*cdf0e10cSrcweir                 sAddr = "margritte.germany.sun.com";
1312*cdf0e10cSrcweir             	if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1313*cdf0e10cSrcweir                 {
1314*cdf0e10cSrcweir                     t_print("found %s!\n", sAddr.getStr());
1315*cdf0e10cSrcweir                 }
1316*cdf0e10cSrcweir //                 else
1317*cdf0e10cSrcweir //                 {
1318*cdf0e10cSrcweir //                     if ( ifAvailable(rtl::OUString::createFromAscii("192.168.7.2")) == sal_True )
1319*cdf0e10cSrcweir //                     {
1320*cdf0e10cSrcweir //                         sAddr = "192.168.7.2";
1321*cdf0e10cSrcweir //                         t_print("moon found ! \n");
1322*cdf0e10cSrcweir //                     }
1323*cdf0e10cSrcweir //                     else
1324*cdf0e10cSrcweir //                     {
1325*cdf0e10cSrcweir //                         if ( ifAvailable(rtl::OUString::createFromAscii("moon.linux.bogus")) == sal_True )
1326*cdf0e10cSrcweir //                         {
1327*cdf0e10cSrcweir //                             sAddr = "moon.linux.bogus";
1328*cdf0e10cSrcweir //                             t_print("moon found ! \n");
1329*cdf0e10cSrcweir //                         }
1330*cdf0e10cSrcweir //                         else
1331*cdf0e10cSrcweir //                         {
1332*cdf0e10cSrcweir //                             if ( ifAvailable(rtl::OUString::createFromAscii("moon")) == sal_True )
1333*cdf0e10cSrcweir //                             {
1334*cdf0e10cSrcweir //                                 sAddr = "moon";
1335*cdf0e10cSrcweir //                                 t_print("moon found ! \n");
1336*cdf0e10cSrcweir //                             }
1337*cdf0e10cSrcweir //                         }
1338*cdf0e10cSrcweir //                     }
1339*cdf0e10cSrcweir //                 }
1340*cdf0e10cSrcweir 
1341*cdf0e10cSrcweir                 // if ( ifAvailable(rtl::OUString::createFromAscii("10.16.64.196")) == sal_False )
1342*cdf0e10cSrcweir                 // {
1343*cdf0e10cSrcweir                 //     t_print("ip 10.16.64.196 is not alive! \n");
1344*cdf0e10cSrcweir                 //     return;
1345*cdf0e10cSrcweir                 // }
1346*cdf0e10cSrcweir 
1347*cdf0e10cSrcweir                 osl::Condition aCondition;
1348*cdf0e10cSrcweir                 ReadSocket2Thread myReadThread(aCondition);
1349*cdf0e10cSrcweir                 myReadThread.setAddr(sAddr);
1350*cdf0e10cSrcweir //                myReadThread.create();
1351*cdf0e10cSrcweir 
1352*cdf0e10cSrcweir                 thread_sleep( 2 );
1353*cdf0e10cSrcweir                 if (! myReadThread.getFailed())
1354*cdf0e10cSrcweir                 {
1355*cdf0e10cSrcweir                     // send_Acceptor(sAddr, aCondition);
1356*cdf0e10cSrcweir                     send_Connector(sAddr, aCondition);
1357*cdf0e10cSrcweir 
1358*cdf0e10cSrcweir                     thread_sleep( 2 );
1359*cdf0e10cSrcweir                     if (myReadThread.isRunning())
1360*cdf0e10cSrcweir                     {
1361*cdf0e10cSrcweir                         myReadThread.join();
1362*cdf0e10cSrcweir                     }
1363*cdf0e10cSrcweir                     // termAndJoinThread(&myReadThread);
1364*cdf0e10cSrcweir 
1365*cdf0e10cSrcweir                     // statistics
1366*cdf0e10cSrcweir                     sal_uInt32 nLength = myReadThread.getCount();
1367*cdf0e10cSrcweir                     bool       bIsOk   = myReadThread.isOk(); // check if the values are right.
1368*cdf0e10cSrcweir 
1369*cdf0e10cSrcweir                     t_print("Length:=%d\n", nLength);
1370*cdf0e10cSrcweir                     t_print(" bIsOk:=%d\n", bIsOk);
1371*cdf0e10cSrcweir                 }
1372*cdf0e10cSrcweir                 else
1373*cdf0e10cSrcweir                 {
1374*cdf0e10cSrcweir                     t_print("ERROR: No echo Server on %s found.\n", sAddr.getStr());
1375*cdf0e10cSrcweir                 }
1376*cdf0e10cSrcweir             }
1377*cdf0e10cSrcweir 
1378*cdf0e10cSrcweir 
1379*cdf0e10cSrcweir         void getPage(rtl::OString const& _sAddr);
1380*cdf0e10cSrcweir         void test_getPage()
1381*cdf0e10cSrcweir             {
1382*cdf0e10cSrcweir                 // rtl::OString sPage("lla-1.germany.sun.com");
1383*cdf0e10cSrcweir                 // getPage(sPage);
1384*cdf0e10cSrcweir 
1385*cdf0e10cSrcweir                 rtl::OString sPage("lla-1");
1386*cdf0e10cSrcweir                 getPage(sPage);
1387*cdf0e10cSrcweir             }
1388*cdf0e10cSrcweir 
1389*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE( justtest );
1390*cdf0e10cSrcweir         CPPUNIT_TEST( send_recv );
1391*cdf0e10cSrcweir         CPPUNIT_TEST( test_getPage );
1392*cdf0e10cSrcweir         CPPUNIT_TEST_SUITE_END();
1393*cdf0e10cSrcweir     }; // class isExceptionPending
1394*cdf0e10cSrcweir 
1395*cdf0e10cSrcweir 
1396*cdf0e10cSrcweir     void justtest::getPage(rtl::OString const& _sAddr)
1397*cdf0e10cSrcweir             {
1398*cdf0e10cSrcweir                 rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr());
1399*cdf0e10cSrcweir                 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1400*cdf0e10cSrcweir                 ::osl::SocketAddr aSocketAddr( suAddr, 80 );
1401*cdf0e10cSrcweir 
1402*cdf0e10cSrcweir             {
1403*cdf0e10cSrcweir                 // some checks
1404*cdf0e10cSrcweir                 aSocketAddr.getPort();
1405*cdf0e10cSrcweir                 oslSocketResult aResult;
1406*cdf0e10cSrcweir                 rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult);
1407*cdf0e10cSrcweir                 if (aResult != osl_Socket_Ok)
1408*cdf0e10cSrcweir                 {
1409*cdf0e10cSrcweir                     t_print("problem with getAddr: ");
1410*cdf0e10cSrcweir                     printSocketResult(aResult);
1411*cdf0e10cSrcweir                 }
1412*cdf0e10cSrcweir 
1413*cdf0e10cSrcweir                 rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1414*cdf0e10cSrcweir                 if (aResult != osl_Socket_Ok)
1415*cdf0e10cSrcweir                 {
1416*cdf0e10cSrcweir                     t_print("problem with hostname: ");
1417*cdf0e10cSrcweir                     printSocketResult(aResult);
1418*cdf0e10cSrcweir                 }
1419*cdf0e10cSrcweir             }
1420*cdf0e10cSrcweir 
1421*cdf0e10cSrcweir                 oslSocketResult aResult;
1422*cdf0e10cSrcweir 
1423*cdf0e10cSrcweir                 // SocketAddr::resolveHostname(suAddr, aSocketAddr);
1424*cdf0e10cSrcweir                 // if (! aSocketAddr.is())
1425*cdf0e10cSrcweir                 // {
1426*cdf0e10cSrcweir                 //     t_print("Can't resolve Hostname.\n");
1427*cdf0e10cSrcweir                 //     return;
1428*cdf0e10cSrcweir                 // }
1429*cdf0e10cSrcweir                 // rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1430*cdf0e10cSrcweir                 // if (aResult != osl_Socket_Ok)
1431*cdf0e10cSrcweir                 // {
1432*cdf0e10cSrcweir                 //     t_print("problem with hostname: ");
1433*cdf0e10cSrcweir                 //     printSocketResult(aResult);
1434*cdf0e10cSrcweir                 //
1435*cdf0e10cSrcweir                 // }
1436*cdf0e10cSrcweir 
1437*cdf0e10cSrcweir                 if (! aSocketAddr.is())
1438*cdf0e10cSrcweir                 {
1439*cdf0e10cSrcweir                     t_print("SocketAddr::is() failed.\n");
1440*cdf0e10cSrcweir                     return;
1441*cdf0e10cSrcweir                 }
1442*cdf0e10cSrcweir 
1443*cdf0e10cSrcweir                 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1444*cdf0e10cSrcweir                 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1445*cdf0e10cSrcweir 
1446*cdf0e10cSrcweir                 aResult = aSocket.connect( aSocketAddr );
1447*cdf0e10cSrcweir                 if  ( aResult != osl_Socket_Ok )
1448*cdf0e10cSrcweir                 {
1449*cdf0e10cSrcweir                     t_print("# send_Connector: connect failed. \n" );
1450*cdf0e10cSrcweir                 }
1451*cdf0e10cSrcweir                 else
1452*cdf0e10cSrcweir                 {
1453*cdf0e10cSrcweir                     // blocking mode, if read/recv failed, block until success
1454*cdf0e10cSrcweir //                    aSocket.enableNonBlockingMode( sal_False );
1455*cdf0e10cSrcweir 
1456*cdf0e10cSrcweir //                     _aCondition.set();
1457*cdf0e10cSrcweir 
1458*cdf0e10cSrcweir                     ::osl::StreamSocket ssStreamConnection(aSocket);
1459*cdf0e10cSrcweir 
1460*cdf0e10cSrcweir                     char const * pBuffer = "GET / HTTP/1.0\015\012\015\012";
1461*cdf0e10cSrcweir                     sal_Int32 nBufferSize = strlen(pBuffer);
1462*cdf0e10cSrcweir                     ssStreamConnection.write( pBuffer, nBufferSize );
1463*cdf0e10cSrcweir 
1464*cdf0e10cSrcweir 
1465*cdf0e10cSrcweir                     char *pBufferPeek = (char*) malloc(1024);
1466*cdf0e10cSrcweir                     sal_Int32 nReadNumber = 1;
1467*cdf0e10cSrcweir                     while ( nReadNumber != 0)
1468*cdf0e10cSrcweir                     {
1469*cdf0e10cSrcweir                         nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1470*cdf0e10cSrcweir                         if (nReadNumber > 0)
1471*cdf0e10cSrcweir                         {
1472*cdf0e10cSrcweir                             char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1473*cdf0e10cSrcweir                             sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1474*cdf0e10cSrcweir                             pBuffer2[nReadNumberReal] = '\0';
1475*cdf0e10cSrcweir                             t_print("%s", pBuffer2);
1476*cdf0e10cSrcweir                             free(pBuffer2);
1477*cdf0e10cSrcweir                         }
1478*cdf0e10cSrcweir                     }
1479*cdf0e10cSrcweir                     free(pBufferPeek);
1480*cdf0e10cSrcweir 
1481*cdf0e10cSrcweir                     // char * pBuffer3 = "quit\n";
1482*cdf0e10cSrcweir                     // nBufferSize = strlen(pBuffer3);
1483*cdf0e10cSrcweir                     // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1484*cdf0e10cSrcweir 
1485*cdf0e10cSrcweir                     rtl::OUString suError = ssStreamConnection.getErrorAsString();
1486*cdf0e10cSrcweir                 }
1487*cdf0e10cSrcweir                 aSocket.shutdown(osl_Socket_DirReadWrite);
1488*cdf0e10cSrcweir                 aSocket.close();
1489*cdf0e10cSrcweir             }
1490*cdf0e10cSrcweir 
1491*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1492*cdf0e10cSrcweir 
1493*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket");
1494*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket");
1495*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket");
1496*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket");
1497*cdf0e10cSrcweir 
1498*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::justtest, "osl_StreamSocket");
1499*cdf0e10cSrcweir 
1500*cdf0e10cSrcweir } // namespace osl_StreamSocket
1501*cdf0e10cSrcweir 
1502*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1503*cdf0e10cSrcweir 
1504*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
1505*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
1506*cdf0e10cSrcweir NOADDITIONAL;
1507