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