Skip to content

Commit

Permalink
Use correct addrlen when connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
bramton committed Apr 4, 2024
1 parent 93b8d97 commit bf9e966
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 bf9e966

Please sign in to comment.