19 #include "connectiontcpserver.h"
20 #include "connectiontcpclient.h"
21 #include "connectionhandler.h"
25 #include "mutexguard.h"
32 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
33 # include <netinet/in.h>
34 # include <arpa/nameser.h>
37 # include <arpa/inet.h>
38 # include <sys/socket.h>
40 # include <sys/select.h>
45 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
47 #elif defined( _WIN32_WCE )
48 # include <winsock2.h>
55 # include <sys/types.h>
62 const std::string& ip,
int port )
64 m_connectionHandler( ch )
92 #ifdef HAVE_SETSOCKOPT
94 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
95 int bufbytes =
sizeof( int );
97 socklen_t bufbytes =
sizeof( int );
99 if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (
char*)&buf, &bufbytes ) != -1 ) &&
100 ( m_bufsize > buf ) )
101 setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (
char*)&m_bufsize,
sizeof( m_bufsize ) );
103 if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (
char*)&buf, &bufbytes ) != -1 ) &&
104 ( m_bufsize > buf ) )
105 setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (
char*)&m_bufsize,
sizeof( m_bufsize ) );
108 struct sockaddr_in local;
109 local.sin_family = AF_INET;
110 local.sin_port =
static_cast<unsigned short int>( htons(
m_port ) );
111 local.sin_addr.s_addr =
m_server.empty() ? INADDR_ANY : inet_addr(
m_server.c_str() );
112 memset( local.sin_zero,
'\0', 8 );
114 if( bind( m_socket, (
struct sockaddr*)&local,
sizeof(
struct sockaddr ) ) < 0 )
116 std::string message =
"bind() to " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
117 +
" (" + inet_ntoa( local.sin_addr ) +
":" + util::int2string(
m_port ) +
") failed. "
118 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
119 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
121 "errno: " + util::int2string( errno );
128 if( listen( m_socket, 10 ) < 0 )
130 std::string message =
"listen on " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
131 +
" (" + inet_ntoa( local.sin_addr ) +
":" + util::int2string(
m_port ) +
") failed. "
132 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
133 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
135 "errno: " + util::int2string( errno );
150 if( m_cancel || m_socket < 0 || !m_connectionHandler )
156 if( !dataAvailable( timeout ) )
162 struct sockaddr_in they;
163 int sin_size =
sizeof(
struct sockaddr_in );
164 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
165 int newfd =
static_cast<int>( accept( static_cast<SOCKET>( m_socket ), (
struct sockaddr*)&they, &sin_size ) );
167 int newfd = accept( m_socket, (
struct sockaddr*)&they, (socklen_t*)&sin_size );
173 ntohs( they.sin_port ) );