1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
51 #include <arpa/inet.h>
61 #include "OSGSocketAddress.h"
62 #include "OSGStreamSocket.h"
67 /** \class OSG::StreamSocket
68 * \brief Stream socket handler
70 * This class is a Handler to connection oriented sockets. A call to
71 * <EM>open</EM> will assing a stream socket and <EM>close</EM>
72 * releases the socket.
79 * s.connect(Address("serverhost.com",4567);
89 * s.bind(AnySocketAddress(4567);
90 * c=s.accept(); // accept incomming client
91 * c.recv(buffer,100); // read client message
97 /*-------------------------------------------------------------------------*/
98 /* constructor destructor */
100 /*! Constructor. Use open to assign a system socket. No system socket is assigned by
102 \see StreamSocket::open
104 StreamSocket::StreamSocket():
109 /** \brief Copy constructor
111 StreamSocket::StreamSocket(const StreamSocket
&source
):
116 /*-------------------------------------------------------------------------*/
117 /* Socket functionaliy */
119 /*! Assign a socket. <CODE>Open</CODE> assignes a system socket to the
123 void StreamSocket::open()
125 _sd
= ::socket(AF_INET
, SOCK_STREAM
, 0);
128 throw SocketError("socket()");
134 /* int rc = */ setsockopt(_sd
, SOL_SOCKET
, SO_LINGER
,
135 reinterpret_cast<SocketOptT
*>(&li
), sizeof(li
));
140 void StreamSocket::close(void)
149 /*! Accept incomming connection. Use the returned StreamSocket to
150 communicate over the accepted communication. If the new StreamSocket
151 is no longer used, you have to close it. A new StreamSocket is
152 returned to communicate with the accepted client.
154 StreamSocket
StreamSocket::acceptFrom(SocketAddress
&address
)
159 len
=address
.getSockAddrSize();
160 client
._sd
=::accept(_sd
,
161 address
.getSockAddr(),
165 throw SocketError("accept()");
170 /*! Accept incomming connection. Use the returned StreamSocket to
171 communicate over the accepted communication. If the new StreamSocket
172 is no longer used, you have to close it.
174 StreamSocket
StreamSocket::accept()
177 return acceptFrom(addr
);
180 /*! A Stream socket doesen't send data immediately. Only if the internal
181 buffer contains enough data, an immediate write is forced. If
182 delay is set to false, then data is written always immediately.
184 void StreamSocket::setDelay(bool value
)
188 rc
=setsockopt(_sd
, IPPROTO_TCP
, TCP_NODELAY
,
189 reinterpret_cast<SocketOptT
*>(&on
), sizeof(on
));
192 throw SocketError("setsockopt(,SOCK_STREAM,TCP_NODELAY)");
196 /*-------------------------------------------------------------------------*/
201 const StreamSocket
& StreamSocket::operator =(const StreamSocket
&source
)