Skip to content

Commit

Permalink
Merge pull request #192 from bramton/tcpsocket
Browse files Browse the repository at this point in the history
Use correct addrlen when connecting
  • Loading branch information
rafael2k authored Jun 18, 2024
2 parents c563767 + bf9e966 commit 8dbb849
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions darkice/trunk/src/TcpSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ TcpSocket :: open ( void )
sockfd = 0;
throw Exception( __FILE__, __LINE__, "gethostbyname error", errno);
}

memset( &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
Expand All @@ -240,12 +240,25 @@ TcpSocket :: open ( void )
// set TCP keep-alive
optval = 1;
optlen = sizeof(optval);
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) {
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) {
reportEvent(5, "can't set TCP socket keep-alive mode", errno);
}

// connect
if ( connect( sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) {
socklen_t addrlen;
switch (addr.ss_family) {
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
addrlen = sizeof(struct sockaddr_in6);
break;
default:
throw Exception( __FILE__, __LINE__, "invalid address family", errno);
}


if ( connect( sockfd, (struct sockaddr*)&addr, addrlen) == -1 ) {
::close( sockfd);
sockfd = 0;
throw Exception( __FILE__, __LINE__, "connect error", errno);
Expand Down

0 comments on commit 8dbb849

Please sign in to comment.