| 135 | TcpSocket::TcpSocket(NetworkMode networkMode, SocketImplPtr impl) : Socket(networkMode, impl, SocketMode::Connected) {} |
| 136 | |
| 137 | void TcpSocket::connect(HostAddressWithPort const& addressWithPort) { |
| 138 | WriteLocker locker(m_mutex); |
| 139 | checkOpen("TcpSocket::connect"); |
| 140 | |
| 141 | if (m_networkMode != addressWithPort.address().mode()) |
| 142 | throw NetworkException("Socket address type mismatch between address and socket."); |
| 143 | |
| 144 | struct sockaddr_storage sockAddr; |
| 145 | socklen_t sockAddrLen; |
| 146 | setNativeFromAddress(addressWithPort, &sockAddr, &sockAddrLen); |
| 147 | if (::connect(m_impl->socketDesc, (struct sockaddr*)&sockAddr, sockAddrLen) < 0) |
| 148 | throw NetworkException(strf("cannot connect to {}: {}", addressWithPort, netErrorString())); |
| 149 | |
| 150 | #if defined STAR_SYSTEM_MACOS || defined STAR_SYSTEM_FREEBSD |
| 151 | // Don't generate sigpipe |
| 152 | int set = 1; |
| 153 | m_impl->setSockOpt(SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(set)); |
| 154 | #endif |
| 155 | |
| 156 | m_socketMode = SocketMode::Connected; |
| 157 | m_remoteAddress = addressWithPort; |
| 158 | } |
| 159 | |
| 160 | TcpServer::TcpServer(HostAddressWithPort const& address) : m_hostAddress(address) { |
| 161 | m_hostAddress = address; |