diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2016-11-07 21:25:36 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2016-11-07 21:37:30 +0100 |
commit | 80f804505dc970613fd9d90b7d3ce52f9fd40c7a (patch) | |
tree | e761faa1c066ef28bc9146be4b2491811a9c1400 | |
parent | 596f46de6532834f5143405120a3896fca27bb6d (diff) | |
download | dabmux-80f804505dc970613fd9d90b7d3ce52f9fd40c7a.tar.gz dabmux-80f804505dc970613fd9d90b7d3ce52f9fd40c7a.tar.bz2 dabmux-80f804505dc970613fd9d90b7d3ce52f9fd40c7a.zip |
Only initialise the socket in TcpSocket when necessary
-rw-r--r-- | src/TcpSocket.cpp | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index 6791286..b9824fa 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -39,31 +39,30 @@ using namespace std; TcpSocket::TcpSocket() : m_sock(INVALID_SOCKET) { - if ((m_sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { - throw std::runtime_error("Can't create socket"); - } } TcpSocket::TcpSocket(int port, const string& name) : m_sock(INVALID_SOCKET) { - if ((m_sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { - throw std::runtime_error("Can't create socket"); - } + if (port) { + if ((m_sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { + throw std::runtime_error("Can't create socket"); + } - reuseopt_t reuse = 1; - if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) - == SOCKET_ERROR) { - throw std::runtime_error("Can't reuse address"); - } + reuseopt_t reuse = 1; + if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) + == SOCKET_ERROR) { + throw std::runtime_error("Can't reuse address"); + } - m_own_address.setAddress(name); - m_own_address.setPort(port); + m_own_address.setAddress(name); + m_own_address.setPort(port); - if (bind(m_sock, m_own_address.getAddress(), sizeof(sockaddr_in)) == SOCKET_ERROR) { - ::close(m_sock); - m_sock = INVALID_SOCKET; - throw std::runtime_error("Can't bind socket"); + if (bind(m_sock, m_own_address.getAddress(), sizeof(sockaddr_in)) == SOCKET_ERROR) { + ::close(m_sock); + m_sock = INVALID_SOCKET; + throw std::runtime_error("Can't bind socket"); + } } } |