00001 00014 // TcpConnectionServer.cpp -- implementation for the TcpConnectionServer class 00015 00016 #include <errno.h> 00017 #include "TcpConnectionServer.h" 00018 #include "TcpSocket.h" 00019 #include "SystemCall.h" 00020 00021 00023 // TcpConnectionServer 00024 00025 00026 // Construct from native OS socket descriptor 00027 TcpConnectionServer::TcpConnectionServer(SOCKET descriptor) : Socket(descriptor) 00028 { 00029 } 00030 00031 // Construct and bind to the SocketAddress 00032 // backlog is the maximum length of the queue of pending connections 00033 TcpConnectionServer::TcpConnectionServer(const SocketAddress& address,int backlog) : Socket(SOCK_STREAM, address.getAddressFamily()) 00034 { 00035 if (address.isIpAddress()) 00036 setReuseLocalAddresses(true); 00037 00038 bind( address ); 00039 listen( backlog ); 00040 } 00041 00042 // Permits an incoming connection attempt on a socket 00043 bool TcpConnectionServer::accept( TcpSocket& socket ) 00044 { 00045 char buffer[ 200 ]; 00046 #ifdef _MSC_VER 00047 int size = sizeof(buffer); 00048 #else 00049 socklen_t size = sizeof( sockaddr ); 00050 #endif 00051 00052 int result; 00053 SOCKET_CALL_3(result=(int),::accept,getDescriptor(),(sockaddr*)buffer,&size) 00054 if ( result >= 0 ) { 00055 socket.close(); 00056 socket.setDescriptor(result); 00057 return true; 00058 } 00059 else { 00060 return false; 00061 } 00062 } 00063 00064 // Places socket state to listen for incoming connections. 00065 // backlog is the maximum length of the queue of pending connections 00066 void TcpConnectionServer::listen( int backlog ) 00067 { 00068 int dummy; 00069 SOCKET_CALL_2( dummy =, ::listen, getDescriptor(), backlog ) 00070 }